Skip to content

Instantly share code, notes, and snippets.

@lionsole
Created December 26, 2018 01:18
Show Gist options
  • Save lionsole/ff642a70127c07d0f8afea74fcd1bba1 to your computer and use it in GitHub Desktop.
Save lionsole/ff642a70127c07d0f8afea74fcd1bba1 to your computer and use it in GitHub Desktop.
log config
const winston = require('winston')
const path = require('path')
const fs = require('fs-extra')
const { printf, timestamp, combine, label } = winston.format
const myFormat = printf(info => {
return `${info.timestamp} [${info.label}]: ${info.message}`
})
/**
* 日志保存目录
*/
const logpath = path.join(__dirname, '../', 'logs')
fs.ensureDirSync(logpath)
module.exports = winston.createLogger({
format: combine(
label({ label: 'oss' }),
timestamp(),
myFormat
),
transports: [
new winston.transports.Console({
name: 'console',
colorize: true,
level: 'info',
timestamp: true
}),
new winston.transports.File({
name: 'info-file',
filename: path.join(logpath, 'oss-info.log'),
level: 'info',
json: false,
maxsize: 100 * 1024 * 1024, // 100M
maxFiles: 20,
timestamp: function () {
return new Date().toString()
}
}),
new winston.transports.File({
name: 'error-file',
filename: path.join(logpath, 'oss-error.log'),
level: 'error',
handleExceptions: true,
json: false,
maxsize: 100 * 1024 * 1024, // 100M
maxFiles: 50,
timestamp: function () {
return new Date().toString()
}
})
]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment