On Github paulzerkel / pres-winston
3/16/2016
console.log and console.error are part of Node.js out of the box
BUT
You're gonna have a bad time!
"A multi-transport async logging library for Node.js"
Available via NPM and MIT licensed
Designed to have few dependencies while being easy to expand for your needs
There are three main concepts in Winston
Each log has a priority called a level
Value Priority 0 Error 1 Warn 2 Info 3 Verbose 4 Debug 5 SillyYou create a log at a specific level
Value Priority 0 Error 1 Warn 2 Info 3 Verbose 4 Debug 5 SillyAnd Loggers and Transports filter based on levels
Value Priority 0 Error 1 Warn 2 Info 3 Verbose 4 Debug 5 SillyIt allows for a one-to-many relationship between the logger and available transports
You can programatically control what messages are logged and where they go
The logger is the object used to log a message
It is configured with a level and one or more transports
Winston provides a default configuration or you can create your own
Your application can have many loggers
Create a default logger and send two info messages to it
var winston = require('winston'); winston.log('info', 'Hello node.jSTL!'); winston.info('I am Winston');
Produces the following on stdout:
info: Hello node.jSTL! info: I am Winston
It's easy to create a custom logger with your configuration
var custom = new (winston.Logger)({ transports:[ new (winston.transports.Console)() ], level: 'error' }); custom.warn('potential problem...'); custom.error('Really big problem!');
This logger only handles errors
error: Really big problem
You can also change the config of an existing logger
custom.level = 'warn'; custom.warn('That\'s no moon...');
Warn messages will be logged now
warn: That's no moon...
The core of a message is just a string
Consider something more meaningful than 'Stuff is happening'
User defined metadata can be part of the message as well and Winston can include a timestamp and the level of the message
There are a variety of ways to format your message
winston.info('a very basic example'); winston.info('some', 'various', 'strings'); winston.info('The %s can\'t lie!', 'log'); var attendance = 45; winston.info(`There were ${attendance} participants`);
Results in:
info: a very basic example info: some various strings info: The log can't lie! info: There were 45 participants
Metadata can be optionally included with the message
var sysStatus = { users: 50, queueLength: 5 }; winston.default.transports.console.prettyPrint = true; winston.info('system status', sysStatus);
Formats the metadata:
info: system status { users: 50, queueLength: 5 }
Transports are destinations for logs
A log may be sent to more than one transport
Some transports are included with Winston but others are available as plugins
Shipped with Winston and supported by the core team
Writes to stdout and stderr
The log levels for stderr are configurable
You can control how metadata is printed
Output can be colorized based on the log level
Used for persisting logs to disk
Can be configured with a max file size, max number of files, ability to zip archives and more
These transports were originally part of Winston, but were removed in order to keep dependencies to a minimum
The transports are still maintained by the core Winston team
Community contributed transports
npm search winston
These are additional features included with Winston