增加丢失日志文件
This commit is contained in:
parent
6944538fa2
commit
636c7de658
|
@ -33,7 +33,6 @@ export const getConfig = () => {
|
||||||
// }
|
// }
|
||||||
// 融合注入参数
|
// 融合注入参数
|
||||||
config = Object.assign(config, process.env);
|
config = Object.assign(config, process.env);
|
||||||
console.log(config)
|
|
||||||
configAll = { ...config, isExist: true }
|
configAll = { ...config, isExist: true }
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
|
79
src/utils/logger.ts
Normal file
79
src/utils/logger.ts
Normal 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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user