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

[Feature] Expected response time colors #219

Open
ruscon opened this issue Aug 23, 2019 · 1 comment
Open

[Feature] Expected response time colors #219

ruscon opened this issue Aug 23, 2019 · 1 comment

Comments

@ruscon
Copy link

ruscon commented Aug 23, 2019

Expected response time colors feature

One of the options that come to mind:
An anonymous function that should return one of the log levels string name https://github.com/winstonjs/winston#logging-levels

"responseTimeColors": (responseTimeMs: number) => {
    switch(responseTimeMs) {
        case responseTimeMs < 50:
            return 'debug';
        case responseTimeMs < 100:
            return 'info';
        case responseTimeMs < 200:
            return 'warn';
        default:
            return 'red';
    }
}

Or try using the more interesting options described here (in the end of thee section)
https://github.com/winstonjs/winston#using-custom-logging-levels

Additionally, you can also change background color and font style. For example,

baz: 'italic yellow',
foobar: 'bold red cyanBG'
@yinzara
Copy link
Collaborator

yinzara commented Mar 1, 2020

Technically this can be done without a modification to the express-winston library currently.

You would first need to create a winston instance in which you've defined custom log levels with matching colors with the "colorize" formatter then use the "level" option of express-winston to define a custom level function that uses the res.

import winston from "winston";
import * as expressWinston from "express-winston";

const customLevels = {
  levels: {
    slow: 0,
    medium: 3,
    fast: 3
  },
  colors: {
    slow: 'bold red',
    medium: 'yellow',
    fast: 'green'
  }
};

const format = winston.format.combine(
  winston.format.colorize(),
  winston.format.json()
);

const logger = winston.createLogger({ 
  levels: customLevels.levels,
  format
});
winston.addColors(customLevels.colors);

const expressLogger = expressWinston.logger({
   winstonInstance: logger,
   level: (req, res) => {
       if (res.responseTime > 3000) {
           return "slow";
       } else if (res.responseTime > 1000) {
           return "medium";
       } else {
           return "fast";
       }
   }
});

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

2 participants