最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Winston logger names - Stack Overflow

programmeradmin6浏览0评论

Flatiron's logging library Winston is quite useful, but I can not figure how to assign names to loggers. I am expecting an output similar to other logging libraries, for instance:

 [<date>] [<log level>] <logger name> - <the message>

Is it possible to configure Winston in such a way?

Thank you.

Flatiron's logging library Winston is quite useful, but I can not figure how to assign names to loggers. I am expecting an output similar to other logging libraries, for instance:

 [<date>] [<log level>] <logger name> - <the message>

Is it possible to configure Winston in such a way?

Thank you.

Share Improve this question asked Sep 2, 2012 at 17:29 TommyMasonTommyMason 5151 gold badge5 silver badges14 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 12

When creating a logging transport, you can provide a label parameter which will be added to the log output between the log level and the log message. For example:

var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)({
            colorize: true,
            prettyPrint: true,
            timestamp: true,
            label: 'CustomLabel'
        })
    ]
});

This will result in the following output:

2016-09-06T12:16:17.335Z - info: [CustomLabel] hello

My way of doing named loggers with winston

import { LoggerFactory } from "./config/logger";
const logger = LoggerFactory('main');

The important part in that imported config/logger.ts file is

// cache of loggers with different labels
const loggerInstances = new Map<string, winston.Logger>();

const LoggerFactory = (label: string) => {

  let logger = loggerInstances.get(label);
  if (logger != null) {
    return logger;
  }

  // if no logger with that label exists yet      
  // create, cache and return new logger
  logger = winston.createLogger({
    levels,
    format: format(label),
    transports,
  });
  loggerInstances.set(label, logger);
  return logger;
}
export { LoggerFactory }

The important part of the format function looks like this in my solution

const format = (label: string) => {

  const fixedLengthLabelString = fixedLengthString(label, 15);

  return winston.format.combine(
    winston.format.label({ label: fixedLengthLabelString }),
    ...other winston.Format objects...
    winston.format.printf(
      ({ level, message, label, timestamp, stack }) => {
        return `${timestamp} ${trimLevel(6, level)} [${label}] ${message} ${stack ? stack : ""}`
      },
    ),
  )
}

fixedLengthString, trimLevel are just helper functions to let those strings always have the same length

Example output from my logs

2022-10-10 17:23:26:2325   info [PromoAReqHandle] GeoLocationProvider initialized!
2022-10-10 17:23:26:2326  debug [PostgresWrapper] DB connection ok! 
2022-10-10 17:23:56:2356 verbos [PostgresWrapper] [undefined, 241036] remove [idle: 0, waiting: 0, total: 0}] 

You can name loggers; however, there doesn't appear to be a way to add the logger name to the output.

But I would use this too, if it was available. There is an open pull request that would allow this by adding a 'name' to the output string, so hopefully that comes through.

https://github.com/mcclellanmj/SimplyLog goes on the premise of being able to code your own appenders with custom output easily and is centered around named loggers.

发布评论

评论列表(0)

  1. 暂无评论