format.errors not working on transport level
See original GitHub issuePlease tell us about your environment:
winston
version?-
winston@2
-
winston@3
-
node -v
outputs: v12.16.1- Operating System? macOS
- Language? TypeScript ^3.9.7
What is the problem?
When I use format.errors({stack: true})
on logger configuration level, it works as expected. The logger parses JavaScript Error
object and prints correctly.
import { createLogger, format, transports } from 'winston';
const logFormatter = format.printf((info) => {
let { timestamp, level, stack, message } = info;
message = stack || message;
return `${timestamp} ${level}: ${message}`;
});
const logToConsole = createLogger({
level: 'info',
format: format.errors({ stack: true }),
transports: [
new transports.Console({
format: format.combine(format.colorize(), format.simple(), format.timestamp(), logFormatter),
}),
],
});
const err = new Error('network');
logToConsole.error(err);
Output:
2021-01-22T06:55:25.500Z error: Error: network
at Object.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/65822479/index.ts:19:13)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Module.m._compile (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/node_modules/ts-node/src/index.ts:858:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Object.require.extensions.<computed> [as .ts] (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/node_modules/ts-node/src/index.ts:861:12)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at main (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/node_modules/ts-node/src/bin.ts:227:14)
at Object.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/node_modules/ts-node/src/bin.ts:513:3)
I think the format.errors({ stack: true })
can be used on transport level too. But it doesn’t.
import { createLogger, format, transports } from 'winston';
const logFormatter = format.printf((info) => {
let { timestamp, level, stack, message } = info;
message = stack || message;
return `${timestamp} ${level}: ${message}`;
});
const logToConsole = createLogger({
level: 'info',
// format: format.errors({ stack: true }),
transports: [
new transports.Console({
format: format.combine(
format.errors({ stack: true }),
format.colorize(),
format.simple(),
format.timestamp(),
logFormatter,
),
}),
],
});
const err = new Error('network');
logToConsole.error(err);
Output:
2021-01-22T06:58:00.582Z error: undefined
What do you expect to happen instead?
I think the configuration of the logger level is a global configuration, and the configuration of the transport level is the configuration for each transport, and has the highest priority, covering the global configuration.
They are should have the same output.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:5
Top Results From Across the Web
Winston 3 + Loopback: Error messages are shown in access.log
The error.log and access.log files are created just fine. The error.log is not showing any info messages as expected. However, the access.log ...
Read more >A Guide Into Logging in Node.js using Winston - Bird Eats Bug
Learn the concept of logging in the context of Node.js apps using Winston and how to send application logs to Amazon's CloudWatch.
Read more >How to use the winston.format.printf function in winston - Snyk
To help you get started, we've selected a few winston.format.printf examples, based on popular ways it is used in public projects.
Read more >logging in JavaScript with Winston.js - ZetCode
Logging is not limited to identifying errors in software development. ... providing information in case of problems, finding application ...
Read more >winstonjs/winston - Gitter
Console( { level: 'error', format: winston.format.combine( ... <3.0.0 version or just doesn't work, including these issues on GitHub: winstonjs/winston#315 ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The winston-transport module “clones” the info object being formatted:
https://github.com/winstonjs/winston-transport/blob/e4a6ce1a0b788d4fe56b4be94c30a3bebe96c2e2/index.js#L90-L94
This leads to only copying enumerable properties, which on
Error
objects not evenmessage
is. So the object itself is pretty much blank at that point (at the very least devoid of anyError
properties).Since the error formatter is expecting an
Error
(or at least an error object inside a message property) it does nothing.An alternative that could work as it is right now:
Which I guess you can monkey patch on the class instance.
I’ll just point out that the log formatter (that is, the one that’s on the logger itself) does not receive a copy of an object but the object itself.
Any news on this?