-
Notifications
You must be signed in to change notification settings - Fork 105
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
messageFormatter doesn't seem to work (since Winston 3.0.0?) #90
Comments
There are a few examples, none about I will add an example for this case, check the result, if that's the case provide a fix, and come back to you; thanks for the issue! |
I've just tested this after reading the source code and this example works for me:
Could you please check if that's the case also for you? |
Sorry, I didn't have much time to get back to you recently. I tried your example and that works fine for me too! So I got curious and tried to find why it didn't work in my original case. Finally I discovered that when specifying the option Cheers! |
I ran into the same issue, expecting the format option to be used if const winston = require('winston');
const WinstonCloudWatch = require('winston-cloudwatch');
const { MESSAGE } = require('triple-beam');
winston.createLogger({
format: winston.format.json(), // default since winston 3.0.0
transports: [
new WinstonCloudWatch({
jsonMessage: false,
messageFormatter(info) {
return info[MESSAGE];
},
// ... other options
})
]
); I think this should be the default behavior from winston 3.0.0. |
Also I'd like to mention a couple more things:
I'd like to say that using the bult-in Winston formats could be done by setting Align this interface WinstonJS' would lead to better usage IMO. Thank you. EDIT: typo |
@Carlovan @tiborbaksa hello and sorry for the delay, I've been quite busy on both work and non-work related issues. Is this still relevant? From what I remember by using Anyway, I have more free time now and could tackle the issues you're experiencing. |
@lazywithclass what @Carlovan reported is still valid. I just came across it as well. Winston Format {
format: winston.format.printf(({level, message}) => {
return `${level} - ${message}`;
})
} Winston-CloudWatch MessageFormatter {
messageFormatter: ({level, msg}) => {
return `${level} - ${msg}`;
}
} |
@lazywithclass I also have the same expectations as @tiborbaksa. If I create my logger with something like: winston.createLogger({
exitOnError: false,
transports: [],
format: winston.format.printf(customFormatter)
}); I would expect if I don't specify a custom Currently, I need to add transport like this in order to achieve the same format as the console transport for example which is making use of the format option inside logger.add(new WinstonCloudWatch({
awsRegion: 'eu-central-1',
logGroupName: 'logger',
logStreamName: process.env.NODE_ENV || 'development',
messageFormatter: customFormatter
})); |
So you want to be able to do winston.createLogger({
exitOnError: false,
transports: [],
format: winston.format.printf(customFormatter)
}); and rely on Winston's default, is that correct? |
@lazywithclass yes that is correct, so I can use the same format in all my transports. |
@fewieden I am trying to put myself in your scenario, I wrote the following example script var winston = require('winston'),
WinstonCloudWatch = require('../index');
var transport = winston.createLogger({
exitOnError: false,
transports: [
new WinstonCloudWatch({
logGroupName: 'testing',
logStreamName: 'first',
awsRegion: 'us-east-1'
})],
format: winston.format.printf(function({level, message}) {
return `${level} - ${message}`;
})
});
transport.error('error error!'); and saw Thanks a lot! |
Hey @lazywithclass, based on your example, it looks like that's what the ask from OP was - to be able to define a I'm curious to try your example, as I've done something similar in the past with no success. I'll give it another shot soon, and if I get it working, I can come back and post some code :) |
@lazywithclass in principle that's what I would like to do, but I think the problem with your example here is, that the format you specified is the same as you use for cloudwatch as default, if Lines 25 to 27 in 276dcdf
Our custom format looks like this: import { format } from 'util';
// workaround for https://github.com/winstonjs/winston/issues/1427
export function customFormatter({level, message = '', msg = '', [Symbol.for('splat')]: args = []}) {
return `${level} - ${format(message || msg, ...args)}`;
} Simple example transport.info('some text'); Advanced example transport.info('some text', {foo: 'bar'}, new Error('wtf')); |
About I've changed my proposed example to sum up your problem as follows var winston = require('winston'),
WinstonCloudWatch = require('../index');
function format(message, ...rest) {
// I am inventing a custom formatter
return `
${message}
${rest.map(r => ` * ${r}`).join('\n')}
`
}
function customFormatter({level, message, [Symbol.for('splat')]: args = []}) {
return `${level} - ${format(message, ...args)}`;
}
var transport = winston.createLogger({
exitOnError: false,
transports: [
new WinstonCloudWatch({
logGroupName: 'testing',
logStreamName: 'first',
awsRegion: 'us-east-1'
})],
format: winston.format.printf(customFormatter)
});
transport.info('some text', {foo: 'bar'}, new Error('wtf')); In AWS CloudWatch it just logs
Do you confirm that you would like the second output to appear in AWS CloudWatch? Thank you! P.S.: This is one of those issues that would've been solved in 5 minutes if we were sitting at the same desk :D, but hopefully I'm getting closer to understanding. |
If you like we could also do a pair programming session. |
I'll start working towards a solution. In case I'll have problems I'll come back to you. Thanks for the offer :) |
To have a clean implementation I think we have to move away from the obsolete inheritance model I coded ages ago and introduce the much cleaner class model. This will also allow to get rid of old practices like One of my wildest dreams would be also to remove the horrific This won't be immediate though and will be surely marked as winston-cloudwatch v3.0.0. |
Besides work and university issues I'm progressing slower than usual because I've got some issue with mockery, I can't get it to mock a |
Try like this guys const messageFormatter = (data) => {
const { level, message, timestamp, ...meta } = data;
const metatdata = `:\n${JSON.stringify(meta, null, 2)}`;
return `${timestamp} [${level}] ${message}${metatdata}`;
};
function cloudwatch() {
const options = { messageFormatter };
return new winstonCloudwatch(options);
}
const logger = winston.createLogger({
format: format.combine(format.timestamp()), // Just to allow timestamp
transports: [cloudwatch()],
}); |
Hello,
Recently I upgraded to Winston 3.0.0 and decided to add some message formatting as well. I my old setup I never used the
messageFormatter
property ofwinston-cloudwatch
, but now I felt a need to.However it seems like the
messageFormatter
function is never executed. No matter what I change on the logObject or what I return from the function. Even when I do aconsole.log
in the formatter, it never shows up.I did notice that the whole formatting system of Winston changed quite a bit since 2.x. Maybe this had an impact on this formatter as well? Do you have a working example somewhere?
Best, Thijs
The text was updated successfully, but these errors were encountered: