simple-log-manager
simple Node.js log manager with different outputs
Install
$ npm install simple-log-manager
Info
You can use 3 different output types:
- File logger
- Console logger
- Dummy logger
Each logger has 4 methods for output - log
, warn
, info
, error
.
File logger
File loggers will write all output to file with logger name.
To create file logger you should specify logger name and 2 options:
fileName
and dir
.
you can specify
fileNamePattern
instead offileName
:
{
fileNamePattern: "instance-<DATE>.log",
dir: "../logs"
}
Example:
var manager = ; var options = fileNamePattern: "testName-<DATE>.log" dir: var logger = manager; // now you can use log methods: logger; logger; logger; //output testName-05_07.log: 14:19:38038 LOG Hello World ! 14:19:38040 WARN Hello World ! 14:19:38041 ERROR Hello World !
Console logger
Console logger is simple js console instance. So all methods will output to console.
Example:
var manager = ; var logger = manager; logger; // => "Hello World !" logger; // => "Hello World !" logger; // => "Hello World !"
Dummy logger
Name speaks for itself. It's dummy logger with all log method which will do nothing.
Example:
var manager = ; var logger = manager; logger; // => nothing logger; // => nothing logger; // => nothing
Usage
You can create loggers in your entry file and get it deeper in your app
Example
//main.js var manager = ; manager; manager; //app.js var logger = ; //controller.js var logger = ;
you can remove already created logger:
manager.remove(console)
Also you can switch between different output types with any condition, e.g. with environment variable.
Example:
const logger = { const manager = ; if processenvLOG && BooleanprocessenvLOG === false return manager; if BooleanprocessenvDEBUG return manager; return manager}; logger;...logger
In the example above all application logs will prints to file in logs
dir.
But if you need to debug you can pass DEBUG=true
to environment and vualá -
all output is in your console. Also you can pass LOG=false
and all logs will
be disabled without any changes in source code.