mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 20:54:53 +00:00
typeorm logger
This commit is contained in:
12
libs/common/src/logger/logger.module.ts
Normal file
12
libs/common/src/logger/logger.module.ts
Normal file
@ -0,0 +1,12 @@
|
||||
// src/common/logger/logger.module.ts
|
||||
import { Module } from '@nestjs/common';
|
||||
import { WinstonModule } from 'nest-winston';
|
||||
import { winstonLoggerOptions } from './services/winston.logger';
|
||||
import { TypeOrmWinstonLogger } from './services/typeorm.logger';
|
||||
|
||||
@Module({
|
||||
imports: [WinstonModule.forRoot(winstonLoggerOptions)],
|
||||
providers: [TypeOrmWinstonLogger],
|
||||
exports: [TypeOrmWinstonLogger],
|
||||
})
|
||||
export class LoggerModule {}
|
||||
64
libs/common/src/logger/services/typeorm.logger.ts
Normal file
64
libs/common/src/logger/services/typeorm.logger.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { Logger as WinstonLogger } from 'winston';
|
||||
import { Logger as TypeOrmLogger } from 'typeorm';
|
||||
import { requestContext } from '@app/common/context/request-context';
|
||||
|
||||
export class TypeOrmWinstonLogger implements TypeOrmLogger {
|
||||
constructor(
|
||||
private readonly logger: WinstonLogger,
|
||||
private readonly slowQueryThreshold = 500, // ms
|
||||
) {}
|
||||
|
||||
logQuery(query: string, parameters?: any[]) {
|
||||
const context = requestContext.getStore();
|
||||
const requestId = context?.requestId ?? 'N/A';
|
||||
const start = Date.now();
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
const duration = Date.now() - start;
|
||||
|
||||
const isSlow = duration > this.slowQueryThreshold;
|
||||
this.logger[isSlow ? 'warn' : 'debug'](`[DB][QUERY] ${query}`, {
|
||||
requestId,
|
||||
parameters,
|
||||
duration: `${duration}ms`,
|
||||
isSlow,
|
||||
});
|
||||
}, 0);
|
||||
|
||||
// Just ensures the setTimeout fires after this function returns
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
|
||||
logQueryError(error: string | Error, query: string, parameters?: any[]) {
|
||||
const requestId = requestContext.getStore()?.requestId ?? 'N/A';
|
||||
this.logger.error(`[DB][ERROR] ${query}`, {
|
||||
requestId,
|
||||
parameters,
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
logQuerySlow(time: number, query: string, parameters?: any[]) {
|
||||
const requestId = requestContext.getStore()?.requestId ?? 'N/A';
|
||||
this.logger.warn(`🔥 [DB][SLOW > ${time}ms] ${query}`, {
|
||||
requestId,
|
||||
parameters,
|
||||
time,
|
||||
});
|
||||
}
|
||||
|
||||
logSchemaBuild(message: string) {
|
||||
this.logger.info(`[DB][SCHEMA] ${message}`);
|
||||
}
|
||||
|
||||
logMigration(message: string) {
|
||||
this.logger.info(`[DB][MIGRATION] ${message}`);
|
||||
}
|
||||
|
||||
log(level: 'log' | 'info' | 'warn', message: any) {
|
||||
this.logger.log({
|
||||
level,
|
||||
message: `[DB] ${message}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
25
libs/common/src/logger/services/winston.logger.ts
Normal file
25
libs/common/src/logger/services/winston.logger.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { utilities as nestWinstonModuleUtilities } from 'nest-winston';
|
||||
import * as winston from 'winston';
|
||||
|
||||
export const winstonLoggerOptions: winston.LoggerOptions = {
|
||||
level: 'debug',
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
nestWinstonModuleUtilities.format.nestLike('MyApp', {
|
||||
prettyPrint: true,
|
||||
}),
|
||||
),
|
||||
}),
|
||||
new winston.transports.File({
|
||||
filename: 'logs/error.log',
|
||||
level: 'error',
|
||||
format: winston.format.json(),
|
||||
}),
|
||||
new winston.transports.File({
|
||||
filename: 'logs/combined.log',
|
||||
format: winston.format.json(),
|
||||
}),
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user