-
Notifications
You must be signed in to change notification settings - Fork 1.8k
this._addDefaultMeta
is not a function
#1577
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
Comments
this._addDefaultMeta
is not definedthis._addDefaultMeta
is not a function
So it might be our fault. I can only reproduce this if we do:
|
Right, so it happens due to function not being bound to |
I am not sure in this case, as such a use case could be typical when using winston as custom logger. In my case I am using it as custom logger for SailsJS and until 3.2.0 everything was working fine :-/ My code: const logger = createLogger({
level: process.env.LOG_LEVEL || 'info',
format: combine(timestamp(), format.json()),
transports: [new transports.Console()],
})
module.exports.log = {
custom: logger,
} |
I guess we need to use arrow function there, it autobinds. Will try to come up with a PR for that ASAP. |
Quick'n'straighforward attempt to fix seems to break stuff. More investigation needed. You can see it here: https://github.com/kibertoad/winston/tree/fix/autobind-for-functions If you have an idea why would that break child logger functionality, I would be glad to hear that. |
is it because |
Howdy folks – thanks for reporting this. I had noticed it in passing when writing an unrelated test for The regression itself was introduced in #1483. Very thankful @soldair added test coverage for the scenario because fixing this would have introduced another regression without it <3. #1579 is an attempt to fix this issue. Currently it appears to be neutral from a performance perspective (yay), but unfortunately not the most obvious, straight-forward, or otherwise "elegant" solution (boo). Given that #1539 was overlooked for |
Perhaps a related error that I am seeing:
|
Yes. It is the same error. Thanks for reporting! |
Am still getting similar issue after updating to 3.2.1. Am using NestJS and have a custom logger service which creates a logger and then assigns class properties to map against the logger methods. etc. When trying to log, get TypeError: this.write is not a function Which now will log without any error, but is then not logging the metadata. |
@ColinMorris83 Could you create a PR with failing test in https://github.com/winstonjs/winston/blob/master/test/logger.test.js? (something similar to |
Hi @kibertoad,
Which gives following:
Changing the line to this.info = logger.info.bind(logger); makes the test pass. However, then I think that will lead to next issue of metadata not outputting, but I'm not sure how to test for that in the unit test |
There are an almost endless number of permutations of
e.g. for your failing test you could have either had Use it('defaultMeta() autobinds correctly when using a class', () => {
var logger = winston.createLogger({
level: 'info',
exitOnError: false,
transports: []
});
const service = Object.create(logger);
service.info('test');
}); Properly bind it('defaultMeta() autobinds correctly when using a class', () => {
class LoggingService {
constructor() {
var logger = winston.createLogger({
level: 'info',
exitOnError: false,
transports: []
});
// Either bind to the logger
this.info = logger.info.bind(logger);
// Or pass through the arguments
this.info = (...args) => logger.info(...args);
}
}
const service = new LoggingService();
service.info('test');
}); if your code was working previously it was an unintended side-effect of the larger regression not being resolved. This was a tricky thing to get right. |
Thanks @indexzero After doing this the metadata does not get output, is that a bug or am I using it wrong. service.info('test', { somemeta: 'something' }); |
Understood. Perhaps I was wrong. The second technique will absolutely work: it('defaultMeta() autobinds correctly when using a class', () => {
class LoggingService {
constructor() {
var logger = winston.createLogger({
level: 'info',
exitOnError: false,
transports: []
});
// Pass through the arguments
this.info = (...args) => logger.info(...args);
}
}
const service = new LoggingService();
service.info('test');
}); |
Still have no metadata logged when using the pass through way too.
The info object is this:
|
I suspect this is unrelated if we are down to the level of debugging your format function. |
the worst thing related this issue is when you write data on databases by waterline ORM. I ended up giving up using mixing winston.js and sails.js, sorry I've got the wrong number. :( I should write this comment on another issue |
@sapsaldog This sounds to me like something that sails.js should address, not winston. |
Yep I think probably unrelated to the initial issue, although I suspect could be a bug introduced somewhere since on 3.1.0 the info object had a meta property, from 3.2.0 it's not there. I will try to find some time to see if it all works ok when using winston as a normal required module, taking the use of classes and NestJs out of the equation. |
Not a bug. By design. Breaking change from The behavior was inconsistent, opted to spread all properties. It you want to "unspread" use https://github.com/winstonjs/logform#metadata |
Yep, just saw that stuff in the changelog, I'll take a look at the metadata docs you linked too. Thanks |
* [fix wip] Attempt to fix winstonjs#1577. * [fix test] Fallback to the "root" instance **always** created by `createLogger` for level convenience methods (e.g. `.info()`, `.silly()`). * [tiny] Remove useless assignment. * [tiny] DRY it up a little.
Fix for `self._addDefaultMeta is not a function` error. See winston issue: winstonjs/winston#1577 Resolves neovim#156
Fix for `self._addDefaultMeta is not a function` error. See winston issue: winstonjs/winston#1577 Resolves #156
Uh oh!
There was an error while loading. Please reload this page.
Please tell us about your environment:
winston
version?winston@2
winston@3
node -v
outputs: 8.9.3What is the problem?
Since the 3.2.0 release we are getting the following:
We're just calling
log
the same way as before.What do you expect to happen instead?
The code to not crash :-)
The text was updated successfully, but these errors were encountered: