登录

javascript - log4js的使用问题

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;

可是‘123’并没有写进到‘。。、logrecord.log’里面 请问为神马 ?

# Node.js
扔个三星炸死你 扔个三星炸死你 2478 天前 769 次浏览

全部回复(1) 我要回复

  • 高洛峰

    高洛峰2017-07-06 10:36:37

    你定义了log方法来使用log4js,但是你没使用你的log方法啊,
    而且log4js也不是这么记录log的,你看你的log方法里,

    const logger = log4js.getLogger(f_name); 
    logger.info(f_log_msg);
    

    这段才是用来打log的

    如果你的log是单独的模块的话,这么改试试:

    const log = require('./log');
    router.get('/render', async (ctx, next) => {
         log('render','123')
         await ctx.render('index',{title:'wanghao'})
    })

    回复
    0
  • 取消 回复 发送