npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-file-logger

v0.9.5

Published

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

Downloads

2,573

Readme

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

| Parameter | Description | Datatype | Optional | | --------- |-------------| ---------| -------- | | object | Configuration Object | object | no |

Details of the configuration object

| Parameter | Description | Datatype | Default Value | | --------- |-------------| ---------| -------- | | folderPath | Path of the folder where log file will be saved | string | ./logs/ | | dateBasedFileNaming | If set to true, date based file naming standard will be followed for log file | boolean |true | | fileName | Needed in case of single log file, where dateBasedFileNaming is set to false | string | All_Logs | | fileNamePrefix | Log file name prefix | string | Logs_ | | fileNameSuffix | Log file name suffix | string | '' | | fileNameExtension | Log file name extension | string | .log | | dateFormat | Date format for timestamp logging | string | YYYY-MM-DD | | timeFormat | Time format for timestamp logging | string | HH:mm:ss.SSS | | logLevel | Allowed values - debug, prod, prod-trace (Details below) | string | debug | | onlyFileLogging | If set to false then messages are logged to console as well | boolean | true |

Details of log levels

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

| Log level name | Description | | --------- |-------------| | debug | All log level messages are logged | | prod | Only 'warn', 'info', 'error' and 'fatal' messages are logged. 'debug' and 'trace' messages are not logged. | | prod-trace | Only 'debug' messages are not logged. All the others are logged. |

Debug()

Logs a debug message in the log file

| Parameter | Description | Datatype | Optional | | --------- |-------------| ---------| -------- | | errorMessage | Error message to be logged in the file | string | no | | serviceName | Service name from which info was logged | string | yes | | methodName | Method name from which info was logged | string | yes | | errorObj | Error object that needs to be logged | object | yes | | callback | Callback method that is called after error logging (may return an error) | function | yes |

Trace()

Logs a trace message in the log file

| Parameter | Description | Datatype | Optional | | --------- |-------------| ---------| -------- | | errorMessage | Error message to be logged in the file | string | no | | serviceName | Service name from which info was logged | string | yes | | methodName | Method name from which info was logged | string | yes | | errorObj | Error object that needs to be logged | object | yes | | callback | Callback method that is called after error logging (may return an error) | function | yes |

Info()

Logs a informational message in the log file

| Parameter | Description | Datatype | Optional | | --------- |-------------| ---------| -------- | | errorMessage | Error message to be logged in the file | string | no | | serviceName | Service name from which info was logged | string | yes | | methodName | Method name from which info was logged | string | yes | | errorObj | Error object that needs to be logged | object | yes | | callback | Callback method that is called after error logging (may return an error) | function | yes |

Warn()

Logs a warning message in the log file

| Parameter | Description | Datatype | Optional | | --------- |-------------| ---------| -------- | | errorMessage | Error message to be logged in the file | string | no | | serviceName | Service name from which info was logged | string | yes | | methodName | Method name from which info was logged | string | yes | | errorObj | Error object that needs to be logged | object | yes | | callback | Callback method that is called after error logging (may return an error) | function | yes |

Error()

Logs a error message in the log file

| Parameter | Description | Datatype | Optional | | --------- |-------------| ---------| -------- | | errorMessage | Error message to be logged in the file | string | no | | serviceName | Service name from which info was logged | string | yes | | methodName | Method name from which info was logged | string | yes | | errorObj | Error object that needs to be logged | object | yes | | callback | Callback method that is called after error logging (may return an error) | function | yes |

Fatal()

Logs a fatal error message in the log file

| Parameter | Description | Datatype | Optional | | --------- |-------------| ---------| -------- | | errorMessage | Error message to be logged in the file | string | no | | serviceName | Service name from which info was logged | string | yes | | methodName | Method name from which info was logged | string | yes | | errorObj | Error object that needs to be logged | object | yes | | callback | Callback method that is called after error logging (may return an error) | function | yes |

Log()

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

| Parameter | Description | Datatype | Optional | | --------- |-------------| ---------| -------- | | logLevel | _Log level - 'debug', 'trace', 'info', 'warn', 'error', 'fatal', on any custom log level _ | string | no | | errorMessage | Error message to be logged in the file | string | no | | serviceName | Service name from which info was logged | string | yes | | methodName | Method name from which info was logged | string | yes | | errorObj | Error object that needs to be logged | object | yes | | callback | Callback method that is called after error logging (may return an error) | function | yes |

License

Apache 2.0