Winston: creating custom format

Created on 4 Jul 2018  路  2Comments  路  Source: winstonjs/winston

Please tell us about your environment:

  • _winston version?_

    • [ ] winston@2

    • [x] winston@3

  • _node -v outputs:_ v8.11.1
  • _Operating System?_ (Windows, macOS, or Linux)
  • _Language?_ (all | TypeScript X.X | ES6/7 | ES5 | Dart)

What is the problem?

I need to stringify all the logs, so instead of change all the logs in my app i'm trying to write a transform function:

let opts = {
    level: level,
 format: winston.format.combine(
// I copied this from [WIKI](https://github.com/winstonjs/winston#creating-custom-formats)
      winston.format((info, opts) => {
         return JSON.stringify(info);;
      }),
      winston.format.timestamp()
    ),
 transports: [...]
}
return winston.createLogger(opts);

all my logs are in this format:

logger.info({....})

getting this error:

node_modules\logform\combine.js:37
    throw new Error([
    ^

Error: No transform function found on format. Did you create a format instance?
const myFormat = format(formatFn);
const instance = myFormat();

Did I missing something?

Most helpful comment

format() returns a 'factory' for your format, so to get an instance of it, you need to do format((info, opts) => ...)() (note the extra parens on the end). Double checked with your example and it worked for me. Good luck!

All 2 comments

format() returns a 'factory' for your format, so to get an instance of it, you need to do format((info, opts) => ...)() (note the extra parens on the end). Double checked with your example and it worked for me. Good luck!

@MHDMAM here is a sample which kept me going (using winston v3.3.3):

const winston = require('winston');

const myFormatter = winston.format((info) => {
  const {level, message} = info;
  info.level = `${level} with modified label`;
  info.message = `${message} with modified message`;
  return info;
})();

const logger = winston.createLogger({
  format: winston.format.combine(
    myFormatter,
    winston.format.simple(),
  ),
  transports: [
    new winston.transports.Console(),
  ],
});

logger.info('Test message'); // "info with modified label: Test message with modified message"
Was this page helpful?
0 / 5 - 0 ratings

Related issues

pocesar picture pocesar  路  3Comments

greenhat616 picture greenhat616  路  3Comments

Tonacatecuhtli picture Tonacatecuhtli  路  4Comments

kjin picture kjin  路  3Comments

anks333 picture anks333  路  3Comments