Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

logawesome

0.0.1 • Public • Published

logawesome

npm node

Travis branch Coveralls github

license GitHub tag GitHub issues GitHub last commit GitHub top language GitHub code size in bytes

logawesome is a rudimentary logging implementation and still WIP.

Why another Logger?

Some loggers are doing too much or specific work.

logawesome is:

  • not having one global logging configuration, but multiple if desired
  • syntactic sugar easy to use logger
  • minimal functional interface for dispatching to so called appenders
  • unrestricted in terms of log levels or log messages

Example:

logger `INFO` `connecting to ${{url: 'https://...'}}`;

Usage

npm install logawesome

We use npm package debug. To make me verbose use DEBUG=logawesome.

Creating a log system

import {LogSystem} from 'logawesome';
 
const logSystem = new LogSystem();
logSystem.addAppender(appender1);
logSystem.addAppender(appender2);
logSystem.addAppender(appender2);

Console Appender

Using moment as date formatter.

import {createConsoleAppender} from 'logawesome';
 
const appender = createConsoleAppender({
    timestampToString: timestamp => moment(timestamp).format('HH:mm:ss.SSS')
});
 
logSystem.addAppender(appender);

File Stream Appender

import {createFileStreamAppender} from 'logawesome';
 
const appender = createFileStreamAppender({
    timestampToString: timestamp => moment(timestamp).format('YYYY-MM-DD HH:mm:ss.SSS'),
    timestampToFilepath: timestamp => path.join('logs', moment(timestamp).format(`[applogs_]YYYY-MM-DD[.${workerName}.json.log]`))
});
 
logSystem.addAppender(appender);

How it works

A logger can be invoked like this:

logger `INFO` `connecting to ${{url: 'https://...'}}`;

The LogSystem invokes all appenders deferred (process.nextTick). Log message is enhanced with:

  • a timestamp (whatever you passed to new LogSystem(timestampFn), or Date.now() per default)
  • an existing immutable context, updated by logger.set(...) or branched by logger.branch()

An Appender simply is a function, taking 6 arguments:

  • timestamp: number, produced by LogSystem
  • context: object, immutable context of the log entry
  • levelTpls: array of strings, first parameter of a template string
  • levelPlaceholders: array, second parameter of a template string
  • msgTpls: array of strings, first parameter of a template string
  • msgPlaceholders: array, second parameter of a template string

It's the appender's responsibility to aggregate information. At current logawesome is shipping with 2 appenders.

  • file stream appender: writing logs to time dependant rotating files
  • console appender: writing logs to console

Both using composition: Appender = Transformer -> Formatter -> Writer

  • Transformer: context aggregation according to template string
  • Formatter: produces log message
  • Writer: writes it to the target

Plans

  • micro packages, move appender logic to external packages
  • ELK stack support
  • syslog support

Keywords

Install

npm i logawesome

DownloadsWeekly Downloads

4

Version

0.0.1

License

MIT

Unpacked Size

176 kB

Total Files

20

Last publish

Collaborators

  • avatar