3.0.0 • Published 9 months ago

als-logger v3.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

ALS Logger (Version 3.0)

als-logger is a flexible and powerful logging utility for Node.js applications. It supports customizable log formats, file management, error handling, and much more.

Features

  • Customizable Logging Levels: Easily log different message types like log, warn, and error.
  • File Management: Automatically rotates log files and removes old entries based on age.
  • Error Handling: Captures uncaught exceptions and unhandled promise rejections.

Installation

Install the package via npm:

npm install als-logger

Usage

Basic Usage

Create an instance of the Logger and start logging messages.

const Logger = require('als-logger');

const logger = new Logger('/path/to/logs');

// Log messages of various types
logger.log('some msg','This is a log message.');
logger.warn('some msg','This is a warning.');
logger.error('some msg','This is an error.');

API

const logger = new Logger(dirPath:String,options={}:Object):instanceof Logger
logger.log(...msgs):Promise
logger.warn(...msgs):Promise
logger.error(...msgs):Promise
add(type:String, msgs:Array):Promise
logger.close():undefined

logger.report:instanceof LoggerReport
logger.report.limit(limit:Number):instanceof LoggerReport
logger.report.days(days:Number):instanceof LoggerReport
logger.report.type(...types:String):instanceof LoggerReport
logger.report.get(query:Object):Promise=>array

Options and validation

  • dirPath - dir path for logs
    • Default:undefined
    • Validation: throwing error if dirPath no type of string
      • if folder not exists, it will be created
    • Limits: You can't create two instances of Logger with same directory (throwing error)
  • options
    • Default: { timeout = 0, types = ['log', 'warn', 'error'], dev = false, errorFn = console, maxDays = 10 }
  • options.timeout - debounce timeout to write logs
    • Default 0
    • Validate: throwing error if timeout no positive number
  • options.types - logger methods from types
    • Default:['log', 'warn', 'error']
    • Validation: throwing error if types not array
      • Filtering array's item which not type of string
  • options.dev - developement mode. Using errorFn, instead writing logs.
    • Default:false
    • Validation: throwing error if dev not type of Boolean
  • options.errorFn
    • Default:console
    • Validation: throwing error if errorFn is not and object or don't have method from options.types
  • options.maxDays - maxDays to save in cache
    • Default:10
    • Validation: throwing error if maxDays no positive number

Report

logger.report
.limit(20) // limit for 20 logs
.days(5) // return up to 5 last days
.types('warn','error') // return only types of warn and error
.get() // return Promise which resolved to  [...,{ timestamp, type, msgs }]
3.0.0

9 months ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.0

1 year ago

0.5.0

2 years ago

0.6.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago