removed configs from all libs and will inject them within the services themselves
This commit is contained in:
parent
fd28162811
commit
6cc5b339bc
32 changed files with 366 additions and 349 deletions
|
|
@ -5,10 +5,10 @@
|
|||
*/
|
||||
|
||||
// Core logger classes and functions
|
||||
export { Logger, getLogger, shutdownLoggers } from './logger';
|
||||
export { Logger, getLogger, shutdownLoggers, setLoggerConfig } from './logger';
|
||||
|
||||
// Type definitions
|
||||
export type { LogLevel, LogContext, LogMetadata } from './types';
|
||||
export type { LogLevel, LogContext, LogMetadata, LoggerConfig } from './types';
|
||||
|
||||
// Default export
|
||||
export { getLogger as default } from './logger';
|
||||
|
|
|
|||
|
|
@ -9,23 +9,41 @@
|
|||
*/
|
||||
|
||||
import pino from 'pino';
|
||||
import { loggingConfig, lokiConfig } from '@stock-bot/config';
|
||||
import type { LogContext, LogLevel, LogMetadata } from './types';
|
||||
import type { LogContext, LogLevel, LogMetadata, LoggerConfig } from './types';
|
||||
|
||||
// Simple cache for logger instances
|
||||
const loggerCache = new Map<string, pino.Logger>();
|
||||
console.log('Logger cache initialized: ', loggingConfig.LOG_LEVEL);
|
||||
|
||||
// Global config that can be set
|
||||
let globalConfig: LoggerConfig = {
|
||||
logLevel: 'info',
|
||||
logConsole: true,
|
||||
logFile: false,
|
||||
logFilePath: './logs',
|
||||
logLoki: false,
|
||||
environment: 'development',
|
||||
};
|
||||
|
||||
/**
|
||||
* Set global logger configuration
|
||||
*/
|
||||
export function setLoggerConfig(config: LoggerConfig): void {
|
||||
globalConfig = { ...globalConfig, ...config };
|
||||
// Clear cache to force recreation with new config
|
||||
loggerCache.clear();
|
||||
console.log('Logger config updated:', globalConfig.logLevel);
|
||||
}
|
||||
/**
|
||||
* Create transport configuration
|
||||
*/
|
||||
function createTransports(serviceName: string): any {
|
||||
function createTransports(serviceName: string, config: LoggerConfig = globalConfig): any {
|
||||
const targets: any[] = [];
|
||||
// const isDev = loggingConfig.LOG_ENVIRONMENT === 'development';
|
||||
|
||||
// Console transport
|
||||
if (loggingConfig.LOG_CONSOLE) {
|
||||
if (config.logConsole) {
|
||||
targets.push({
|
||||
target: 'pino-pretty',
|
||||
level: loggingConfig.LOG_LEVEL, // Only show errors on console
|
||||
level: config.logLevel || 'info',
|
||||
options: {
|
||||
colorize: true,
|
||||
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
|
||||
|
|
@ -40,29 +58,35 @@ function createTransports(serviceName: string): any {
|
|||
}
|
||||
|
||||
// File transport
|
||||
if (loggingConfig.LOG_FILE) {
|
||||
if (config.logFile) {
|
||||
targets.push({
|
||||
target: 'pino/file',
|
||||
level: loggingConfig.LOG_LEVEL,
|
||||
level: config.logLevel || 'info',
|
||||
options: {
|
||||
destination: `${loggingConfig.LOG_FILE_PATH}/${serviceName}.log`,
|
||||
destination: `${config.logFilePath}/${serviceName}.log`,
|
||||
mkdir: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Loki transport
|
||||
if (lokiConfig.LOKI_HOST) {
|
||||
if (config.logLoki && config.lokiHost) {
|
||||
targets.push({
|
||||
target: 'pino-loki',
|
||||
level: loggingConfig.LOG_LEVEL,
|
||||
level: config.logLevel || 'info',
|
||||
options: {
|
||||
host: lokiConfig.LOKI_URL || `http://${lokiConfig.LOKI_HOST}:${lokiConfig.LOKI_PORT}`,
|
||||
host: config.lokiHost,
|
||||
labels: {
|
||||
service: serviceName,
|
||||
environment: lokiConfig.LOKI_ENVIRONMENT_LABEL,
|
||||
environment: config.environment || 'development',
|
||||
},
|
||||
ignore: 'childName',
|
||||
...(config.lokiUser && config.lokiPassword ? {
|
||||
basicAuth: {
|
||||
username: config.lokiUser,
|
||||
password: config.lokiPassword,
|
||||
},
|
||||
} : {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -73,27 +97,28 @@ function createTransports(serviceName: string): any {
|
|||
/**
|
||||
* Get or create pino logger
|
||||
*/
|
||||
function getPinoLogger(serviceName: string): pino.Logger {
|
||||
if (!loggerCache.has(serviceName)) {
|
||||
const transport = createTransports(serviceName);
|
||||
function getPinoLogger(serviceName: string, config: LoggerConfig = globalConfig): pino.Logger {
|
||||
const cacheKey = `${serviceName}-${JSON.stringify(config)}`;
|
||||
if (!loggerCache.has(cacheKey)) {
|
||||
const transport = createTransports(serviceName, config);
|
||||
|
||||
const config: pino.LoggerOptions = {
|
||||
level: loggingConfig.LOG_LEVEL,
|
||||
const loggerOptions: pino.LoggerOptions = {
|
||||
level: config.logLevel || 'info',
|
||||
base: {
|
||||
service: serviceName,
|
||||
environment: loggingConfig.LOG_ENVIRONMENT,
|
||||
version: loggingConfig.LOG_SERVICE_VERSION,
|
||||
environment: config.environment || 'development',
|
||||
version: '1.0.0',
|
||||
},
|
||||
};
|
||||
|
||||
if (transport) {
|
||||
config.transport = transport;
|
||||
loggerOptions.transport = transport;
|
||||
}
|
||||
|
||||
loggerCache.set(serviceName, pino(config));
|
||||
loggerCache.set(cacheKey, pino(loggerOptions));
|
||||
}
|
||||
|
||||
return loggerCache.get(serviceName)!;
|
||||
return loggerCache.get(cacheKey)!;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -105,8 +130,8 @@ export class Logger {
|
|||
private serviceName: string;
|
||||
private childName?: string;
|
||||
|
||||
constructor(serviceName: string, context: LogContext = {}) {
|
||||
this.pino = getPinoLogger(serviceName);
|
||||
constructor(serviceName: string, context: LogContext = {}, config?: LoggerConfig) {
|
||||
this.pino = getPinoLogger(serviceName, config);
|
||||
this.context = context;
|
||||
this.serviceName = serviceName;
|
||||
}
|
||||
|
|
@ -232,8 +257,8 @@ export class Logger {
|
|||
/**
|
||||
* Main factory function
|
||||
*/
|
||||
export function getLogger(serviceName: string, context?: LogContext): Logger {
|
||||
return new Logger(serviceName, context);
|
||||
export function getLogger(serviceName: string, context?: LogContext, config?: LoggerConfig): Logger {
|
||||
return new Logger(serviceName, context, config);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -14,3 +14,16 @@ export interface LogContext {
|
|||
export interface LogMetadata {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// Logger configuration
|
||||
export interface LoggerConfig {
|
||||
logLevel?: LogLevel;
|
||||
logConsole?: boolean;
|
||||
logFile?: boolean;
|
||||
logFilePath?: string;
|
||||
logLoki?: boolean;
|
||||
lokiHost?: string;
|
||||
lokiUser?: string;
|
||||
lokiPassword?: string;
|
||||
environment?: string;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue