Logging with Winston – node.jSTL



Logging with Winston – node.jSTL

0 0


pres-winston

A presentation on the logging library Winston for node.jSTL

On Github paulzerkel / pres-winston

Logging with Winston

node.jSTL

3/16/2016

Who am I?

Paul Zerkel

CTO @
Consultant @

What is logging?

  • The act of declaring a fact about the state of a program or system
  • Log messages are often persisted to some form of storage
  • Messages frequently include metadata

Why do we want logging?

  • Piece together what happened when things go wrong
  • Enables Exception Driven Development
  • Tracing logs can help you reason about a system in real time

Sounds great! How?

console.log and console.error are part of Node.js out of the box

BUT

You're gonna have a bad time!

Introducing Winston

"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

High Level Overview

There are three main concepts in Winston

  • Logs - the contents of the message
  • Loggers - how you save a message
  • Transports - destinations for logs

Levels

Each log has a priority called a level

Value Priority 0 Error 1 Warn 2 Info 3 Verbose 4 Debug 5 Silly

Levels

You create a log at a specific level

Value Priority 0 Error 1 Warn 2 Info 3 Verbose 4 Debug 5 Silly

Levels

And Loggers and Transports filter based on levels

Value Priority 0 Error 1 Warn 2 Info 3 Verbose 4 Debug 5 Silly

Why seperate the Logger and Transport?

It 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

Loggers

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

Default Logger

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

Custom Loggers

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

Update Configuration

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...

Log Messages

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

String Formatting

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

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

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

Transports - Winston Core

Shipped with Winston and supported by the core team

  • Console
  • File
  • HTTP
  • Memory (deprecated?)

Console Transport

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

File Transport

Used for persisting logs to disk

Can be configured with a max file size, max number of files, ability to zip archives and more

Transports - Winston 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

  • CouchDB
  • MongoDB
  • Redis
  • Riak
  • Loggly
  • Log.io

3rd Party Transports

Community contributed transports

  • Email
  • Message Queues
  • Windows Events
  • Much more

npm search winston

Extras!

These are additional features included with Winston

  • Profiling
  • Log querying
  • Log streaming
  • CLI presets

Implementation Considerations

  • Standardize Logging
  • Plan for log retrieval
  • Environment based settings
  • Purge policy
  • Beware NIH

Thanks!

Logging with Winston node.jSTL 3/16/2016