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

Allow configuring formats per-transport within the same logger #1401

Closed
dandv opened this issue Jul 8, 2018 · 5 comments
Closed

Allow configuring formats per-transport within the same logger #1401

dandv opened this issue Jul 8, 2018 · 5 comments

Comments

@dandv
Copy link
Contributor

dandv commented Jul 8, 2018

What's the feature?

I have a logger with a console transport, and another transport that goes to a cloud-based logging service. For the console, I want to prefix messages with a timestamp, and colorize them. For the cloud-based logging service, I don't want timestamps because the service provides its own, and I don't want colors either.

Is this a feature you're prepared to implement, with support from us?

PRs for this feature were already sent: #427 and #422

Is configuring formats per transport already possible, but I haven't seen that capability explained in the README?

@dandv
Copy link
Contributor Author

dandv commented Jul 8, 2018

Nevermind, this is indeed possible, but the documentation completely omits mentioning the feature.

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({
      filename: 'error.log', level: 'error',
      format: winston.format.simple(),
    }),
    new winston.transports.File({
      filename: 'combined.log', level: 'debug',
      format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),
    }),
  ],
});

logger.error('prefixed by the timestamp only in `combined.log`');

@dandv dandv closed this as completed Jul 8, 2018
@bradennapier
Copy link

bradennapier commented Aug 6, 2018

Definitely add to docs plz

@ariestav
Copy link

@dandv The printf code in your comment does not work out of the box, there is a missing closing brace after the timestamp code.

So this line...

format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),

...needs a closing brace like so

format: winston.format.printf(info => `${new Date().toISOString()}, ${info.message}`),

Thanks for the code and knowledge for custom formatting! Much appreciated!

@datnq102
Copy link

datnq102 commented Jul 7, 2019

Nevermind, this indeed possible, but the documentation completely omits mentioning the feature.

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({
      filename: 'error.log', level: 'error',
      format: winston.format.simple(),
    }),
    new winston.transports.File({
      filename: 'combined.log', level: 'debug',
      format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),
    }),
  ],
});

logger.error('prefixed by the timestamp only in `combined.log`');

@dandv what if I have another format outside of transports and inside the create logger method. Which format will be applied?

@sarangnx
Copy link

sarangnx commented Feb 4, 2020

Nevermind, this indeed possible, but the documentation completely omits mentioning the feature.

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({
      filename: 'error.log', level: 'error',
      format: winston.format.simple(),
    }),
    new winston.transports.File({
      filename: 'combined.log', level: 'debug',
      format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),
    }),
  ],
});

logger.error('prefixed by the timestamp only in `combined.log`');

@dandv what if I have another format outside of transports and inside the create logger method. Which format will be applied?

The one inside.

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

5 participants