Creating a custom logger for our application

To be useful, the logs need to be put in a context. In an environment where things are quickly changing, you want to provide some extra information in your messages, including the type of log (info, warning, critical, and so on), which version of your application produced it, and an accurate timestamp of when the error was produced. If your logs are all aggregated in the same place, you may also want to include the name of the service and the server that produced it. We will change our code to include that information.

When we first created the application, we kept the code to a bare minimum and avoided the use of extra packages. This made it easy to initially deploy the service. Now that we have more tooling around it, adding extra libraries is a non-issue. To improve our logging, we will rely on a library called Winston (https://www.npmjs.com/package/winston). Winston is one of the most common logging libraries in JavaScript. The library has many features and lets you manipulate your logs in several powerful ways, as we will see shortly.

On your computer, go to the root directory of the helloworld application:

$ cd helloworld  

Then install the winston library, as follows:

$ npm install winston --save  

Adding the --save option will make npm include the package definition in the package.json file.

With your text editor, open the helloworld.js file containing your application.

After the initialization of the http variable, we will initialize the Winston library as follows:

var http = require("http") 
var winston = require("winston")

Next, we will create a new variable to specify the version of the code. One of the ways we can achieve this is by assuming that this information will be provided later through the use of environment variables. For now, we will simply import the values as follows:

var version = process.env.HELLOWORLD_VERSION 

We will now create our custom logger. This will allow us to specify that we want a timestamp and provide the code to define that timestamp:

var logger = new winston.Logger({ 
  transports: [new winston.transports.Console({ 
    timestamp: function() { 
       var d = new Date()
       return d.toISOString()
    }, 
  })] 
}) 

To add the remaining context to all our logs, we will use a feature of the library called a rewriter, which allows us to modify the content of the meta.

Under the definition of the var logger variable definition, add the following:

logger.rewriters.push(function(level, msg, meta) { 
  meta.version = version 
  return meta 
}) 

Lastly, we now need to use the logger we just created and configured. For that, we will select the following code fragment:

console.log("Server running") 

We replace this code fragment with the following:

logger.info("Server running") 

We can now test our application by running it locally:

$ node helloworld.js  

As the application starts, it produces logs that look as follows:

2017-09-01T01:59:06.095Z - info: Server running version=undefined

Your helloworld.js file should look as shown at http://bit.ly/2uHZ0p3

The collection of logs on EC2 and ECS are typically different. We will show you how to make both collections, starting with EC2.

At this point, the console log isn't captured anywhere on EC2. We need to make some changes to our upstart script to save the console log and set the HELLOWORLD_VERSION environment variable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset