Logging me softly

Riyadh Al Nur
Aug 12, 2016 · 2 min read

LogDNA is a game changer. Offering 50GB with 2 day retention in an easy-to-use interface is a breath of fresh air. I have been through the well-known cloud logging services — Papertrail & Loggly before this, but their high cost against my simple applications was not something I wanted to buy into. Till LogDNA came out, I had stuck to local file based logging! LogDNA recently released their NodeJS library (17 days ago at the time of writing) with built-in Winston integration, so I immediately integrated it into my existing logging system and all is good with the world again.

To start, lets install our dependencies:

npm install —-save winston logdna

Now, in a new file — I usually put these files under a folder called utils — utils/logger.js:

‘use strict’;const winston = require(‘winston’);// This will expose winston.transports.Logdna
require(‘logdna’).WinstonTransport;
const logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'info',
filename: '/var/logs/logs.log',
handleExceptions: true,
json: true,
maxsize: 5242880,
maxFiles: 10,
colorize: false
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
}),
new winston.transports.Logdna({
key: <apikey>,
hostname: <hostname>,
ip: <ipAddress>,
mac: <macAddress>,
app: <appName>
})
],
exitOnError: false
});

module.exports = logger;

We will be dealing with the third transport type which deals with sending your logs to LogDNA —

  1. key: The API key used to access your account. You can obtain the key by signing in to your LogDNA account and navigating to Settings > Account Profile.
  2. hostname: The name you would like you to use for your server. If you would like to stick to the default hostname of your server, you can use Node’s built-in OS module to retrieve it using os.hostname().
  3. ip: Pretty self explanatory. This is the IP address of your server . Again you can retrieve this information using os.networkInterfaces(). Your IP address will be listed under the eth0 array.
  4. mac: This is your network interface’s physical address which if you are using os.networkInterfaces(), will be listed in the eth0 array next to the IP address.
  5. app: Last but not the least, this is the name you want to identify your application by.

So, now anywhere you use the logger module, one copy of it will be stored in a local log file, a copy printed on your console and finally another copy will be sent to your LogDNA account.

As an example:

‘use strict’;const http = require(‘http’);
const logger = require(‘./utils/logger’);
const server = http.createServer((request, response) => {
res.end();
});
server.listen(3000, () => {
logger.info(‘Server listening on port 3000’);
});

Have fun logging!

Riyadh Al Nur

Written by

Full-stack Software engineer. OSS lover. Write sometimes. Gamer. Family guy. Expat. Low-key goofy guy!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade