Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fluentd winston color formatting doesnt work at transport level #172

Open
happy-machine opened this issue Jun 11, 2020 · 1 comment
Open

Comments

@happy-machine
Copy link

happy-machine commented Jun 11, 2020

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 happy-machine changed the title fluentd winston color formatting doesnt work at volume level fluentd winston color formatting doesnt work at transport level Jun 11, 2020
@happy-machine
Copy link
Author

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant