1.0.0 • Published 5 years ago

logger3 v1.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

logger3

a simple but in depth logger for node.js, Customize by Composition.

The idea for this tool is composing logging functions, so you can make the logs look exactly how you want or need them to.

Usage

You just import the module, and call log.

const log = require('logger3');

log('Hello World');

That's not interesting, lets compose a simple prefix in front of the message.

const log = require('logger3');

const prefixed = log.prefix('[DEBUG]');

prefixed('Hello World, Debug Edition');
// -> [DEBUG] Hello World, Debug Edition

Prefixes can also be functions that get executed every time a log happens, this can be used for timestamps.

const log = require('logger3');

const timestamped = log.prefix(() => `[${Date.now()}]`);

timestamped('Hello World, Timestamped');
// -> [1558056117302] Hello World, Debug Edition
//
// Will vary depending when you run it.

You can also chain the logging modifiers

const log = require('logger3');

const chained = log.prefix(() => `[${Date.now()}]`).suffix('[SUFFIX]');

chained('Hello World');
// -> [1558056117302] Hello World, Debug Edition [SUFFIX]
//
// Will vary depending when you run it.

There are also a bunch of built in formatters, and colors. Colors are based on the chalk module, so you can use their api of chaining properties.

const log = require('logger3');
const { format, color } = log;

const red = log.make(color.red);
const brackets = log.make(format.paren);
const time = log.prefix(format.bracket(format.time));

red('Hello World');
brackets('Hello World');
time('Hello World');

// Hello World        (in red)
// (Hello World)
// [21:26:35] Hello World

When using in a project, you should create a file where you compose all your log functions, which are used by the rest of the program.

const log = require('logger3');
const { format, color } = log;

const bracketTime = format.bracket(format.time);

module.exports.info = log.make(color.blue).prefix('[INFO] ').prefix(bracketTime);
module.exports.warn = log.make(color.yellow).prefix('[WARN] ').prefix(bracketTime);
module.exports.error = log.make(color.red).prefix('[ERROR]').prefix(bracketTime);
module.exports.debug = log.make(color.magenta).prefix('[DEBUG]').prefix(bracketTime);

API

Logger

Get a blank logger with const log = require('logger3');

  • log() Logs to the console
  • log.prefix(prefix) Applies a prefix, returns a logger
  • log.suffix(suffix) Applies a suffix, returns a logger
  • log.make(maker) Applies a transformer function, returns a logger

Formatters (log.format)

Get the formatters object with const format = require('logger3').format;;

  • format.bracket(item) Formats text with [brackets]
  • format.paren(item) Formats text with (parenthesis)
  • format.brace(item) Formats text with {braces}
  • format.createFormatter((item) =>${item}) Creates a formatter, used by the other formatters to ensure a string is passed
  • format.timestamp() returns the timestamp (see Date.now);
  • format.time() returns the time formatted to HH:MM:SS;

Colors

Get the colors object with const color = require('logger3').color;. You can chain the properties to apply a color, background, and other text decoration, provided your terminal supports it.

Available Properties

  • reset
  • bold
  • dim
  • italic
  • underline
  • inverse
  • hidden
  • strikethrough
  • visible
  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • gray
  • redBright
  • greenBright
  • yellowBright
  • blueBright
  • magentaBright
  • cyanBright
  • whiteBright
  • bgBlack
  • bgRed
  • bgGreen
  • bgYellow
  • bgBlue
  • bgMagenta
  • bgCyan
  • bgWhite
  • bgBlackBright
  • bgRedBright
  • bgGreenBright
  • bgYellowBright
  • bgBlueBright
  • bgMagentaBright
  • bgCyanBright
  • bgWhiteBright