Как обрабатывать несколько объектов в Winston Logger с помощью подстановки строк? - PullRequest
0 голосов
/ 09 февраля 2019

Я использую winston@3.2.0 / winston-daily-rotate-file@3.6.0 для входа в проект моего узла [v11.6] и настраиваю его какthis:

const appRoot = require('app-root-path');
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, printf, colorize, splat, prettyPrint, label } = format;
require('winston-daily-rotate-file');

const logFormat = printf(({ timestamp, label, level, user, filifu, message }) => {
  let u = (user === undefined ? '' : user);
  let f = (filifu === undefined ? '' : `filifu`);

  return `${timestamp} [${label}] [${level}] [${u}] ${f}: ${message}`;
});

const logDir = `${appRoot}/logs`;
const appName = 'my App';

const options = {
  file: {
    level: 'debug',
    filename: `${logDir}/%DATE%.log`,
    datePattern: 'YYYYMMDD',
    format: combine(
      splat(),
      label({label: appName}),
      timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
      logFormat
    ),
    prepend: true,
    zippedArchive: true,
    json: true,
    handleExceptions: true
  },
  console: {
    level: 'debug',
    format: combine(
      splat(),
      label({label: appName}),
      colorize(),
      timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
      logFormat
    ),
    handleExceptions: true
  }
};

const logger = createLogger({
  transports: [
    new transports.Console(options.console),
    new transports.DailyRotateFile(options.file)
  ],
  exitOnError: false
});

Я использую модифицированную версию модуля magic-globals для расширения регистрации по текущему файлу, строке и функции.Чтобы сэкономить время записи, я добавил следующий код в magic-globals.js:

Object.defineProperty(global, '__filifu', {
  get: function(){
    var file = __stack[1].getFileName().split(/[\\\/]/).slice(-1)[0];
    var line = __stack[1].getLineNumber(); 
    var func = arguments.callee.caller && arguments.callee.caller.name || '(anonymous)';

    return file + ' (' + line + '): ' + func;
  }
});

, чтобы можно было писать «filifu: __filifu» вместо «file: __file, line: __line, func: __func».

В консоли журналы соответствуют аспектам, но в файле журнала я пропускаю «местоположение», если присутствует другой объект:

// works as aspected:
logger.debug('plain text');
// console: 2019-02-09 16:35:22 [my App] [debug] [] []: plain text
//    file: 2019-02-09 16:35:22 [my App] [debug] [] []: plain text

// no problems:
logger.info(`test object without user and filifu %o`, {test: 'test', number: 123});
// console: 2019-02-09 16:35:22 [my App] [info] [] []: test object without user and filifu { test: 'test', number: 123 }
//    file: 2019-02-09 16:35:22 [my App] [info] [] []: test object without user and filifu { test: 'test', number: 123 }

// still no problems:
logger.warn(`a log with user and filifu`, {user: 'testuser', filifu: __filifu});
// console: 2019-02-09 16:35:22 [my App] [warn] [testuser] [index.js (12): myFunction]: a log with user and filifu
//    file: 2019-02-09 16:35:22 [my App] [warn] [testuser] [index.js (12): myFunction]: a log with user and filifu

// using object substitution pattern fails for log file - it is undefined and set to an empty string by 'logFormat':
logger.warn(`test object with user and filifu %o`, {test: 'test 2', number: 456}, {user: 'testuser', filifu: __filifu});
// console: 2019-02-09 16:35:22 [my App] [warn] [testuser] [index.js (13): myFunction]: test object with user and filifu { test: 'test 2', number: 456 }
//    file: 2019-02-09 16:35:22 [my App] [warn] [] []: test object with user and filifu { test: 'test 2', number: 456 }

// same for string ubstitution pattern:
logger.info(`a string: %s`, 'mystring', {user: 'testuser', filifu: __filifu});
// console: 2019-02-09 16:35:22 [my App] [info] [testuser] [index.js (20): myFunction]: a string: mystring
//    file: 2019-02-09 16:35:22 [my App] [info] [] []: a string: mystring

Я обнаружил, что возможный обходной путь будетиспользуя «JSON.stringify» вместо «% o» и литералы шаблонов вместо «% s»:

logger.warn('test object with user and filifu ' + JSON.stringify({test: 'test 2', number: 456}), {user: 'testuser', filifu: __filifu});
// console: 2019-02-09 16:35:22 [my App] [warn] [testuser] [index.js (41): myFunction]: test object with user and filifu {"test":"test 2","number":456}
//    file: 2019-02-09 16:35:22 [my App] [warn] [testuser] [index.js (41): myFunction]: test object with user and filifu {"test":"test 2","number":456}

var myTestString = 'this is my test string';
logger.error(`another string ${myTestString}`, {user: 'testuser', filifu: __filifu});
// console: 2019-02-09 16:35:22 [my App] [error] [testuser] [index.js (22): myFunction]: another string this is my test string
//    file: 2019-02-09 16:35:22 [my App] [error] [testuser] [index.js (22): myFunction]: another string this is my test string

Но на мой взгляд это приводит к большему печатанию и меньшей читаемости.

Можно ли настроить winston (-daily-rotate-file) так, чтобы он записывал пропущенные вещи желаемым образом?

...