diff --git a/src/infrastructure/logging/factory.ts b/src/infrastructure/logging/factory.ts index 3104b3ca..41a56a2d 100644 --- a/src/infrastructure/logging/factory.ts +++ b/src/infrastructure/logging/factory.ts @@ -1,5 +1,5 @@ -import * as os from 'node:os'; import * as path from 'node:path'; +import { execFileSync } from 'node:child_process'; import { ILogger, ILoggerOptions, LogLevel } from '../../domain/interfaces/ILogger'; import { Logger } from './Logger'; import { NullLogger } from './NullLogger'; @@ -10,8 +10,20 @@ function isValidLogLevel(value: string): value is LogLevel { return VALID_LEVELS.includes(value as LogLevel); } +function getGitRoot(): string | null { + try { + return execFileSync('git', ['rev-parse', '--show-toplevel'], { + encoding: 'utf8', + stdio: ['pipe', 'pipe', 'pipe'], + }).trim(); + } catch { + return null; + } +} + export function defaultLogDir(): string { - return path.join(os.homedir(), '.git-mem', 'logs'); + const base = getGitRoot() ?? process.cwd(); + return path.join(base, '.git-mem', 'logs'); } export function loadLoggerOptions(): ILoggerOptions { diff --git a/tests/unit/infrastructure/logging/factory.test.ts b/tests/unit/infrastructure/logging/factory.test.ts index d3366819..5e56d697 100644 --- a/tests/unit/infrastructure/logging/factory.test.ts +++ b/tests/unit/infrastructure/logging/factory.test.ts @@ -1,5 +1,7 @@ import { describe, it, before, after } from 'node:test'; import assert from 'node:assert/strict'; +import { join } from 'path'; +import { execFileSync } from 'child_process'; import { createLogger, createNullLogger, defaultLogDir, loadLoggerOptions } from '../../../../src/infrastructure/logging/factory'; import { Logger } from '../../../../src/infrastructure/logging/Logger'; import { NullLogger } from '../../../../src/infrastructure/logging/NullLogger'; @@ -27,6 +29,16 @@ describe('Logging factory', () => { } }); + describe('defaultLogDir', () => { + it('should return .git-mem/logs under the git root', () => { + const gitRoot = execFileSync('git', ['rev-parse', '--show-toplevel'], { + encoding: 'utf8', + }).trim(); + + assert.equal(defaultLogDir(), join(gitRoot, '.git-mem', 'logs')); + }); + }); + describe('loadLoggerOptions', () => { it('should return defaults when no env vars set', () => { delete process.env['LOG_LEVEL'];