als-logger
v3.0.0
Published
Flexible and powerful logging utility for Node.js applications.
Downloads
14
Readme
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
, anderror
. - 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)
- Default:
options
- Default:
{ timeout = 0, types = ['log', 'warn', 'error'], dev = false, errorFn = console, maxDays = 10 }
- Default:
options.timeout
- debounce timeout to write logs- Default
0
- Validate: throwing error if timeout no positive number
- Default
- 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
- Default:
- options.dev - developement mode. Using errorFn, instead writing logs.
- Default:
false
- Validation: throwing error if dev not type of Boolean
- Default:
- options.errorFn
- Default:
console
- Validation: throwing error if errorFn is not and object or don't have method from
options.types
- Default:
- options.maxDays - maxDays to save in cache
- Default:
10
- Validation: throwing error if maxDays no positive number
- Default:
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 }]