winston-graylog2 icon indicating copy to clipboard operation
winston-graylog2 copied to clipboard

log level not making it through

Open yitzgold opened this issue 5 years ago • 1 comments

If you have colorize turned on for the winston logger you will not get log level set correctly

The reason is that the first character is an escape character to make error display in red. https://github.com/namshi/winston-graylog2/blob/master/lib/winston-graylog2.js#L25

Not sure why you write that formatters can't be set on transport level see below how I did it and now it works!

Here is a sample what will work.

"use strict";

const os = require("os");
const winston = require("winston");
const { format } = winston;
const WinstonGraylog2 = require("winston-graylog2");
const {
  NODE_ENV,
  GRAYLOG_URL,
  GRAYLOG_PORT,
  GRAYLOG_FACILITY_NAME,
  LOG_LEVEL,
} = require("../config/env");

var grayLogOptions = {
  name: "Graylog",
  silent: false,
  level: LOG_LEVEL,
  handleExceptions: false,
  //showLevel: false,
  graylog: {
    servers: [
      {
        host: GRAYLOG_URL,
        port: GRAYLOG_PORT,
      },
    ],
    hostname: os.hostname(),
    facility: GRAYLOG_FACILITY_NAME,
    bufferSize: 1400,
  },
};

const winstonTransports = [];

if (NODE_ENV === "production") {
  winstonTransports.push(new WinstonGraylog2(grayLogOptions));
}

const logger = new winston.createLogger({
  transports: winstonTransports,
  format: format.combine(
    format.errors({ stack: true }), // Handle errors (was automagic in winston@2)
    // new
    format.metadata(),
  ),
});
logger.add(
  new winston.transports.Console({
    format: winston.format.combine(
      format.splat(), // Handle splat (was automagic in winston@2)
      format.timestamp(), // { timestamp: true }
      format.colorize(), // { colorize: true })
      format.simple(), // Default serialization in winston@2
    ),
  }),
);

module.exports = { logger };

yitzgold avatar Nov 07 '19 23:11 yitzgold

I think it was just assumed that you couldn't set the format at the transport level because the Winston formatting docs don't mention or show it.

jeremy-j-ackso avatar Feb 06 '20 20:02 jeremy-j-ackso