Sunday, 12 March 2017

How to only errors with winston module in Node.js

I have an application running on Node.js (server side) and I would like to only log errors based on an environment variable (For example : log_level=error node api.js) so this only logs errors on the console.

I have a log.js file that uses winston library

var fs = require("fs"),
  mkdirp = require("mkdirp"),
  path = require("path"),
  winston = require("winston");
  //const env = process.env.node_env || "development";

var filename = path.join(__dirname, "../app-debug.log");

//
// Remove the file, ignoring any errors
//
try {
  fs.unlinkSync(filename);
} catch (ex) {}

var logger = new(winston.Logger)({
  levels: {
    trace: 0,
    input: 1,
    verbose: 2,
    error: 3,
    debug: 4,
    info: 5,
    data: 6,
    help: 7,
    warn: 8
  },
  colors: {
    trace: "magenta",
    input: "grey",
    verbose: "cyan",
    prompt: "red",
    error: "blue",
    info: "green",
    data: "grey",
    help: "cyan",
    warn: "yellow"
  },
  transports: [
    new(winston.transports.Console)({
      prettyPrint: true,
      colorize: true,
      silent: false,
      timestamp: true,
      level: "error"
    }),
    new(winston.transports.File)({
      filename: filename
    })
  ]
});

module.exports = logger;

This file is in my root directory,What is happening now is that all log levels are printed in my console.What am I doing wrong? Any help?



via Rawan Hamdi

No comments:

Post a Comment