0

Хочу выводить логи в консоль и в файл, использую логер log4js, но вылетает ошибка

var log4js = require('log4js');
log4js.configure({
    appenders: [
        {type: 'console'},
        {type: 'file', filename: 'logs/cheese.log', category: 'cheese'}
    ]
});

var logger = log4js.getLogger('cheese');
logger.debug('Тест.');

Ошибка:

\WebstormProjects\node\node_modules\log4js\lib\configuration.js:31
      throw new Error(`Problem with log4js configuration: (${util.inspect(config, { depth: 5 })})`
      ^

Error: Problem with log4js configuration: ({
  appenders: [
    { type: 'console' },
    { type: 'file', filename: 'logs/cheese.log', category: 'cheese' }
  ]
}) - must have a property "appenders" of type object.
    at \WebstormProjects\node\node_modules\log4js\lib\configuration.js:31:13
    at Array.forEach (<anonymous>)
    at Object.throwExceptionIf (\WebstormProjects\node\node_modules\log4js\lib\configuration.js:29:9)
    at \WebstormProjects\node\node_modules\log4js\lib\appenders\index.js:108:17
    at \WebstormProjects\node\node_modules\log4js\lib\configuration.js:46:33
    at Array.forEach (<anonymous>)
    at Object.configure (\WebstormProjects\node\node_modules\log4js\lib\configuration.js:46:13)
    at Object.configure (\WebstormProjects\node\node_modules\log4js\lib\log4js.js:66:17)
    at Object.<anonymous> (\WebstormProjects\node\server.js:11:8)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)

Подскажите в чем проблема? Как настроить логер, чтобы он выводил логи, и в консоль, и в файл

1 ответ 1

0

Проблема решилась:

const log4js = require('log4js');

log4js.configure({
    appenders: {
        out: { type: 'stdout', layout: { type: 'pattern', pattern: '%[[%d{dd.MM.yyyy hh:mm:ss} %p]:%] %m'} },
        everything: { type: 'file', filename: 'logs.log',
            layout: { type: 'pattern', pattern: '[%d{dd.MM.yyyy hh:mm:ss} %p]: %m'}}
    },
    categories: {
        default: { appenders: [ 'out', 'everything' ], level: 'ALL' }
    }
});

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.