1.0.0 • Published 5 years ago

logner v1.0.0

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

Logner

Simple logger for your NodeJS application.

NOTE: This module is meant for Node.JS applications, not Web Browser.

Installation

npm install logner --save

Usage

To define log directory you need to call .configure function in your application's main file.

  • main.js
const { resolve } = require('path');
const log = require('logner');

// Basic Usage
log.configure({ rootDir: resolve(__dirname, 'logs') });

// Advanced Usage - Customize all properties
log.configure({
  rootDir: resolve(__dirname, 'logs'),
  logFiles: {
    info: true, // info will be now logged into file
    warn: true, // warn will be now logged into file
    debug: true,
    success: true,
    error: true
  },
  // To Store logs in one directory instead of grouping by date you can use following:
  // output: ./logs/<today>_<type>.log
  logFormat: {
    date: 'YYYYMMDD',
    directoryName: '%root%',
    fileName: '%date%_%type%'
  },
  colors: {
    info: 'cyan',
    warn: 'yellow',
    debug: 'magenta',
    success: 'green',
    error: 'red'
  }
});


log.info('this is info message!');
log.error('this', 'is', 'error', 'message!');
log.warn('this', 'is', 'warning', 'message!');
log.success('this', 'is', { success: 'message' });
log.debug(['this', 'is', 'debug', 'message!']);

// Using chalk. See all chalk options: https://www.npmjs.com/package/chalk#usage
const chalk = require('chalk');
log.info('this', 'is', chalk`{blue CHALK}`, 'message!');
  • some-other-file.js
const log = require('logner');

log.info('this is info message from some-other-file.js!');

Options

KeyTypeDescriptionRequiredDefault
rootDirstringLog directory path.:white_check_mark: YesN/A
logFilesobjectToggle which category can log into file.:white_check_mark: NoSee: Options: logFiles
logFormatobjectChange format log files.:white_check_mark: NoSee: Options: logFormat
colorsobjectChange specific category default color.:white_check_mark: NoSee: Options: colors

Options: logFiles |Key|Type|Description|Required|Default| |---|----|-----------|--------|-------| |info |boolean or number|Allow 'info' logs to be stored into file. |No| false | |warn |boolean or number|Allow 'warning' logs to be stored into file.|No| false | |debug |boolean or number|Allow 'debug' logs to be stored into file. |No| true | |success|boolean or number|Allow 'success' logs to be stored into file.|No| true | |error |boolean or number|Allow 'error' logs to be stored into file. |No| true |

Options: logFormat

  • See all available date formats @moment
  • Available directoryName shortcuts: %root% | %date% | %type%
  • Available fileName shortcuts: %date% | %type%

    Shortcuts:

    • %root% = value from options.rootDir
    • %date% = value from options.logFormat.date
    • %type% = log type: INFO | WARN | DEBUG | SUCCESS | ERROR
KeyTypeDescriptionRequiredDefault
datestringDate format for %date% parameterNoYYYY.MM.DD
directoryNamestringAllow 'warning' logs to be stored into file.No%root%/%date%
fileNamestringAllow 'debug' logs to be stored into file.No%type%

Options: colors

  • See all available colors @chalk
KeyTypeDescriptionRequiredDefault
infostringAllow 'info' logs to be stored into file.Nocyan
warnstringAllow 'warning' logs to be stored into file.Noyellow
debugstringAllow 'debug' logs to be stored into file.Nomagenta
successstringAllow 'success' logs to be stored into file.Nogreen
errorstringAllow 'error' logs to be stored into file.Nored