Skip to content

monsur/little-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

little-logger

Sometimes you need a robust logging mechanism for your enterprise-level system. Little-logger is not for those times.

Little-logger is a little logging utility for Node.js. Its for those times when you need to sprinkle a little logging throughout your code.

Though it may be little, it still packs some nice features, like:

  • Four different logging levels: debug, info, warn, error
  • Colorized output
  • Custom formatters for controlling the output
  • Log to stdio, stderr, or specify your own logging function.
  • Individual levels can be logged to different locations.
  • Small footprint: less than 100 loc, no external dependencies.

Install

npm install little-logger

Or just copy little-logger.js to your project directory.

Usage

Take a look at example.js for complete usage info. Here's an example:

var logger = require('little-logger');

var l = new logger.Logger();
l.info('The default log level is "info".');

l = new logger.Logger('debug');
l.info('This logger will log any message at level DEBUG or higher.');
l.info('Supports four log levels:');
l.debug('DEBUG');
l.info('INFO');
l.warn('WARN');
l.error('ERROR');

Creating a new logger

The Logger(level, options) method creates a new log instance. This constructor takes two arguments:

  • level (string) - The level to log. Valid values are 'debug', 'info', 'warn', 'error'. Default value is 'info'.
  • options (object) - An object specifying additional arguments for creating the logger:
    • color (boolean) - Whether the output should be colorized.
    • format (string) - The format of the output message. More on format below.
    • writer (function) - The function that logs the message. Default value is console.log.
    • utc (boolean) - Log dates as UTC. Defaults to false.

Message formatting

The format option supports printf-style formatting variables. The default format is:

%Y-%m-%d %H:%M:%S.%f %l: %a

Whie logs the date in YYYY-mm-dd format, followed by the time, followed by the log level and then the log message. The supported formatting options are:

  • %D - Logs the date as a string, like calling the date's toString() method.
  • %Y - The 4-digit year.
  • %m - The month.
  • %d - The date.
  • %H - The hour.
  • %M - The minute.
  • %S - The seconds.
  • %f - The milliseconds.
  • %l - The log level (lowercase).
  • %L - The log level (uppercase).
  • %a - The log message.
  • %% - The '%' symbol.

Methods

debug(msg) info(msg) warn(msg) error(msg) - Logs a message of the given type. Supports additional formatting parameters, see the Node.js docs for more details.

log(level, msg) - Logs a message of the given log level. The methods above are all shorthand for the log() method.

enable() disable() - Enables or disables logging.

level() - Returns the current log level.

level(l) - Sets the current log level.

About

A little logging utility for node.js.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published