🪵 refactor: Dynamic getLogDirectory utility for Loggers (#8686)

This commit is contained in:
Danny Avila 2025-07-26 20:11:20 -04:00 committed by GitHub
parent 545a909953
commit f4facb7d35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 43 additions and 6 deletions

View file

@ -1,6 +1,6 @@
{
"name": "@librechat/data-schemas",
"version": "0.0.12",
"version": "0.0.13",
"description": "Mongoose schemas and models for LibreChat",
"type": "module",
"main": "dist/index.cjs",

View file

@ -1,8 +1,8 @@
import path from 'path';
import winston from 'winston';
import 'winston-daily-rotate-file';
import { getLogDirectory } from './utils';
const logDir = path.join(__dirname, '..', '..', '..', 'api', 'logs');
const logDir = getLogDirectory();
const { NODE_ENV, DEBUG_LOGGING = 'false' } = process.env;

View file

@ -0,0 +1,37 @@
import path from 'path';
/**
* Determine the log directory in a cross-compatible way.
* Priority:
* 1. LIBRECHAT_LOG_DIR environment variable
* 2. If running within LibreChat monorepo (when cwd ends with /api), use api/logs
* 3. If api/logs exists relative to cwd, use that (for running from project root)
* 4. Otherwise, use logs directory relative to process.cwd()
*
* This avoids using __dirname which is not available in ESM modules
*/
export const getLogDirectory = (): string => {
if (process.env.LIBRECHAT_LOG_DIR) {
return process.env.LIBRECHAT_LOG_DIR;
}
const cwd = process.cwd();
// Check if we're running from within the api directory
if (cwd.endsWith('/api') || cwd.endsWith('\\api')) {
return path.join(cwd, 'logs');
}
// Check if api/logs exists relative to current directory (running from project root)
// We'll just use the path and let the file system create it if needed
const apiLogsPath = path.join(cwd, 'api', 'logs');
// For LibreChat project structure, use api/logs
// For external consumers, they should set LIBRECHAT_LOG_DIR
if (cwd.includes('LibreChat')) {
return apiLogsPath;
}
// Default to logs directory relative to current working directory
return path.join(cwd, 'logs');
};

View file

@ -1,9 +1,9 @@
import path from 'path';
import winston from 'winston';
import 'winston-daily-rotate-file';
import { redactFormat, redactMessage, debugTraverse, jsonTruncateFormat } from './parsers';
import { getLogDirectory } from './utils';
const logDir = path.join(__dirname, '..', '..', '..', 'api', 'logs');
const logDir = getLogDirectory();
const { NODE_ENV, DEBUG_LOGGING, CONSOLE_JSON, DEBUG_CONSOLE } = process.env;