0.9.5 • Published 6 years ago

node-file-logger v0.9.5

Weekly downloads
311
License
Apache-2.0
Repository
github
Last release
6 years ago

Node File Logger

A simple logger for logging exceptions and error details in a log file.

  _   _           _        _____ _ _        _                                
 | \ | | ___   __| | ___  |  ___(_) | ___  | |    ___   __ _  __ _  ___ _ __ 
 |  \| |/ _ \ / _` |/ _ \ | |_  | | |/ _ \ | |   / _ \ / _` |/ _` |/ _ \ '__|
 | |\  | (_) | (_| |  __/ |  _| | | |  __/ | |__| (_) | (_| | (_| |  __/ |   
 |_| \_|\___/ \__,_|\___| |_|   |_|_|\___| |_____\___/ \__, |\__, |\___|_|   
                                                       |___/ |___/            

Docs & Features

Dependencies

moment.js moment-timezone.js node-stringify.js

Suports all logging levels

  • DEBUG: The DEBUG Level designates fine-grained informational events that are most useful to debug an application.
  • TRACE: The TRACE Level designates finer-grained informational events than the DEBUG
  • INFO: The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
  • WARN: The WARN level designates potentially harmful situations.
  • ERROR: The ERROR level designates error events that might still allow the application to continue running.
  • FATAL: The FATAL level designates very severe error events that will presumably lead the application to abort.

Create date-wise log files

It has an option to create a new log file for the current date which will be named like YYYY-MM-DD-log.log

Extras

  • Allows logging the service name & method name
  • Allows logging a short error message and an entire error object (optional)
  • Error objects are stringified and then logged to the log file

Has a callback

It allows you to run a callback after the logging process.

Steps to use

Installation

npm install node-file-logger --save

Create instance of Node file logger

// Ceate an instance of node file logger
const log = require('node-file-logger');
log.SetUserOptions(options); // Options are optional

Using options

// It is recommended to set options in a separate module and include it in the code
// Everything in the example below are default values
const options = {
  timeZone: 'America/Los_Angeles',
  folderPath: './logs/',      
  dateBasedFileNaming: true,
  // Required only if dateBasedFileNaming is set to false
  fileName: 'All_Logs',   
  // Required only if dateBasedFileNaming is set to true
  fileNamePrefix: 'Logs_',
  fileNameSuffix: '',
  fileNameExtension: '.log',     
  
  dateFormat: 'YYYY-MM-DD',
  timeFormat: 'HH:mm:ss.SSS',
  logLevel: 'debug',
  onlyFileLogging: true
}

// Note: If you set dateBasedFileNaming to false, then a log file will be created at the folder path with the provided fileName.
// If set to true then a logfile will be created with the name <fileName> provided in the options

log.SetUserOptions(options); 

Examples

Simple logging to a file with date-based log file naming

const options = {
  folderPath: './logs/',
  dateBasedFileNaming: true,
  fileNamePrefix: 'DailyLogs_',
  fileNameExtension: '.log',    
  dateFormat: 'YYYY_MM_D',
  timeFormat: 'h:mm:ss A',
}

const log = require('node-file-logger');
log.SetUserOptions(options);

// Log a simple error message
log.Info('Some informational log message');

// *****************************************************
// Ouput in Logfile: 
// File name : ./logs/Logs_2018_02_23.log
// 5:52:28 PM | Info | Some informational log message
// *****************************************************

// Log an error message with service and method names
log.Error('Something has failed!', 'Some service', 'Some method');

// *****************************************************
// Ouput in Logfile: 
// File name : ./logs/Logs_2018_02_23.log
// 5:52:28 PM | Error | Service: Some service | Method: Some method | Some error message
// *****************************************************

// Log an fatal error message with service and method names and error object
log.Fatal('Something has failed!', 'Some service', 'Some method', errorObj);

// *****************************************************
// Ouput in Logfile: 
// File name : ./logs/Logs_2018_02_23.log
// 5:52:28 PM | Fatal | Service: Some service | Method: Some method | Something is broken
{
  unhandledException: Something serious has happened
}
// *****************************************************

// Log an error message with callback
log.Info('Something has failed!', null, null, null, function() {
  // Do something
});

Simple logging to a file without date-based file naming

const options = {
  folderPath: './logs/',
  dateBasedFileNaming: false,
  fileName: 'All_Logs.log', 
  dateFormat: 'YYYY_MM_D',
  timeFormat: 'h:mm:ss A',
}

const log = require('node-file-logger');
log.SetUserOptions(options);

// Log a simple error message
log.Info('Some informational log message');


// *****************************************************
// Ouput in Logfile: 
// File name : ./logs/All_Logs.log
// 2018_02_23 5:52:28 PM | Info | Some informational log message
// *****************************************************

API Reference

Methods:

SetUserOptions()

Sets user options for node file logger

ParameterDescriptionDatatypeOptional
objectConfiguration Objectobjectno

Details of the configuration object

ParameterDescriptionDatatypeDefault Value
folderPathPath of the folder where log file will be savedstring./logs/
dateBasedFileNamingIf set to true, date based file naming standard will be followed for log filebooleantrue
fileNameNeeded in case of single log file, where dateBasedFileNaming is set to falsestringAll_Logs
fileNamePrefixLog file name prefixstringLogs_
fileNameSuffixLog file name suffixstring''
fileNameExtensionLog file name extensionstring.log
dateFormatDate format for timestamp loggingstringYYYY-MM-DD
timeFormatTime format for timestamp loggingstringHH:mm:ss.SSS
logLevelAllowed values - debug, prod, prod-trace (Details below)stringdebug
onlyFileLoggingIf set to false then messages are logged to console as wellbooleantrue

Details of log levels

'prod' and 'prod-trace' levels are suitable for production releases

Log level nameDescription
debugAll log level messages are logged
prodOnly 'warn', 'info', 'error' and 'fatal' messages are logged. 'debug' and 'trace' messages are not logged.
prod-traceOnly 'debug' messages are not logged. All the others are logged.

Debug()

Logs a debug message in the log file

ParameterDescriptionDatatypeOptional
errorMessageError message to be logged in the filestringno
serviceNameService name from which info was loggedstringyes
methodNameMethod name from which info was loggedstringyes
errorObjError object that needs to be loggedobjectyes
callbackCallback method that is called after error logging (may return an error)functionyes

Trace()

Logs a trace message in the log file

ParameterDescriptionDatatypeOptional
errorMessageError message to be logged in the filestringno
serviceNameService name from which info was loggedstringyes
methodNameMethod name from which info was loggedstringyes
errorObjError object that needs to be loggedobjectyes
callbackCallback method that is called after error logging (may return an error)functionyes

Info()

Logs a informational message in the log file

ParameterDescriptionDatatypeOptional
errorMessageError message to be logged in the filestringno
serviceNameService name from which info was loggedstringyes
methodNameMethod name from which info was loggedstringyes
errorObjError object that needs to be loggedobjectyes
callbackCallback method that is called after error logging (may return an error)functionyes

Warn()

Logs a warning message in the log file

ParameterDescriptionDatatypeOptional
errorMessageError message to be logged in the filestringno
serviceNameService name from which info was loggedstringyes
methodNameMethod name from which info was loggedstringyes
errorObjError object that needs to be loggedobjectyes
callbackCallback method that is called after error logging (may return an error)functionyes

Error()

Logs a error message in the log file

ParameterDescriptionDatatypeOptional
errorMessageError message to be logged in the filestringno
serviceNameService name from which info was loggedstringyes
methodNameMethod name from which info was loggedstringyes
errorObjError object that needs to be loggedobjectyes
callbackCallback method that is called after error logging (may return an error)functionyes

Fatal()

Logs a fatal error message in the log file

ParameterDescriptionDatatypeOptional
errorMessageError message to be logged in the filestringno
serviceNameService name from which info was loggedstringyes
methodNameMethod name from which info was loggedstringyes
errorObjError object that needs to be loggedobjectyes
callbackCallback method that is called after error logging (may return an error)functionyes

Log()

Used to log any kind of log message in the log file (Custom Log level)

ParameterDescriptionDatatypeOptional
logLevelLog level - 'debug', 'trace', 'info', 'warn', 'error', 'fatal', on any custom log level stringno
errorMessageError message to be logged in the filestringno
serviceNameService name from which info was loggedstringyes
methodNameMethod name from which info was loggedstringyes
errorObjError object that needs to be loggedobjectyes
callbackCallback method that is called after error logging (may return an error)functionyes

License

Apache 2.0