pino testing
This commit is contained in:
parent
0497541a47
commit
18c2720fe8
2 changed files with 41 additions and 23 deletions
|
|
@ -240,11 +240,12 @@ shutdown.onShutdown(async () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Logger shutdown - registered last so it runs after all other shutdown operations
|
||||||
shutdown.onShutdown(async () => {
|
shutdown.onShutdown(async () => {
|
||||||
try {
|
try {
|
||||||
logger.info('Shutting down loggers...');
|
logger.info('Shutting down loggers...');
|
||||||
await shutdownLoggers();
|
await shutdownLoggers();
|
||||||
logger.info('Loggers shut down successfully');
|
// Don't log after shutdown
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Silently ignore logger shutdown errors
|
// Silently ignore logger shutdown errors
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,6 @@ function createTransports(serviceName: string, config: LoggerConfig = globalConf
|
||||||
ignore: 'pid,hostname,service,environment,version,childName',
|
ignore: 'pid,hostname,service,environment,version,childName',
|
||||||
errorLikeObjectKeys: ['err', 'error'],
|
errorLikeObjectKeys: ['err', 'error'],
|
||||||
errorProps: 'message,stack,name,code',
|
errorProps: 'message,stack,name,code',
|
||||||
sync: false,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -119,7 +118,8 @@ function getPinoLogger(serviceName: string, config: LoggerConfig = globalConfig)
|
||||||
loggerOptions.transport = transport;
|
loggerOptions.transport = transport;
|
||||||
}
|
}
|
||||||
|
|
||||||
loggerCache.set(cacheKey, pino(loggerOptions));
|
const logger = pino(loggerOptions);
|
||||||
|
loggerCache.set(cacheKey, logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
const logger = loggerCache.get(cacheKey);
|
const logger = loggerCache.get(cacheKey);
|
||||||
|
|
@ -307,13 +307,28 @@ export function getLogger(
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gracefully shutdown all logger instances
|
* Gracefully shutdown all logger instances
|
||||||
* This should be called during application shutdown to ensure all logs are flushed
|
* This ensures all transports are flushed and closed properly
|
||||||
*/
|
*/
|
||||||
export async function shutdownLoggers(): Promise<void> {
|
export async function shutdownLoggers(): Promise<void> {
|
||||||
|
try {
|
||||||
|
// Log final message before shutdown
|
||||||
|
for (const logger of loggerCache.values()) {
|
||||||
|
logger.info('Logger shutting down...');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flush all loggers
|
||||||
const flushPromises = Array.from(loggerCache.values()).map(logger => {
|
const flushPromises = Array.from(loggerCache.values()).map(logger => {
|
||||||
return new Promise<void>(resolve => {
|
return new Promise<void>((resolve) => {
|
||||||
|
// First try to flush if the method exists
|
||||||
if (typeof logger.flush === 'function') {
|
if (typeof logger.flush === 'function') {
|
||||||
logger.flush(err => {
|
const timeout = setTimeout(() => {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.warn('Logger flush timeout after 100ms');
|
||||||
|
resolve();
|
||||||
|
}, 100);
|
||||||
|
|
||||||
|
logger.flush((err) => {
|
||||||
|
clearTimeout(timeout);
|
||||||
if (err) {
|
if (err) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error('Logger flush error:', err);
|
console.error('Logger flush error:', err);
|
||||||
|
|
@ -326,13 +341,15 @@ export async function shutdownLoggers(): Promise<void> {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
await Promise.all(flushPromises);
|
||||||
await Promise.allSettled(flushPromises);
|
|
||||||
|
// Give transports time to finish writing
|
||||||
|
// This is especially important for file and network transports
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||||||
|
|
||||||
// console.log('All loggers flushed successfully');
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error('Logger flush failed:', error);
|
console.error('Logger shutdown failed:', error);
|
||||||
} finally {
|
} finally {
|
||||||
loggerCache.clear();
|
loggerCache.clear();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue