chore: 更新.gitignore并添加新文件
在.gitignore中添加对*.png、*.log、*-lock.json、*.woff2文件的忽略规则,并新增OFL.txt文件。同时,添加vue.svg图标文件以支持前端展示。更新多个TypeScript文件以优化代码格式和增强可读性。
This commit is contained in:
@@ -1,6 +1,21 @@
|
||||
import { Injectable, LoggerService, Scope } from '@nestjs/common';
|
||||
import { winstonLogger } from './winston.config';
|
||||
|
||||
type LogMessage = string | Error | Record<string, unknown>;
|
||||
|
||||
function formatMessage(message: LogMessage): string {
|
||||
if (typeof message === 'string') {
|
||||
return message;
|
||||
}
|
||||
if (message instanceof Error) {
|
||||
return message.message;
|
||||
}
|
||||
if (typeof message === 'object' && message !== null) {
|
||||
return JSON.stringify(message);
|
||||
}
|
||||
return String(message);
|
||||
}
|
||||
|
||||
@Injectable({ scope: Scope.TRANSIENT })
|
||||
export class CustomLogger implements LoggerService {
|
||||
private context?: string;
|
||||
@@ -9,23 +24,34 @@ export class CustomLogger implements LoggerService {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
log(message: any, context?: string) {
|
||||
winstonLogger.info(message, { context: context || this.context });
|
||||
log(message: LogMessage, context?: string) {
|
||||
winstonLogger.info(formatMessage(message), {
|
||||
context: context || this.context,
|
||||
});
|
||||
}
|
||||
|
||||
error(message: any, trace?: string, context?: string) {
|
||||
winstonLogger.error(message, { context: context || this.context, trace });
|
||||
error(message: LogMessage, trace?: string, context?: string) {
|
||||
winstonLogger.error(formatMessage(message), {
|
||||
context: context || this.context,
|
||||
trace,
|
||||
});
|
||||
}
|
||||
|
||||
warn(message: any, context?: string) {
|
||||
winstonLogger.warn(message, { context: context || this.context });
|
||||
warn(message: LogMessage, context?: string) {
|
||||
winstonLogger.warn(formatMessage(message), {
|
||||
context: context || this.context,
|
||||
});
|
||||
}
|
||||
|
||||
debug(message: any, context?: string) {
|
||||
winstonLogger.debug(message, { context: context || this.context });
|
||||
debug(message: LogMessage, context?: string) {
|
||||
winstonLogger.debug(formatMessage(message), {
|
||||
context: context || this.context,
|
||||
});
|
||||
}
|
||||
|
||||
verbose(message: any, context?: string) {
|
||||
winstonLogger.verbose(message, { context: context || this.context });
|
||||
verbose(message: LogMessage, context?: string) {
|
||||
winstonLogger.verbose(formatMessage(message), {
|
||||
context: context || this.context,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,13 +16,33 @@ const logFormat = winston.format.combine(
|
||||
winston.format.errors({ stack: true }),
|
||||
winston.format.splat(),
|
||||
winston.format.printf(({ timestamp, level, message, context, stack }) => {
|
||||
let log = `${timestamp} [${level}]`;
|
||||
if (context) {
|
||||
log += ` [${context}]`;
|
||||
}
|
||||
log += ` ${message}`;
|
||||
const timestampStr =
|
||||
typeof timestamp === 'string' ? timestamp : String(timestamp);
|
||||
const levelStr = typeof level === 'string' ? level : String(level);
|
||||
const messageStr = typeof message === 'string' ? message : String(message);
|
||||
const contextStr = context
|
||||
? typeof context === 'string'
|
||||
? context
|
||||
: JSON.stringify(context)
|
||||
: '';
|
||||
let stackStr = '';
|
||||
if (stack) {
|
||||
log += `\n${stack}`;
|
||||
if (typeof stack === 'string') {
|
||||
stackStr = stack;
|
||||
} else if (typeof stack === 'object' && stack !== null) {
|
||||
stackStr = JSON.stringify(stack);
|
||||
} else {
|
||||
stackStr = String(stack);
|
||||
}
|
||||
}
|
||||
|
||||
let log = `${timestampStr} [${levelStr}]`;
|
||||
if (contextStr) {
|
||||
log += ` [${contextStr}]`;
|
||||
}
|
||||
log += ` ${messageStr}`;
|
||||
if (stackStr) {
|
||||
log += `\n${stackStr}`;
|
||||
}
|
||||
return log;
|
||||
}),
|
||||
@@ -30,10 +50,7 @@ const logFormat = winston.format.combine(
|
||||
|
||||
// 控制台传输
|
||||
const consoleTransport = new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.colorize(),
|
||||
logFormat,
|
||||
),
|
||||
format: winston.format.combine(winston.format.colorize(), logFormat),
|
||||
});
|
||||
|
||||
// 应用日志传输(按天轮转)
|
||||
@@ -61,10 +78,6 @@ const errorLogTransport = new DailyRotateFile({
|
||||
export const winstonLogger = winston.createLogger({
|
||||
level: process.env.LOG_LEVEL || 'info',
|
||||
format: logFormat,
|
||||
transports: [
|
||||
consoleTransport,
|
||||
appLogTransport,
|
||||
errorLogTransport,
|
||||
],
|
||||
transports: [consoleTransport, appLogTransport, errorLogTransport],
|
||||
exitOnError: false,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user