Skip to content

Commit ca5d7f7

Browse files
committed
chore: improve logger save path
1 parent f151579 commit ca5d7f7

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/config.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import type { ClarityConfig } from './types'
2-
import { join } from 'node:path'
2+
import { join, relative, resolve } from 'node:path'
33
import process from 'node:process'
44
import { loadConfig as bunfigLoadConfig } from 'bunfig'
55

66
// Get project root directory (where the package.json is located)
7-
function getProjectRoot(): string {
8-
return process.cwd()
7+
function getProjectRoot(filePath?: string, options: { relative?: boolean } = {}): string {
8+
let path = process.cwd()
9+
10+
while (path.includes('storage')) path = resolve(path, '..')
11+
12+
const finalPath = resolve(path, filePath || '')
13+
14+
// If the `relative` option is true, return the path relative to the current working directory
15+
if (options?.relative)
16+
return relative(process.cwd(), finalPath)
17+
18+
return finalPath
919
}
1020

1121
// Default log directory is in project root
12-
const defaultLogDirectory = process.env.CLARITY_LOG_DIR || join(getProjectRoot(), 'logs')
22+
const defaultLogDirectory = process.env.CLARITY_LOG_DIR || join(getProjectRoot(), 'storage/logs')
1323

1424
export const defaultConfig: ClarityConfig = {
1525
level: 'info',

src/logger.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ClarityConfig, EncryptionConfig, Formatter, LogEntry, LoggerOption
33
import { Buffer } from 'node:buffer'
44
import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto'
55
import { closeSync, createReadStream, createWriteStream, existsSync, fsyncSync, openSync, writeFileSync } from 'node:fs'
6-
import { mkdir, readdir, rename, stat, unlink, writeFile } from 'node:fs/promises'
6+
import { access, constants, mkdir, readdir, rename, stat, unlink, writeFile } from 'node:fs/promises'
77
import { join } from 'node:path'
88
import { pipeline } from 'node:stream/promises'
99
import { createGzip } from 'node:zlib'
@@ -88,6 +88,12 @@ export class Logger {
8888
this.config.logDirectory = defaultConfig.logDirectory
8989
}
9090

91+
// Ensure storage/logs folder structure exists
92+
if (!isBrowserProcess()) {
93+
mkdir(this.config.logDirectory, { recursive: true, mode: 0o755 })
94+
.catch(err => console.error('Failed to create log directory:', err))
95+
}
96+
9197
this.currentLogFile = this.generateLogFilename()
9298

9399
this.encryptionKeys = new Map()
@@ -166,9 +172,16 @@ export class Logger {
166172
const operationPromise = (async () => {
167173
let fd: number | undefined
168174
try {
169-
// Ensure log directory exists
175+
// Ensure storage/logs folder structure exists
170176
try {
171-
await mkdir(this.config.logDirectory, { recursive: true, mode: 0o755 })
177+
// First check if directory exists to avoid unnecessary mkdir calls
178+
try {
179+
await access(this.config.logDirectory, constants.F_OK | constants.W_OK)
180+
}
181+
catch {
182+
// If directory doesn't exist or isn't writable, create it
183+
await mkdir(this.config.logDirectory, { recursive: true, mode: 0o755 })
184+
}
172185
}
173186
catch (err) {
174187
console.error('Debug: [writeToFile] Failed to create log directory:', err)

0 commit comments

Comments
 (0)