Beautiful Winston: A Simple and Universal Logger

Sulenchy
Chingu
Published in
2 min readJul 25, 2018

Andela bootcamp like any other coding bootcamps is an intensive program of software development. There are deliverable of required standards bootcampers are required to deliver. One major requirement is that a code base with console.log statement or any commented line of code should not be deployed to production. A node.js application needs a server that can serves files on the go. When a server is started there should be a message confirming the start of the server. The common way developers use to display such message is by using console.log. Having the requirements established, how appropriate can one displays the server start message? Winston came to my rescue.

Winston is a logger for just about everything. It is designed to be a simple and universal logging library with support for multiple transports. Transport is a storage device for your logs. Each winston logger can have multiple transport configured at different levels (Logging levels). Winston aims to decouple parts of the logging process to make it more flexible and extensible. Using winston logging library is very easy.

There are ways one can use the library:

  1. The recommended way to use winston is to create your own logger. The simplest way is to use winston.createLogger
  2. One may also log directly via the default logger exposed by import winston from 'winston' //es6

Let us explore the second option, using the default logger. The default logger is accessible through the winston npm module directly. Any method that you could call on an instance of a logger is available on the default logger.

import winston from 'winston';
winston.log('debug','Now my debug message are written to console!');

By default, no transports are set on the default logger. You must add or remove transport via the add() and remove() methods.

const files = new wisnton.transports.file({filename: 'conmined.log'});
const console = new winston.transports.console();
winston.add(console);
winston.add(files);
winston.remove(console);
or do it with one call to configure();winston.configure({
transports:[
new winston.transports.file({filename: 'somefile.log'})
]
});

Hands on Keyboard

1. Clone the repo

git clone https://github.com/sulenchy/WinstonGettingStarted

2. Change direction into the winstongettingstarted folder

cd winstongettingstarted

3. Install the required dependencies

npm install

4. Run npm start

Note: The server starts and displays the start message Example app listening on port 3000using console.log

5. Press ctrl + c to halt the server running

6. Run npm run lint

Note: the eslint flags 1 no-console error

7. Run npm i wisntonto install winston library. Confirm the installation in your package.json under dependencies object.

8. Refactor the code in server/app.js as follow:

import express from "express";
import winston from "winston";
const app = express();app.get("/", (req, res) => res.send("Hello World!"));app.listen(3000, () => {
winston.log("info", "App listening at localhost:3000");
});

9. Run npm run lint

Note: eslint is no longer flagging no-console error

10. Run npm start

Now the codebase is ready for production

--

--