My logger is set up like:
const myFormat = printf(info => { return `${info.timestamp}: ${info.level}: ${info.message}: ${info.err}`; }); const logger = winston.createLogger({ level: "info", format: combine(timestamp(), myFormat), transports: [ new winston.transports.File({ filename: "./logger/error.log", level: "error" }), new winston.transports.File({ filename: "./logger/info.log", level: "info" }) ] })
Then I am logging out some error like this:
logger.error(`GET on /history`, { err });
How is it possible to log the full stack trace for errors to via the error transport? I tried passing in the err.stack and it came out as undefined.
Thanks !
You can change the logging level in runtime by modifying the level property of the appropriate transport: var log = new (winston. Logger)({ transports: [ new (winston. transports.
Go to the example. log file in the logs folder to view the log. Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database. The Logger configuration below logs to a console and a file.
winston is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Each winston logger can have multiple transports (see: Transports) configured at different levels (see: Logging levels).
To print a stack trace to log you Should declare logger and method info(e. toString()) or log(Level.INFO, e. toString()). Logging is the process of writing log messages during the execution of a program to get error and warning messages as well as info messages.
For winston version 3.2.0+
, following will add stacktrace to log output:
import { createLogger, format, transports } from 'winston'; const { combine, timestamp, prettyPrint, colorize, errors, } = format; const logger = createLogger({ format: combine( errors({ stack: true }), // <-- use errors format colorize(), timestamp(), prettyPrint() ), transports: [new transports.Console()], });
Ref: https://github.com/winstonjs/winston/issues/1338#issuecomment-482784056
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With