fluent / fluent-logger-node

A structured logger for Fluentd (Node.js)
Apache License 2.0
259 stars 83 forks source link

fluentd winston color formatting doesnt work at transport level #172

Open happy-machine opened 4 years ago

happy-machine commented 4 years ago

Hey folks

I apologise in advance ive read previous issues and stack overflow about this and cant seem to find a resolution. Ive even tried resorting to injecting a fluend plugin to remove color formatting before logs are sent to elasticsearch, this is clearly not best practice, it shouldnt be necessary to send something and then remove it i think.

If i remove the loggers format property the transport formats dont have any effect See below (i am caching levels warn and above in a local directory)

import { createLogger, format, transports, addColors } from "winston";
var fluentLogger = require("fluent-logger");

const winstonTransportConfigFactory = type => {
  addColors({
    error: "red",
    warn: "yellow",
    info: "cyan",
    debug: "green"
  });
  if (type === "file")
    return {
      //store warn and above locally
      filename: "./logs/local.log",
      level: "warn",
      format: format.json(),
      colorize: false
    };
  if (type === "console")
    return {
      format: format.combine(format.simple(), format.colorize()),
      colorize: true
    };
  if (type === "fluentd")
    return {
      format: format.json(),
      colorize: false
    };
};

const fluent = new (fluentLogger.support.winstonTransport(
  winstonTransportConfigFactory("fluentd")
))("agronomy-api.test", {
  host: "localhost",
  port: 24224,
  timeout: 3.0,
  requireAckResponse: true
});

const logger = createLogger({
  level: "debug",
  exitOnError: false,
  format: format.combine(format.simple(), format.colorize()),
  transports: [
    new transports.Console(winstonTransportConfigFactory("console")),
    new transports.File(winstonTransportConfigFactory("file")),
    fluent
  ]
});

logger.on("flush", () => {
  console.log("Flush");
});

logger.on("finish", () => {
  console.log(`Finish: ${fluent.sender}`);
  fluent.sender.end("end", {}, () => {});
});

export default logger;

I have used colorize as well as running the fomat.colorize function as there are mixed messages floating around about this i wasnt clear on how to do this and figured it wouldnt do any harm to overwrite this property if it did already exist?

happy-machine commented 4 years ago

Just for clarity, this is what i think i should be writing here!

import { createLogger, format, transports, addColors } from "winston";
var fluentLogger = require("fluent-logger");

const winstonTransportConfigFactory = type => {
  addColors({
    error: "red",
    warn: "yellow",
    info: "cyan",
    debug: "green"
  });
  if (type === "file")
    return {
      //store warn and above locally
      filename: "./logs/local.log",
      level: "warn",
      colorize: true
    };
  if (type === "console")
    return {
      format: format.combine(format.simple(), format.colorize()),
      colorize: true
    };
  if (type === "fluentd")
    return {
      colorize: false
    };
};

const fluent = new (fluentLogger.support.winstonTransport(
  winstonTransportConfigFactory("fluentd")
))("agronomy-api.test", {
  host: "localhost",
  port: 24224,
  timeout: 3.0,
  requireAckResponse: true
});

const logger = createLogger({
  level: "debug",
  exitOnError: false,
  format: format.simple(),
  transports: [
    new transports.Console(winstonTransportConfigFactory("console")),
    new transports.File(winstonTransportConfigFactory("file")),
    fluent
  ]
});

logger.on("flush", () => {
  console.log("Flush");
});

logger.on("finish", () => {
  console.log(`Finish: ${fluent.sender}`);
  fluent.sender.end("end", {}, () => {});
});

export default logger;