Node.js: Winston: Can I add default meta data to all log messages

I’m using Winston
in Node.js
for logging. I know I can add metadata individually to each log message but is there a way to specify a default set of metadata that’ll be added to every log message (such as the app name) as I don’t want to specify it every time I need to send a log message.

Problem courtesy of: Sean Bannister


There’s no built-in way to do this, but you can definitely add it yourself – here’s how:

First, set up your logger like you normally would. For example:

var logger = new (winston.Logger)({
            "exitOnError" : true,
            "transports" : [
                new (winston.transports.Console)({ "colorize" : true, "level" : "silly", "silent" : false, "handleExceptions" : false }),

Then override the log() method (this is always called by the level methods –
actually calls logger.log('foo')

logger.log = function(){
  var args = arguments;
  if(args[2]) args[3] = args[2];
  args[2] = {
    "foo" : "bar"

All I’m doing above is making it so when logger.log()
is called, it instead calls the above method, which adds the metadata (in this case, an object containing a foo
key). Then it calls winston’s Logger.log method from the proper context.

The above code would be in a module you create, at the bottom just export the logger:

module.exports = logger;

and import the logger module instead of the winston module in your sub classes.

var logger = require('./logger.js');

Hope that helps!

Solution courtesy of: Jesse


I’ve found a better way using util-extend
based on what I read on this blog
. It will append the data in all cases, which I found useful for catching vs logger.log(“info”, message) and won’t overwrite your other arguments.

logger.log = function(){
   var args = arguments;
   var level = args[0];

   var newArgs = {
        foo: "bar",
        baz: "abc"
   var originalMeta = args[2] || {};
   args[2] = extend(originalMeta, newArgs);


Will output in the console and in your logs.

Discussion courtesy of: harryBundles

Another option with an output more like log4j(s):

There is a (currently undocumented) ‘label’ property in the Console transport which will add a label to the output (json or line):

var _ = require('lodash');
var winston = require('winston');
var path = require('path');
var baseDir = path.resolve(__dirname, '..');

var container = new winston.Container();
container.add("exception", {
    console: {
        handleExceptions: true,
        timestamp: true,
        label: "EXCEPTION",
        colorize: true
container.get("exception").exitOnError = false;
var keys = [];

module.exports = function(filename) {
    var label = path.relative(baseDir, filename);
    if (!_.contains(keys, label)) {
        container.add(label, {
            console: {
                handleExceptions: false,
                level: 'debug',
                timestamp: true,
                label: label,
                colorize: true
    var logger = container.get(label);
    logger.exitOnError = false;
    return logger;

And in the other modules require it like this:

var logger = require('./logger')(__filename);

Example output:

2014-07-23T07:05:27.770Z - info: [config/config.js] .......

Discussion courtesy of: Risadinha

There is now

that are the documented and official way to do this:

logger.rewriters.push(function(level, msg, meta) { = 'myApp';

  return meta;

Will add a property app
to every metadata going throught this logger.

You can also declare it when building the logger:

new (winston.Logger)({
        level: config.log[file].level,
        rewriters: [
            (level, msg, meta) => {
       = 'myApp';
                return meta;
        transports: [
            /*your transports*/

Discussion courtesy of: DrakaSAN

This recipe can be found in it’s original form on Stack Over Flow

稿源:Node.js Recipes (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Node.js: Winston: Can I add default meta data to all log messages

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录