增加丢失日志文件

This commit is contained in:
wuhan 2024-04-17 16:13:22 +08:00
parent 6944538fa2
commit 636c7de658
2 changed files with 79 additions and 1 deletions

View File

@ -33,7 +33,6 @@ export const getConfig = () => {
// }
// 融合注入参数
config = Object.assign(config, process.env);
console.log(config)
configAll = { ...config, isExist: true }
return config;
};

79
src/utils/logger.ts Normal file
View File

@ -0,0 +1,79 @@
import { Logger, createLogger, format, transports } from 'winston';
import 'winston-daily-rotate-file';
const customFormat = format.combine(
format.timestamp({ format: 'MMM-DD-YYYY HH:mm:ss' }),
format.align(),
format.printf((i) => `${i.level}: ${[i.timestamp]}: ${i.message}`),
);
const defaultOptions: any = {
format: customFormat,
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '5m',
maxFiles: '14d',
};
export default class AppLogger {
private context?: string;
private logger: Logger;
public setContext(context: string): void {
this.context = context;
}
constructor() {
this.logger = createLogger({
format: customFormat,
transports: [
// new transports.Console(),
new transports.DailyRotateFile({
filename: 'logs/info-%DATE%.log',
level: 'info',
...defaultOptions,
}),
new transports.DailyRotateFile({
filename: 'logs/error-%DATE%.log',
level: 'error',
...defaultOptions,
}),
],
});
}
error(ctx: any, message: string, meta?: Record<string, any>): Logger {
return this.logger.error({
message: `${this.context}:${message}`,
contextName: this.context,
ctx,
...meta,
});
}
warn(ctx: any, message: string, meta?: Record<string, any>): Logger {
return this.logger.warn({
message: `${this.context}:${message}`,
contextName: this.context,
ctx,
...meta,
});
}
debug(ctx: any, message: string, meta?: Record<string, any>): Logger {
return this.logger.debug({
message: `${this.context}:${message}`,
contextName: this.context,
ctx,
...meta,
});
}
info(ctx: any, message: string, meta?: Record<string, any>): Logger {
return this.logger.info({
message: `${this.context}:${message}`,
contextName: this.context,
ctx,
...meta,
});
}
}