Skip to content

loggin-js/loggin-js

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Loggin' JS

NPM version NPM quality Quality build status Downloads Dependencies Known Vulnerabilities

A little customizable logger for NodeJS.
Log to the console, to a file, to a remote service or create a custom one.

Check out the new v1.x beta here

Features

  • βœ”οΈŽ Easy
  • βœ”οΈŽ Customizable
  • βœ”οΈŽ Liteweighted

Table Of Content

Installing

  • Install with npm
npm install loggin-js --save

Usage

Using in node

// Require the logging library
const logging = require('loggin-js');

Check the wiki for more in depth documentation.

Using in express

Check this other repo for better integration with express.

Examples

Simplest Example

The fastest way of creating a logger is by using the .getLogger method wich creates a logger based on some options.
Based on those options it will create one of [ConsoleLogger, FileLogger, RemoteLogger] _explained below.
Here is a little example:

const logging = require('loggin-js');
const logger = loggin.getLogger({ channel: 'my-cool-app' });

logger.debug('User is loggin in');

Now let's check the .getLogger method a bit more in depth.
All Loggers and .getLogger accept what is called a Severity level, wich is used internally for filtering and managing log output. You can check more info about Severities here.

We can set a level in three ways:

  1. Passing a string (info):
    logger.getLogger({ level: 'DEBUG' })
  2. Passing an int (info):
    logger.getLogger({ level: 9 })
  3. Passing a severity instance (info):
    const { Severity } = logging;
    logging.getLogger({ level: Severity.DEBUG });

You can also pass a set of options, like setting colored output, changing the format, and more.

Full Example

// Require the logging library
const logging = require('loggin-js');

// Shortcuts
const { Severity } = logging;

// Get a logger with DEBUG severity. 
// Severity DEBUG will output any severity.
const logger = logging.getLogger({
  
  // level can be a <string> = 'DEBUG' a <int> = 7 or a <Severity> = Severity.DEBUG 
  level: 'DEBUG',

  // If output should be colored
  color: true,

  // Set formatter to medium - one of: ['short', 'medium', 'long']
  formatter: 'medium',
});

// Does the same as passing into settings, as done above
logger.setLevel(Severity.DEBUG);
logger.setColor(true);
logger.setFormatter('medium');


// Available predefined log levels
logger.info('info', { user: 'pedro', id: 10 });
logger.error('error');
logger.warning('warning');
logger.alert('alert');
logger.emergency('emergency');
logger.critical('critical');
logger.debug('debug');
logger.notice('Notice', {}, 'channel');


// If enabled set to false logs will not be output
logger.setEnabled(false);

File Logging Example

Log to files instead of the console

We create it making use of the FileLogger class.

// Require the logging library
const logging = require('loggin-js');

// Shortcut for the severity constants
const { Severity, Loggers, Notifiers } = logging;

// Create a file logger
const logger = new Loggers.FileLogger({
  
  // Display line number at the begining of the log 
  lineNumbers: true,

  // You can pass a pipes array to the file logger or you can do after instancing (showed below)
  pipes: [

    // Here we create a pipe that will pipe level ERROR logs to the file 'logs/error-logs.log'
    new Notifiers.Pipe(Severity.ERROR, 'logs/error-logs.log'),

    // This one will pipe level INFO logs to the file 'logs/info-logs.log'
    new Notifiers.Pipe(Severity.INFO, 'logs/info-logs.log')
  ]
});

// You can also add pipes after creating the logger as follows
logger.pipe(Severity.ERROR, 'logs/error-logs.log');
logger.pipe(Severity.INFO, 'logs/info-logs.log');


// INFO message will log to 'logs/info-logs.log'
logger.info('Logging a info log');

// ERROR message will log to 'logs/error-logs.log'
logger.error('Logging a error log', new Error('An error'));

// Will not be logged as only the ERROR and INFO severities will be output to their respective files
logger.warning('Logging a warning log');
logger.notice('Logging a notice log');
logger.alert('Logging a error log');

Custom Formatter Example

Custom formatter, customize the output of the log

const logging = require('loggin-js');
const logger = logging.getLogger({
  level: logging.Severity.DEBUG,
  color: true,

  /**
   * You can also use a custom formatter if the default one does not satisfy your needs.
   * In the formatter you can access all log properties and you can also set the 
   * color of some segments of the log by using <%L> where L is one of:
   *  - r red
   *  - g green
   *  - gr gray
   *  - b blue
   *  - p pink
   *  - y yellow
   *  - c cyan
   *  - m magenta
   *  - (nnn) a number between 0-255 # not implemented yet
   */
  formatter: '[{time.toLocaleString}] - <%m{user}> | {severityStr} | {message} - {JSON.stringify(data)}'
});

// Set user to root
logger.setUser('root');

// Set formatter
logger.setFormatter('[{time.toLocaleString}] - <%m{user}> | {severityStr} | {message} - {JSON.stringify(message)}');

// Log something
logger.debug('debug');             // $ [2018-6-2 00:46:24] - root - DEBUG - debug
logger.info('info', {data: 'Hi'}); // $ [2018-6-2 00:46:24] - root - INFO - info - {"data":"Hi"}


/**
 * Aditionally you can set the color of some parts of the message:
 * The output will be something like, with the last ERROR beeing red:
 * $ [2018-6-2 00:46:24] - root - ERROR - There was an ERROR 
 */
logger.error('There was an <%rERROR>'); 

Found a bug?

If you found a bug or like to leave a feature request, please leave an issue and we will take care of it.

Just make sure it's not already filed.

Collaborating

Hi there, if you like the project don't hesitate in collaborating (if you like to), submit a pull request, post an issue, ...
Any help or ideas are apreciated!