winston
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
@colors/colors | 1.6.0 | 1 B | MIT | prod | |
@dabh/diagnostics | 2.0.3 | 9.79 kB | MIT | prod | |
async | 3.2.5 | 146.47 kB | MIT | prod | |
is-stream | 2.0.1 | 2.17 kB | MIT | prod | |
logform | 2.6.0 | 25.6 kB | MIT | prod | |
one-time | 1.0.0 | 2.63 kB | MIT | prod | |
readable-stream | 3.6.2 | 32.46 kB | MIT | prod | |
safe-stable-stringify | 2.4.3 | 6.97 kB | MIT | prod | |
stack-trace | 0.0.10 | 3.51 kB | MIT | prod | |
triple-beam | 1.4.1 | 1 B | MIT | prod | |
winston-transport | 4.7.0 | 11.56 kB | MIT | prod |
Winston is a versatile logging library for Node.js. It's designed to be simple, universal and with support for multiple transports. A transport in Winston is essentially a storage device for your logs. Each logger can have multiple transports configured at different levels. For example, one might want error logs to be stored in a persistent remote location (like a database), but all logs to output to the console or a local file. This offers a flexible and extensible logging solution that caters to a broad range of needs.
To use Winston in a JavaScript codebase, first, you need to install it via npm with npm install winston
. After installation, create a logger using Winston's createLogger
function. The function takes an object as a parameter, specifying the logging level, the format of the logs, and the transports.
For example:
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
});
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple(),
}));
}
In the example, we're setting the log level to 'info', which means only events at this level and above will be logged. Logs are formatted in JSON and will be written to two different files: error.log for error logs and combined.log for logs with lower than the error log level. When not in production mode, logs are also outputted to the console.
The documentation for Winston can be found at the official Github repository here. The comprehensive documentation includes information on core concepts, capabilities, various options and detailed code examples to guide you on using the library effectively for your logging needs. Make sure to explore the examples folder for practical usage examples of the library.