【问题标题】:log4js is not working in node , How to configure it...?log4js 在节点中不起作用,如何配置它...?
【发布时间】:2020-05-23 13:35:58
【问题描述】:

我已经安装了 log4js npm 并尝试将记录器实现到我的节点 api... 我第一次尝试使用 log4js。 代码:

var log4js = require('log4js'); // include log4js

log4js.configure({ // configure to use all types in different files.
    appenders: [
        {   type: 'file',
            filename: "/logs/error.log", // specify the path where u want logs folder error.log
            category: 'error',
            maxLogSize: 20480,
            backups: 10
        },
        {   type: "file",
            filename: "/logs/info.log", // specify the path where u want logs folder info.log
            category: 'info',
            maxLogSize: 20480,
            backups: 10
        },
        {   type: 'file',
            filename: "/logs/debug.log", // specify the path where u want logs folder debug.log
            category: 'debug',
            maxLogSize: 20480,
            backups: 10
        }
    ]
});

var loggerinfo = log4js.getLogger('info'); // initialize the var to use.
var loggererror = log4js.getLogger('error'); // initialize the var to use.
var loggerdebug = log4js.getLogger('debug'); // initialize the var to use.

loggerinfo.info('This is Information Logger');
loggererror.info('This is Error Logger');
loggerdebug.info('This is Debugger');

我收到错误:

****throw new Error(`Problem with log4js configuration: (${util.inspect(config, { depth: 5 })})`
      ^
Error: Problem with log4js configuration: ({ appenders: 
   [ { type: 'file',
       filename: '/logs/error.log',
       category: 'error',
       maxLogSize: 20480,
       backups: 10 },
     { type: 'file',
       filename: '/logs/info.log',
       category: 'info',
       maxLogSize: 20480,
       backups: 10 },
     { type: 'file',
       filename: '/logs/debug.log',
       category: 'debug',
       maxLogSize: 20480,
       backups: 10 } ] }) - must have a property "appenders" of type object.****

我已经在我的应用程序中使用 cmd node log4js.js(这个文件名)运行了一个 log4js 示例代码 请帮帮我...

【问题讨论】:

    标签: node.js json logging log4js-node


    【解决方案1】:

    我有这个。

    文件名:myHelperlog4js.js

    const { configure, getLogger } = require("log4js");
    configure({
     appenders: {
         console: { type: 'console' ,"layout": { "type": "pattern", "pattern": "%d - %c:[%p]: %m" }},
         fileLog: { type: 'file', filename: '/logs/application.log' }
       },
       categories: {
         default: { appenders: ['console','fileLog'], level: 'trace' }, 
       }
     });
    
    const logger = getLogger();
    module.exports = {logger};
    

    在你要使用的文件中,有这个:

    const {logger } = require('MY PAHT OF MY JS CONFIGURED/myHelperlog4js');
    

    仅供参考:

     logger.info(msg);
     logger.info(msg);
     logger.debug(msg);
     logger.info(msg);
     logger.warn(msg);
     logger.error(msg);
     logger.fatal(msg);
     logger.info(msg); 
    
     
    

    /logs/application.log:保存登录:

    • Windows (C:/logs/)
    • linux (/logs/)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 2018-05-17
      • 2013-10-27
      • 2018-12-19
      • 2021-01-31
      • 1970-01-01
      相关资源
      最近更新 更多