Winston Log: Restrict transport to only 1 level

 Target specific only specific log levels · Issue #1108 · winstonjs/winston (github.com)

winstonjs/winston: A logger for just about everything. (github.com)

This kind of feature request is exactly why format was introduced in winston@3. You can define a format for this for each of the transports:

const { createLogger, format } = require('winston');
const LEVEL = Symbol.for('level');

/**
 * Log only the messages the match `level`.
 */
function filterOnly(level) {
  return format(function (info) {
    if (info[LEVEL] === level) {
      return info;
    }
  })();
}

const logger = createLogger({
  format: zformat,
  transports: [
    // only 'info' level
    new transprts.File({
      level: 'info',
      format: filterOnly('info'),
      filename: 'logs/access.log',
    }),
    // any level below 'verbose'
    new transports.File({
      level: 'verbose',
      format: filterOnly('verbose'),
      filename: 'logs/combined.log',
    })
  ]
});

Filtering info Objects

If you wish to filter out a given info Object completely when logging then simply return a falsey value.

const { createLogger, format, transports } = require('winston');

// Ignore log messages if they have { private: true }
const ignorePrivate = format((info, opts) => {
  if (info.private) { return false; }
  return info;
});

const logger = createLogger({
  format: format.combine(
    ignorePrivate(),
    format.json()
  ),
  transports: [new transports.Console()]
});

// Outputs: {"level":"error","message":"Public error to share"}
logger.log({
  level: 'error',
  message: 'Public error to share'
});

// Messages with { private: true } will not be written when logged.
logger.log({
  private: true,
  level: 'error',
  message: 'This is super secret - hide it.'
});

Use of format.combine will respect any falsey values return and stop evaluation of later formats in the series. For example:

const { format } = require('winston');
const { combine, timestamp, label } = format;

const willNeverThrow = format.combine(
  format(info => { return false })(), // Ignores everything
  format(info => { throw new Error('Never reached') })()
);
const { createLoggerformattransports } = require('winston');
const LEVEL = Symbol.for('level');

/**
 * Log only the messages the match `level`.
 */
 function filterOnly(level) {
  return format(function (info) {
    if (info[LEVEL] === level) {
      return info;
    }
  })();
}

const logger = createLogger({
  level: 'info',
  format: format.json(),
  defaultMeta: { service: 'user-service' },
  transports: [
    //
    // - Write all logs with level `error` and below to `error.log`
    // - Write all logs with level `info` and below to `combined.log`
    //
    new transports.File({ filename: 'logs/error.log'level: 'error' }),
    new transports.Console({ 
      level: 'debug',
      format: format.combine(
        format.colorize(),
        filterOnly('debug')
      )
     }),
  ],
});


// const levels = {
//   error: 0,
//   warn: 1,
//   info: 2,
//   verbose: 3,
//   debug: 4,
//   silly: 5
// };

logger.log('error'"127.0.0.1 - there's no place like home");

logger.log('warn'"127.0.0.1 - there's no place like home");

logger.log('info'"127.0.0.1 - there's no place like home");

logger.log('verbose'"127.0.0.1 - there's no place like home");

logger.log('debug'"127.0.0.1 - there's no place like home");

logger.log('silly'"127.0.0.1 - there's no place like home");


Không có nhận xét nào:

Is it okay to use both fetchpriority="high" and loading="eager" in img tag?

 https://stackoverflow.com/questions/77744344/is-it-okay-to-use-both-fetchpriority-high-and-loading-eager-in-img-tag Yes Fetchpriority and l...