ts-logger-service v1.2.0
Logger
Simple library written in Typescript.
Installation
Use npm to install the package:
npm install ts-logger-service
The package has been tested with typescript 3.4.3.
Important Full support for different versions is not guaranteed.
Usage
Create and use logger
Use the Factory
Every logger is instantiated into LoggerFactory
. LoggerFactory provide static method to create and get a Logger.
LoggerFactory.instanceLogger('foo', LoggerLevels.INFO, new ConsoleLoggerService()); const logger = LoggerFactory.getLogger('foo'); logger.info('logger info'); // printed logger.error('logger error'); // not printed
In this example we create a new Logger named
foo
, set its level toINFO
and provide his service that manage every single log (see LoggerService).After created we get the Logger from LoggerFactory by its name and call methods info and error to print in console. Here the result
Use decorator
Work in progress...
Create custom LoggerService
It's possible create custom LoggerService to specify behaviour for the logger. We must extends the abstract class LoggerService
export class CustomLoggerService extends LoggerService {
// Console print
debug(message: string): void {
console.debug(message);
}
// Send error to API
error(message: string): void {
this.http.post('http://log-api.it', {
level: 'error'
message: message
})
}
// Write info into file
info(message: string): void {
fs.appendfile('log.txt', message);
}
.
.
.
}
It's in working the possibility to split the message in some different part (example logName, message, Date) to provide a more flessibility to the LoggerService.
Documentation
See HERE
License
Licenza MIT (todo)
Add copyright