node.js - Log error using a custom formatter in winston -


i have custom formatter added in winston. code looks below:-

'use strict';  const winston = require('winston'); winston.transports.dailyrotatefile = require('winston-daily-rotate-file');  var timestampformat = "yyyy-mm-ddthh:mm:ss.sssz"; var moment = require('moment');  function logtemplate(level, meta, message){     return "{" +         "\"timestamp\" : \"" + moment().format(timestampformat) + "\" ," +         "\"level\" : \"" + level + "\"," +         meta +         "\"content\" :" + message +         "}"; }  function formatter(args) {     var message = "";     var metastring = "";       message = json.stringify(args.message);      var reqid = "1212121211";     var metastring = "\"reqid\" : \"" + reqid + "\", ";     console.dir(json.stringify(args))     return logtemplate(args.level, metastring, message);     }     class logger {     constructor(appname, stream, shouldconsole) {         stream.formatter = formatter;         this.logger = new winston.logger({             level: stream.level,             transports: [                 new (winston.transports.console)(),                 new (winston.transports.dailyrotatefile)(stream)             ]         });         this.logger.level = "silly";         this.logger.cli();         return this;     }      trace() {         return this.logger.log("trace", arguments);     }      debug() {         return this.logger.log("debug", arguments);     }      info() {         return this.logger.log("info", arguments);     }      warn() {         return this.logger.log("warn", arguments);     }      error() {         return this.logger.log("error", arguments);     }      fatal() {         return this.logger.log("fatal", arguments)     } }  exports = module.exports = logger; 

i logging using string or object:-

var x = {}; x.a = "hello"; x.b = "world"; log.info("error message test", x); 

output

{"timestamp" : "2016-11-10t12:53:22.334+05:30" ,"level" : "info","reqid" : "1212121211", "content" :"   { '0': 'error message test', '1': { a: 'hello', b: 'world' } }"} 

when trying log error object below:-

log.info(new error("the error message")); 

output:-

{"timestamp" : "2016-11-10t03:43:47.613+05:30" ,"level" : "info","reqid" : "1212121211", "content" :"   { '0': \n   error: error message\n     @ server.<anonymous> (/users/debraj/code/github/jabong/jarvis/server.js:91:15)\n     @ server.g (events.js:286:16)\n     @ emitnone (events.js:86:13)\n     @ server.emit (events.js:185:7)\n     @ emitlisteningnt (net.js:1279:10)\n     @ _combinedtickcallback (internal/process/next_tick.js:71:11)\n     @ process._tickcallback (internal/process/next_tick.js:98:9)\n     @ function.module.runmain (module.js:577:11)\n     @ startup (node.js:160:18)\n     @ node.js:449:3\n    }"} 

can 1 let me know how can log error object formatted error message inside errmsg field , stack-trace inside trace field in json log using winston?

how this:

const formatter = name => options => {   let stack = '';   if (options.meta) {     if(options.meta.stack) {       stack += '\n' + options.meta.stack;       options.meta.stack = undefined;     }     if(options.meta.errorstack) {       if(stack !== '') {         stack += '\n';       }       stack += options.meta.errorstack;       options.meta.errorstack = undefined;     }   }   return timestamp() + ' - ' + name + ' - ' + formatlevel[options.level] + ' '     + (options.message ? json.stringify(options.message) : '')     + (options.meta && object.keys(options.meta).length ? '\n\t' + json.stringify(options.meta) : '' )     + stack; };  // config: {   transports: [     new winston.transports.console({... loggingconfig.console, formatter: formatter(name)})   ],   exitonerror: false } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -