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

Optional color configuration for custom log levels #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ptlm500
Copy link

@ptlm500 ptlm500 commented Feb 3, 2021

Big fan of this library, thanks for writing it!

This is a small PR to add an optional color parameter containing a map of log levels to colors to the DiscordTransportOptions. When set, this will override the default color map.

import DiscordTransport from 'winston-discord-transport';

const levelColors = {
emerg: 9315107,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do emerg and some of these other levels exist in winston?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the syslog logging levels as specified by RFC5424. winston does support these logging levels if specified. eg:

const logger = winston.createLogger({
  levels: winston.config.syslog.levels,
  transports: [
    new winston.transports.Console({ level: 'error' })
  ]
});

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so how is it propagated to the transports?

Should the transport check for both kind of levels? Or does winston propagate the level to the transports in the default way (i.e. error, warn, info, etc in that order)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The level is sent to the transport as info.level, for example:

{
  message: 'An example log',
  level: 'notice',
  service: 'example-service',
  timestamp: '2021-03-29 12:28:30',
  [Symbol(level)]: 'notice',
  [Symbol(message)]: '{"message":"An example log","level":"notice","service":"example-service","timestamp":"2021-03-29 12:28:30"}'
}

There doesn't seem to be any way of getting the full log level/color configuration from winston. winstonjs/winston#1425

Perhaps a better approach would be to update the Colors interface to use the same type signature as Winston's AbstractConfigSetColors:

interface Colors {
  [key: string]: string | string[];
}

This would allow you to configure the transport using the built-in level configurations like this:

new DiscordTransport({
  webhook: 'https:/your/discord/webhook',
  defaultMeta: { service: 'my_node_service' },
  level: 'notice',
  colors: winston.config.syslog.colors
})

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea!

Although, I was working on moving this functionality to a standalone discord logger - https://github.com/sidhantpanda/node-discord-logger

And working to use that library inside this transport - https://github.com/sidhantpanda/winston-discord-transport/blob/feat/use_external_node_discord_lib/src/index.ts under branch eat/use_external_node_discord_lib.

I can create this feature in that library and extend those options here and add you as collaborator in the README. I don't want to discard your efforts here :(

Or if you feel like it, you can create this pull request there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, node-discord-logger looks great! I really like the error/json logging and the customizable metadata per log!

Would you plan then to extend node-discord-logger to allow configuration of custom logging/levels and colours?

I think that after migrating to use node-discord-logger in the transport it would be useful to accept some configuration in the form of winston's AbstractConfigSet and pass to node-discord-logger to configure both logging levels and colors. What do you think?

More than happy to help out with either library if you'd like.

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

Successfully merging this pull request may close these issues.

2 participants