nightingale

Logger for browser and node

Usage no npm install needed!

<script type="module">
  import nightingale from 'https://cdn.skypack.dev/nightingale';
</script>

README

nightingale

Logger for browser and node

Nightingale

The nightingale is a bird best known for its powerful and beautiful song.

Unlike most popular loggers (winston, bunyan) handlers (also called transporters) are not defined at the logger, but rather with the configure method in nightingale or nightingale-app-*. That means libraries can freely use the nightingale-logger package without much dependencies and letting the application to configure all loggers ! It's more closer to the debug philosophy (the DEBUG env variable is in fact supported by nightingale-console, the basic handler for node). nightingale is and always will be compatible with all versions of nightingale-logger.

Install

npm install --save nightingale
npm install --save nightingale-console # for console handler in nodejs
npm install --save nightingale-browser-console # for console handler in browser

How to use

In an application

You can also use pre-configured isomorphic nightingale-app-console. If you use react-native or expo, check out nightingale-app-react-native.

npm install --save nightingale nightingale-console
import Logger, { configure, Level } from 'nightingale';
import ConsoleHandler from 'nightingale-console';

configure([
  {
    handlers: [new ConsoleHandler(Level.WARN)],
  },
  {
    keys: ['mylib', 'myotherlib'],
    handlers: [new ConsoleHandler(Level.ALL)],
  },
  {
    pattern: /^app\.server$/,
    handlers: [new ConsoleHandler(Level.ALL)],
    stop: true, // means the following config won't be used, if the pattern matches.
  },
  {
    pattern: /^app(?!\.server)/,
    handlers: [new ConsoleHandler(Level.INFO)],
  },
]);

// in one of your controllers
const logger = new Logger('app.controllers');
logger.debug('This is a log'); // will not be displayed

// in your server.js file
const logger = new Logger('app.server');
logger.debug('This is a log'); // will be displayed

You can configure several handlers with different Level, like console, slack, sentry.

Ensure the configure is always called before the first log! For example:

import './configure-logger';
import './myApp';

In a library

npm install --save nightingale-logger
import Logger from 'nightingale-logger';

const logger = new Logger('mylib');

logger.info('This is a log');
logger.warn('This is a warning !');
logger.success('It works !');

In the browser

You can also use pre-configured isomorphic nightingale-app-console.

npm install --save nightingale nightingale-browser-console

browser log example

import { configure, levels } from 'nightingale';
import { BrowserConsoleHandler } from 'nightingale-browser-console';

configure([
  {
    key: 'app',
    handlers: [new BrowserConsoleHandler(Level.INFO)],
  },
]);

The Logger class

See the Logger API, with all the methods you call to log things.

Debug

DEBUG=worker1 node example/debug

In browser

  • via url : ?DEBUG=worker1
  • via localStorage (in a console): localStorage.DEBUG='worker1'

Values are minimatch patterns and separated by ,.

Processors

Add information on a request

import Koa from 'koa';
import Logger, { configure, Level } from 'nightingale';
import { ConsoleHandler } from 'nightinale-console';
import webProcessor from 'nightinale-web-processor';

const app = new Koa();
const logger = new Logger('app');

configure([
  {
    key: 'app',
    handlers: [new ConsoleHandler(Level.ALL)],
    processors: [webProcessor],
  },
]);

app.use(async (ctx) => {
  logger.info('something to log !', { context: ctx });
});

app.use(async (ctx) => {
  ctx.logger.info('something to log !', { context: ctx });
});

More info

Handler

How a log is processed: has a layout and an output. Also define a minimum level.

You can find handlers on npm

Formatter

You can find formatters on npm

How the record is formatted, with its colors.

Output

Where the log is sent: console, file, ...

You can find outputs on npm

Processor

Add extra data in the record

Extends Logger

import { Logger } from 'nightingale';

class MyCustomLogger extends Logger {
  myCustomMethod(message) {
    return this.info(`custom message: ${message}`);
  }
}

const logger = new MyCustomLogger('app');

Global processors

import { configure } from 'nightingale';
import errorProcessor from './myErrorProcessor';

configure([{ processors: [errorProcessor] }]);

Global handlers

import { addGlobalHandler, levels } from 'nightingale';
import { SentryHandler } from 'nightingale-sentry';

configure([{ handlers: [new SentryHandler(Level.ERROR)] }]);

Children

You can create children.

import { Logger } from 'nightingale';
const loggerApp = new Logger('app');
const loggerMyService = loggerApp.child('myService');
// loggerMyService key is `app.myService`

Context

You can use context to add data to each log.

import { Logger } from 'nightingale';
const loggerMyService = new Logger('app.myService');

export function someAction(arg1) {
  const logger = loggerMyService.context({ arg1 });
  logger.info('starting');
  // do stuff
  logger.info('done');
}