优化注释和配置

main
expressgy 1 year ago
parent b53f891c56
commit 06a6ef6a42
  1. 1
      .gitignore
  2. 10
      app.js
  3. 21
      src/winston/index.js

1
.gitignore vendored

@ -6,6 +6,7 @@ yarn-debug.log*
yarn-error.log* yarn-error.log*
pnpm-debug.log* pnpm-debug.log*
lerna-debug.log* lerna-debug.log*
*logs/*
node_modules node_modules
dist dist

@ -18,4 +18,12 @@ winston.info('Hello Wrold')
winston.debug('Hello Wrold') winston.debug('Hello Wrold')
winston.trace('Hello Wrold') winston.trace('Hello Wrold')
console.timeEnd(1) console.timeEnd(1)
const obj = {
name:'Nier',
age:25,
sex:1,
muster:"PAC",
role:'保密者',
premier:'ALL'
}
winston.info(obj)

@ -34,20 +34,20 @@ const myCustomLevels = {
trace: 'white' trace: 'white'
} }
} }
// 创建颜色方法
const colorizer = winston.format.colorize(); const colorizer = winston.format.colorize();
colorizer.addColors(myCustomLevels.levelsColor) colorizer.addColors(myCustomLevels.levelsColor)
const logger = winston.createLogger({ const logger = winston.createLogger({
// colorize: true, // colorize: true,
prettyPrint: true, // prettyPrint: true,
json:true, // json:true,
level: 'trace', // 日志打印等级 level: 'trace', // 日志打印等级
levels: myCustomLevels.levels,// 日志等级列表 levels: myCustomLevels.levels,// 日志等级列表
exitOnError: false,// 如果为false,处理异常不会导致process.exit exitOnError: false,// 如果为false,处理异常不会导致process.exit
silent: false, // 如果为true,所有日志被抑制 silent: false, // 如果为true,所有日志被抑制
format: winston.format.combine( format: winston.format.combine(
winston.format.label({ label: '测试' }),// 自定义标签 // winston.format.label({ label: '测试' }),// 自定义标签
// winston.format.align(),//加了打印不出来对象 // winston.format.align(),//加了打印不出来对象
// winston.format.prettyPrint(), // winston.format.prettyPrint(),
// winston.format.json(), // winston.format.json(),
@ -57,7 +57,7 @@ const logger = winston.createLogger({
const splatList = Array.from(Object.getOwnPropertySymbols(i)) const splatList = Array.from(Object.getOwnPropertySymbols(i))
const splat = splatList && splatList.filter(item => item.toString() === 'Symbol(splat)') const splat = splatList && splatList.filter(item => item.toString() === 'Symbol(splat)')
const splatName = splat.length == 1 ? splat[0] : null const splatName = splat.length == 1 ? splat[0] : null
const data = splatName ? JSON.stringify([i.message, ...i[splatName]]) : i.message const data = splatName ? JSON.stringify([i.message, ...i[splatName]]) : JSON.stringify({message:i.message})
return `${i.timestamp} [${i.level.padEnd(5, ' ').toUpperCase()}] ${data}` return `${i.timestamp} [${i.level.padEnd(5, ' ').toUpperCase()}] ${data}`
// return colorizer.colorize( // return colorizer.colorize(
// i.level, // i.level,
@ -71,9 +71,9 @@ const logger = winston.createLogger({
filename: "winston-logs/%DATE%-INFO--winston.log", filename: "winston-logs/%DATE%-INFO--winston.log",
level: "info", level: "info",
datePattern: "YYYY-MM-DD", datePattern: "YYYY-MM-DD",
zippedArchive: true, zippedArchive: true,// 启用压缩
maxSize: "20m", maxSize: "10m",// 单文件大小
maxFiles: "14d", maxFiles: "30d",// 存储时间
// format: winston.format.combine( // format: winston.format.combine(
// winston.format.printf(i => `${i.timestamp} [${i.level.padEnd(5, ' ').toUpperCase()}] ${i.message}`), // winston.format.printf(i => `${i.timestamp} [${i.level.padEnd(5, ' ').toUpperCase()}] ${i.message}`),
// ) // )
@ -83,8 +83,8 @@ const logger = winston.createLogger({
level: "error", level: "error",
datePattern: "YYYY-MM-DD", datePattern: "YYYY-MM-DD",
zippedArchive: true, zippedArchive: true,
maxSize: "20m", maxSize: "10m",
maxFiles: "30d", maxFiles: "365d",
// format: winston.format.combine( // format: winston.format.combine(
// winston.format.printf(i => `${i.timestamp} [${i.level.padEnd(5, ' ').toUpperCase()}] ${i.message}`), // winston.format.printf(i => `${i.timestamp} [${i.level.padEnd(5, ' ').toUpperCase()}] ${i.message}`),
// ) // )
@ -99,6 +99,7 @@ const logger = winston.createLogger({
new winston.transports.File({ filename: 'winston-logs/rejections.log' }) new winston.transports.File({ filename: 'winston-logs/rejections.log' })
] ]
}); });
// 后续加入记录器,这里可以加入环境变量的判断,当为生产模式时,取消控制台的打印操作。
logger.add(new winston.transports.Console({ logger.add(new winston.transports.Console({
format:winston.format.combine( format:winston.format.combine(
winston.format.printf( winston.format.printf(

Loading…
Cancel
Save