Skip to content

Logger library for new (and old) projects

Notifications You must be signed in to change notification settings

aorinevo/logger-1

Repository files navigation

@pager/logger

Circle CI

Structured logging for your verbose projects.

  • Pino
  • Hapi Plugin
  • Global default

Basic usage

Works as both a service worker logger or a Hapi plugin - both of which use standardized formatting and redaction configuration. For more details on how to work with Pino, take a look at its documentation or see configuration below for the setup details.

Redacting

This library has been set up with an array of standard redactions based on current usage. Each app should explicitly append and detail all potential leaks. There are no wildcard defaults because there are large associated performance issues with wildcards, particularly intermediate wildcards. Please do your part in log security to ensure no PHI or secrets are leaked into the logs; defaults provided in the code are append only.

Environment

Name Default Description
LOG_LEVEL info Lowest level to log in this order: trace, debug, info, warn, error, fatal
LOG_ERROR_THRESHOLD error Lowest error to send to error transport
LOG_PRETTY_PRINT none Set to 1 to enable pretty print - this is not json and follows the configuration for prettyPrint docs
LOG_EXPOSE_ERRORS none (Hapi plugin only) Set to 1 to expose errors to callers in the HTTP 500 range

Non-hapi:

{
  "level": "warn", // any pino default option overrides
  "redact": ['redactKey']
}

pino options

Pino default overrides per Pino's documentation.

Hapi

{
  "pino":  { // pino default overrides that matches Pino's configuration documentation
    "base": {
      "version": "v1.0.2" // adds `version: 'v1.0.2'` to every log
    }
  },
  "instance": customPinoInstance, // optionally, an already configured pino instance,
  "exposeErrors": true
}

pino (Object)

Pino configuration object per Pino's documentation

instance (pino object)

Already configured pino object

exposeErrors (boolean)

Return error and stacktrace along with 500 response as a payload. Useful in non-production environments. Default: false

Installation and Usage

Hapi

For 90% of projects, there will be no configuration needed, the plugin will do all the heavy lifting, and you can use the existing hapi server.log and request.log that you know and love. You can extract the logging instance for injection by server.logger() function or the require.logger object - see Hapi Pino docs for details.

const Hapi = require('hapi');
const LogPlugin = require('@pagerinc/logger/plugin');

const server = new Hapi.Server();
await server.register(LogPlugin);

server.log(['info'], { request: 'please log', response: 'hapi logging ^_^' });
// {
//    "level": 30,
//    "time": 1550778694025,
//    "pid": 74042,
//    "hostname": "securitys-MacBook-Pro.local",
//    "tags": [
//        "info"
//    ],
//    "data": {
//        "request": "please log",
//        "response": "hapi logging ^_^"
//    },
//    "v": 1
// }

Non-Hapi

// injecting a logger is best practice for most cases, defaulting to singleton is acceptable
const Logger = require('@pager/logger');

module.exports = (logger = Logger) => {

    Logger.info('Worker log');

    // .. worker works ...
    try {
        // do work
    }
    catch (err) {
        logger.error(err);
    }
};

Custom

const Logger = require('@pager/logger/lib/logger');
const MyCustomPrettyPrintLogger = Logger.createLogger({ prettyPrint: { colorize: false } });
MyCustomPrettyPrintLogger.info('pretty print me please');

About

Logger library for new (and old) projects

Resources

Stars

Watchers

Forks

Packages

No packages published