small log fixes

This commit is contained in:
Boki 2025-06-20 16:53:30 -04:00
parent 62a29259b9
commit afa381e390
4 changed files with 32 additions and 26 deletions

View file

@ -11,8 +11,8 @@ import { getLogger, setLoggerConfig, shutdownLoggers } from '@stock-bot/logger';
import { connectMongoDB } from '@stock-bot/mongodb-client'; import { connectMongoDB } from '@stock-bot/mongodb-client';
import { connectPostgreSQL } from '@stock-bot/postgres-client'; import { connectPostgreSQL } from '@stock-bot/postgres-client';
import { QueueManager, type QueueManagerConfig } from '@stock-bot/queue'; import { QueueManager, type QueueManagerConfig } from '@stock-bot/queue';
import { ProxyManager } from '@stock-bot/utils';
import { Shutdown } from '@stock-bot/shutdown'; import { Shutdown } from '@stock-bot/shutdown';
import { ProxyManager } from '@stock-bot/utils';
// Local imports // Local imports
import { exchangeRoutes, healthRoutes, queueRoutes } from './routes'; import { exchangeRoutes, healthRoutes, queueRoutes } from './routes';
@ -111,7 +111,7 @@ async function initializeServices() {
}, },
enableScheduledJobs: true, enableScheduledJobs: true,
}; };
queueManager = QueueManager.getOrInitialize(queueManagerConfig); queueManager = QueueManager.getOrInitialize(queueManagerConfig);
logger.info('Queue system initialized'); logger.info('Queue system initialized');
@ -127,7 +127,7 @@ async function initializeServices() {
const { initializeProxyProvider } = await import('./providers/proxy.provider'); const { initializeProxyProvider } = await import('./providers/proxy.provider');
const { initializeQMProvider } = await import('./providers/qm.provider'); const { initializeQMProvider } = await import('./providers/qm.provider');
const { initializeWebShareProvider } = await import('./providers/webshare.provider'); const { initializeWebShareProvider } = await import('./providers/webshare.provider');
initializeExchangeSyncProvider(); initializeExchangeSyncProvider();
initializeIBProvider(); initializeIBProvider();
initializeProxyProvider(); initializeProxyProvider();
@ -139,12 +139,12 @@ async function initializeServices() {
logger.debug('Creating scheduled jobs from registered handlers...'); logger.debug('Creating scheduled jobs from registered handlers...');
const { handlerRegistry } = await import('@stock-bot/queue'); const { handlerRegistry } = await import('@stock-bot/queue');
const allHandlers = handlerRegistry.getAllHandlers(); const allHandlers = handlerRegistry.getAllHandlers();
let totalScheduledJobs = 0; let totalScheduledJobs = 0;
for (const [handlerName, config] of allHandlers) { for (const [handlerName, config] of allHandlers) {
if (config.scheduledJobs && config.scheduledJobs.length > 0) { if (config.scheduledJobs && config.scheduledJobs.length > 0) {
const queue = queueManager.getQueue(handlerName); const queue = queueManager.getQueue(handlerName);
for (const scheduledJob of config.scheduledJobs) { for (const scheduledJob of config.scheduledJobs) {
// Include handler and operation info in job data // Include handler and operation info in job data
const jobData = { const jobData = {
@ -152,7 +152,7 @@ async function initializeServices() {
operation: scheduledJob.operation, operation: scheduledJob.operation,
...(scheduledJob.payload || {}), ...(scheduledJob.payload || {}),
}; };
// Build job options from scheduled job config // Build job options from scheduled job config
const jobOptions = { const jobOptions = {
priority: scheduledJob.priority, priority: scheduledJob.priority,
@ -161,7 +161,7 @@ async function initializeServices() {
immediately: scheduledJob.immediately, immediately: scheduledJob.immediately,
}, },
}; };
await queue.addScheduledJob( await queue.addScheduledJob(
scheduledJob.operation, scheduledJob.operation,
jobData, jobData,
@ -174,7 +174,7 @@ async function initializeServices() {
operation: scheduledJob.operation, operation: scheduledJob.operation,
cronPattern: scheduledJob.cronPattern, cronPattern: scheduledJob.cronPattern,
immediately: scheduledJob.immediately, immediately: scheduledJob.immediately,
priority: scheduledJob.priority priority: scheduledJob.priority,
}); });
} }
} }
@ -231,7 +231,7 @@ shutdown.onShutdown(async () => {
try { try {
const { disconnectMongoDB } = await import('@stock-bot/mongodb-client'); const { disconnectMongoDB } = await import('@stock-bot/mongodb-client');
const { disconnectPostgreSQL } = await import('@stock-bot/postgres-client'); const { disconnectPostgreSQL } = await import('@stock-bot/postgres-client');
await disconnectMongoDB(); await disconnectMongoDB();
await disconnectPostgreSQL(); await disconnectPostgreSQL();
logger.info('Database connections closed'); logger.info('Database connections closed');
@ -243,7 +243,7 @@ shutdown.onShutdown(async () => {
shutdown.onShutdown(async () => { shutdown.onShutdown(async () => {
try { try {
await shutdownLoggers(); await shutdownLoggers();
process.stdout.write('Data service loggers shut down\n'); // process.stdout.write('Data service loggers shut down\n');
} catch (error) { } catch (error) {
process.stderr.write(`Error shutting down loggers: ${error}\n`); process.stderr.write(`Error shutting down loggers: ${error}\n`);
} }

View file

@ -169,7 +169,7 @@ shutdown.onShutdown(async () => {
shutdown.onShutdown(async () => { shutdown.onShutdown(async () => {
try { try {
await shutdownLoggers(); await shutdownLoggers();
process.stdout.write('Data sync service loggers shut down\n'); // process.stdout.write('Data sync service loggers shut down\n');
} catch (error) { } catch (error) {
process.stderr.write(`Error shutting down loggers: ${error}\n`); process.stderr.write(`Error shutting down loggers: ${error}\n`);
} }

View file

@ -8,9 +8,9 @@ import { getLogger, setLoggerConfig, shutdownLoggers } from '@stock-bot/logger';
import { createAndConnectMongoDBClient, MongoDBClient } from '@stock-bot/mongodb-client'; import { createAndConnectMongoDBClient, MongoDBClient } from '@stock-bot/mongodb-client';
import { createAndConnectPostgreSQLClient, PostgreSQLClient } from '@stock-bot/postgres-client'; import { createAndConnectPostgreSQLClient, PostgreSQLClient } from '@stock-bot/postgres-client';
import { Shutdown } from '@stock-bot/shutdown'; import { Shutdown } from '@stock-bot/shutdown';
// Import routes
import { exchangeRoutes } from './routes/exchange.routes'; import { exchangeRoutes } from './routes/exchange.routes';
import { healthRoutes } from './routes/health.routes'; import { healthRoutes } from './routes/health.routes';
// Import routes
import { setMongoDBClient, setPostgreSQLClient } from './clients'; import { setMongoDBClient, setPostgreSQLClient } from './clients';
// Initialize configuration with automatic monorepo config inheritance // Initialize configuration with automatic monorepo config inheritance
@ -160,7 +160,7 @@ shutdown.onShutdown(async () => {
shutdown.onShutdown(async () => { shutdown.onShutdown(async () => {
try { try {
await shutdownLoggers(); await shutdownLoggers();
process.stdout.write('Web API loggers shut down\n'); // process.stdout.write('Web API loggers shut down\n');
} catch (error) { } catch (error) {
process.stderr.write(`Error shutting down loggers: ${error}\n`); process.stderr.write(`Error shutting down loggers: ${error}\n`);
} }

View file

@ -9,14 +9,14 @@
*/ */
import pino from 'pino'; import pino from 'pino';
import type { LogContext, LogLevel, LogMetadata, LoggerConfig } from './types'; import type { LogContext, LoggerConfig, LogLevel, LogMetadata } from './types';
// Simple cache for logger instances // Simple cache for logger instances
const loggerCache = new Map<string, pino.Logger>(); const loggerCache = new Map<string, pino.Logger>();
// Global config that can be set // Global config that can be set
let globalConfig: LoggerConfig = { let globalConfig: LoggerConfig = {
logLevel: 'info', // Default to info, but trace and fatal are supported logLevel: 'info', // Default to info, but trace and fatal are supported
logConsole: true, logConsole: true,
logFile: false, logFile: false,
logFilePath: './logs', logFilePath: './logs',
@ -39,7 +39,7 @@ export function setLoggerConfig(config: LoggerConfig): void {
*/ */
function createTransports(serviceName: string, config: LoggerConfig = globalConfig): any { function createTransports(serviceName: string, config: LoggerConfig = globalConfig): any {
const targets: any[] = []; const targets: any[] = [];
// Console transport // Console transport
if (config.logConsole) { if (config.logConsole) {
targets.push({ targets.push({
@ -82,12 +82,14 @@ function createTransports(serviceName: string, config: LoggerConfig = globalConf
environment: config.environment || 'development', environment: config.environment || 'development',
}, },
ignore: 'childName', ignore: 'childName',
...(config.lokiUser && config.lokiPassword ? { ...(config.lokiUser && config.lokiPassword
basicAuth: { ? {
username: config.lokiUser, basicAuth: {
password: config.lokiPassword, username: config.lokiUser,
}, password: config.lokiPassword,
} : {}), },
}
: {}),
}, },
}); });
} }
@ -294,7 +296,11 @@ export class Logger {
/** /**
* Main factory function * Main factory function
*/ */
export function getLogger(serviceName: string, context?: LogContext, config?: LoggerConfig): Logger { export function getLogger(
serviceName: string,
context?: LogContext,
config?: LoggerConfig
): Logger {
return new Logger(serviceName, context, config); return new Logger(serviceName, context, config);
} }
@ -321,8 +327,8 @@ export async function shutdownLoggers(): Promise<void> {
try { try {
await Promise.allSettled(flushPromises); await Promise.allSettled(flushPromises);
// eslint-disable-next-line no-console
console.log('All loggers flushed successfully'); // 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 flush failed:', error);
@ -332,4 +338,4 @@ export async function shutdownLoggers(): Promise<void> {
} }
// Export types for convenience // Export types for convenience
export type { LogLevel, LogContext, LogMetadata } from './types'; export type { LogContext, LogLevel, LogMetadata } from './types';