router.get('/render', async (ctx, next) => {
const log = require('../util/log.js')
log('render','123')
await ctx.render('index',{title:'wanghao'})
})
//../util/log.js
function log(f_name='index',f_log_msg=2){
const log4js = require('log4js');
log4js.configure({
appenders: [
{
type: 'console',
category: "console"
},
{
type: "dateFile",
filename: '../logrecord/log',
pattern: "_yyyyMMdd.log", //日期文件格式
// absolute: false,
alwaysIncludePattern: true,
maxLogSize: 20480,
backups: 3
// category: 'logInfo' //过滤功能
}
],
replaceConsole: true, //替换console.log
levels:{
logInfo: 'info',
console: 'debug'
}
});
console.log(f_name) //render
const logger = log4js.getLogger(f_name);
logger.info(f_log_msg);
}
module.exports=log;
But ‘123’ was not written into ‘. . , logrecord.log’ What is the name of Shenma?
高洛峰2017-07-06 10:36:37
You defined the log method to use log4js, but you did not use your log method,
And log4js does not record logs in this way. Look at your log method,
const logger = log4js.getLogger(f_name);
logger.info(f_log_msg);
This paragraph is for logging
If your log is a separate module, try changing it like this:
const log = require('./log');
router.get('/render', async (ctx, next) => {
log('render','123')
await ctx.render('index',{title:'wanghao'})
})