31

I use Winston for my backend logging I cannot log the object without using JSON.stringify which is annoying

logger.debug(`Register ${JSON.stringify(req.body)}`)
const logger: Logger = createLogger({
    // change level if in dev environment versus production
    level: env === 'production' ? 'info' : 'debug',
    format: format.combine(
        format.label({label: path.basename(process.mainModule.filename)}),
        format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
        format.prettyPrint()
    ),
    transports: [
        new transports.Console({
            format: format.combine(format.colorize(), logFormat),
        }),
        new transports.File({
            filename,
            format: format.combine(format.json()),
        }),
    ],
    exitOnError: false,
})

Could you show me the way to log object with Winston. I am using version 3.2.1

7 Answers 7

59

You are trying to insert a JSON object directly into the string, so it will print [Object Object] without the JSON.stringify.

This is not fixable by configuring Winston, as this problem happens while the string is generated (before the logger.debug function actually reads it), so a console.log call would print the same thing.

The first parameter of the logger.* functions is the message (string), then you can pass a metadata object (JSON).

To use the metadata in your logFormat function, update your Logger instantiation as follow:

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

const logFormat = format.printf(info => `${info.timestamp} ${info.level} [${info.label}]: ${info.message}`)

const logger = winston.createLogger({
  level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
  format: format.combine(
    format.label({ label: path.basename(process.mainModule.filename) }),
    format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    // Format the metadata object
    format.metadata({ fillExcept: ['message', 'level', 'timestamp', 'label'] })
  ),
  transports: [
    new transports.Console({
      format: format.combine(
        format.colorize(),
        logFormat
      )
    }),
    new transports.File({
      filename: 'logs/combined.log',
      format: format.combine(
        // Render in one line in your log file.
        // If you use prettyPrint() here it will be really
        // difficult to exploit your logs files afterwards.
        format.json()
      )
    })
  ],
  exitOnError: false
})

Usage:

const req = {
  body: {
    name: 'Daniel Duuch',
    email: '[email protected]',
    password: 'myGreatPassword'
  }
}

logger.debug(`Register ${req.body.name} with email ${req.body.email}`, { ...req.body, action: 'register' })

Console output:

2019-05-11 17:05:45 debug [index.js]: Register Daniel Duuch with email [email protected]

Logfile output (prettified by hand, see comment in the transport file format):

{
  message: 'Register Daniel Duuch with email [email protected]',
  level: 'debug',
  timestamp: '2019-05-11 17:05:45',
  label: 'index.js',
  metadata: {
    name: 'Daniel Duuch',
    email: '[email protected]',
    password: 'myGreatPassword',
    action: 'register'
  }
}

Hope this solves your issue.

Important note: as noted by @Xetera in the comments, "you should make sure you're not actually logging people's passwords anywhere"

Code for this answer

1
  • 9
    I know the code is just an example but if you ever do this you should make sure you're not actually logging people's passwords anywhere.
    – Xetera
    Dec 28, 2019 at 23:39
22

You can use format.splat() in your logger config:

const logger = createLogger({
    format: combine(
        ...
        format.splat(), // <--
        ...
    ),
    ...
});

...and log object using string interpolation:

let myObj = { /* ... */ };
logger.info('This message will include a complete object: %O', myObj);
7
  • Thanks. By the way, do you know how can I log the filename as label. Since I use import logger from '../../utils/logger', I don't know how to do it stackoverflow.com/questions/53655740/…
    –  coinhndp
    May 12, 2019 at 10:36
  • Interesting since I use typescript and use babel7 to transform ts files. All my module and error stacktrace points to dist/*.js files
    –  coinhndp
    May 12, 2019 at 10:46
  • 1
    @anton-pastukhov @coinhndp I'm sorry but this is a bad practice. I suggested the OP to use the metadata param for a reason, which is that if you ever need to use your log in monitoring / analytics system, it is preferable to have a JSON object directly in the logs, instead of searching datas through strings. And if the logs are to be red by a human ops, then it is better to parse that object into a readable string. This is why my solution gives a console output optimized for human ops readability and a logfile output optimized for data treatment. Please do not encourage bad practices. May 12, 2019 at 12:13
  • 1
    Thank. Got your idea. I just use that on debug level and the logs to files I use Json. Agree with you that I should use metadata. Do you have experience in React, have lots of questions to ask @SherloxFR
    –  coinhndp
    May 13, 2019 at 6:50
  • @coinhndp I'm aware that you got my point, I'm just concerned about the next people that will come to your question and just do the wrong thing in production grade work ^^ yeah I'm experienced with React, I saw you joined the Discord I'll help you there May 13, 2019 at 7:03
16

My solution was to use this kind of formatter:

const { format } = winston
const consoleFormat = format.combine(
  format.prettyPrint(),
  format.splat(),
  format.printf((info) => {
    if (typeof info.message === 'object') {
      info.message = JSON.stringify(info.message, null, 3)
    }

    return info.message
  })
)

now all those options works as expected:

logger.info('plain text')
logger.info('plain text with object %o', { a:1, b: 2} )
logger.info({ a:1, b: 2 })
1
  • 1
    The printf example here is a great way to handle the situation where you have passed your logger into a 3rd party package that is trying to log objects, since you have no control over what is passed in as input.
    – stefan2718
    Jun 8 at 16:48
6

I had to combine the solution provided by @SherloxFR and @Anton.

const Winston = require('winston');
const { format } = Winston;

const options = {
    file: {
        ....
        format: format.combine(
            format.splat(), 
            format.json()
        ),
        ...
    },
    console: {
        ...
        format: format.combine(
            format.splat(),
            format.json()
        ),
        ...
    }
};

You can see that I added both format.splat() and format.json() to the options config in the above code.

const logger = new Winston.createLogger({
    transports: [
        new Winston.transports.File(options.file),
        new Winston.transports.Console(options.console)
    ],
    exitOnError: false // do not exit on handled exceptions
});

That is how I used the options config object. You can actually write the format code inside the transports array but I don't like it that way. It's your choice anyway.

After the configuration like that, the is how I used it in my code

let myObj = {
   name: "StackOverflow",
};

logger.info('Content: %o', myObj);

You can also spread it like this if you want

logger.info('Content: %o', {...myObj});

That's all. Winston should log your object with this set up.

3

If you want the object to be logged to the console and to the file, here is what you can do:

1.Initialize 2 formats. One for file and the other for the console. Notice the JSON.stringify method used in the consoleFormat

const winston = require("winston");
const { format, transports, createLogger } = winston;
const path = require("path");
const consoleloggerLevel = process.env.WINSTON_LOGGER_LEVEL || "info";

const consoleFormat = format.combine(
  format.colorize(),
  format.timestamp(),
  format.align(),
  format.printf((info) => {
    return `${info.timestamp} - ${info.level}:  [${info.label}]: ${
      info.message
    } ${JSON.stringify(info.metadata)}`;
  })
);

const fileFormat = format.combine(
  format.timestamp(),
  format.label({ label: path.basename(process.mainModule.filename) }),
  format.metadata({ fillExcept: ["message", "level", "timestamp", "label"] }),
  format.json()
);

2.Now, create the logger.

const logger = createLogger({
  level: "info",
  defaultMeta: { service: "some-random-service" },
  format: fileFormat,
  transports: [
    new transports.File({
      filename: path.join(__dirname, "../logs/error.log"),
      level: "error",
    }),
    new transports.File({
      filename: path.join(__dirname, "../logs/activity.log"),
      maxsize: 5242880, //5MB
      maxFiles: 5 // just in case  
    }),
  ],
});

3.Enable console logging only in non-prod environments:

if (process.env.NODE_ENV !== "production") {
  logger.add(
    new transports.Console({
      level: consoleloggerLevel,
      format: consoleFormat,
    })
  );
}

4.Export it as the default logger

module.exports = logger;

On logger.info("Server started listening", { port: 9000 } );

This will print,

On the console:

2021-06-22T07:47:25.988Z - info:  [index.js]:   Server started listening {"service":"some-random-service", "port": 9000}

In the file:

{"message":"Server started listening","level":"info","timestamp":"2021-06-22T07:47:25.988Z","label":"index.js","metadata":{"service":"some-random-service", "port": 9000}}
2

Or you just use the

printf

function in conjunction with JSON.stringify

new winston.transports.Console({
  format: winston.format.combine(
    winston.format.colorize(),
    winston.format.simple(),
    winston.format.printf(context => {
      const msgstr = JSON.stringify(context.message, null, '\t')
      return `[${context.level}]${msgstr}`
    }),
  ),
})
0

winston-pretty will solve this issue in a robust and versatile way. It's a standalone program that takes winston json log as input and outputs human readable log lines. It also pretty prints json and errors.

npm install -g winston-pretty

npm run <your-project> | winston-pretty

https://www.npmjs.com/package/winston-pretty

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.