search

Home  >  Q&A  >  body text

javascript - Problems using 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;

But ‘123’ was not written into ‘. . , logrecord.log’ What is the name of Shenma?

扔个三星炸死你扔个三星炸死你2782 days ago945

reply all(1)I'll reply

  • 高洛峰

    高洛峰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'})
    })

    reply
    0
  • Cancelreply