fishybell
fishybell

Reputation: 373

Using express-pino-logger and pino-pretty together

I'm have existing code that uses express-pino-logger. This words great with our ELK stack setup, but is pretty unfortunate (logs minified JSON) when running locally.

I'd like to use pino-pretty to make local use not a pain.

There is an alternative in pino-pretty-express that solves the problem, but uses its own pretty formatter. I'd like to use the standard packages from pinojs if I could.

Here's what I have so far:

// with just pino-pretty installed, pino works out of the box
const pino = require('pino')
const logger = pino({
  prettyPrint: true
})

logger.info('hi') // prints pretty

And:

// adding this option to express-pino-logger, doesn't work
const pino = require('express-pino-logger')
const logger = pino({
  prettyPrint: true
})

logger.info('hi') // does NOT print pretty

Upvotes: 5

Views: 9740

Answers (3)

omar yousry
omar yousry

Reputation: 21

const pino = require('pino');
const expressPino = require('express-pino-logger');
const logger = pino({ level: process.env.LOG_LEVEL || 'info', prettyPrint: true });
const expressLogger = expressPino({ logger });

Upvotes: 2

vijayakumarpsg587
vijayakumarpsg587

Reputation: 1177

I suppose the express pino logger is explicitly being used to log unique request ids. There is an alternative approach using the http-context and node-uuid (although I am unsure about the benchmark results)

You can create a unique uuid before the call of each request like this

const uuid = require('node-uuid');
const httpContext = require('express-http-context');
....
  app.use(httpContext.middleware);
 app.use((req, res, next) => {
            httpContext.set('reqId', uuid.v4());
            next();
        });

.. you can obtain the requestId anywhere from the httpContext. No need of relying on the req object to be passed Example usage in a custom implementation of PinoLogger service

public infoLogService (fileName): pino.Logger {
        return pino({
            level: 'info',
            name: this.appService.getApp_name(),    

            messageKey: 'feedback-Logs',
            base: {pid: process.pid, hostname: os.hostname,
                timestamp: this.getTimeStamp(),
                appName: this.appService.getApp_name(),
                fileName: fileName,
                request_id: isNullOrUndefined(httpContext.get('reqId')) ? 'Not an actual request ' : httpContext.get('reqId')
            },
            enabled: true,
            useLevelLabels: true,
        });
    }

For every http calls, we get a UUID. for other type of loggings like logger invocation before the app starts to find if the connection to database happened successfully, there will be no uuid and hence passed a default message

Upvotes: 0

fishybell
fishybell

Reputation: 373

I've solved my own problem I guess.

The key lies in the very last example on the express-pino-logger page:

'use strict'

const pino = require('pino')()
const expressPino = require('express-pino-logger')({
  logger: pino
})

Here's my solution:

// use pino-pretty and express-pino-logger together
const basicPino = require('pino')
const basicPinoLogger = basicPino({ prettyPrint: true })
const expressPino = require('express-pino-logger')({
  logger: basicPinoLogger
})

const logger = expressPino.logger

logger.info('hi') // prints pretty

Upvotes: 6

Related Questions