本文介绍了Log4js JavaScript创建每日日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个项目nodejs,并使用log4js编写日志.我要在开始新日期时创建新文件日志.
示例:
每天.2017_07_31.log
每天.2017_08_01.log
每天.2017_08_02.log
每天.2017_08_03.log
在Java中,我知道config log4j,但是在不知道有log4js的nodejs中.谢谢大家的帮助:)
I have a project nodejs and use log4js to write log.I want create new file log when start new date.
Example:
daily.2017_07_31.log
daily.2017_08_01.log
daily.2017_08_02.log
daily.2017_08_03.log
In java, I know config log4j but in nodejs with log4js I don't know.Thank every body for your help :)
推荐答案
winston.它非常易于使用.
winston is recommended for nodejs. Its pretty easy to use.
创建一个logger.js文件并进行此配置'
Create a logger.js file and have this configuration '
require('winston-daily-rotate-file');
var winston = require('winston');
winston.loggers.add('logger', {
transports: [
new (winston.transports.Console)(
{
level: config.debugLogLevel,
colorize: true
}),
//new files will be generated each day, the date patter indicates the frequency of creating a file.
new winston.transports.DailyRotateFile({
name: 'debug-log',
filename: '<log file name>',
level: '<level>',
prepend: true,
datePattern: '<pattern>',
maxFiles: <max file>
}
),
new (winston.transports.DailyRotateFile)({
name: 'error-log',
filename: '<log file name>',
level: '<level>',
prepend: true,
datePattern: '<pattern>',
maxFiles: <max file>
})
]
});
var logger = winston.loggers.get('logger');
Object.defineProperty(exports, "LOG", {value: logger});
现在您可以在任何地方使用它
now you can use it anywhere like
var log = require('./logger.js').LOG
log.error('hello');
这篇关于Log4js JavaScript创建每日日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!