Javascript logging module which writes in the console all warnings and erros
npm install --save jsw-logger
Currently, JSW-Logger can be used within a TypeScript based project, as a node dependency or directly in the browser.
First we need to import the dependency:
import { JSWLogger } from "jsw-logger";
var JSWLogger = require("jsw-logger").JSWLogger;
<script src="path/to/deps/dist/jsw-logger.min.js"></script>
<!-- This exposes a global "JSWLogger" variable -->
We can instanciate the logger passing a bunch of options:
var Logger = JSWLogger.getInstance({
level: 2, // logs "info", "warn" and "error" by default
hideAllLogs: false, // hides messsages from being logged
hideLevelLog: false, // hides the "LOG: " from the begining of the message
throwError: true // throw an error when "Logger.throw"
});
Or we can retrieve the instance with the options by default:
var Logger = JSWLogger.instance;
Also, the options can only by setted when instantiating with the first way.
To change them, we should access the options object: Logger.options.throwError = false
Or we can drop the instance so we can regenerate it: JSWLogger.__dropInstance()
Note that as this is a singleton (can only be instantiated once), so if you drop the instance, it will be dropped for all
The level logging hierarchy is as follows:
Name | Level | Method | Uses |
---|---|---|---|
Silly | 6 | Logger.silly | Some dummy logs, less relevants than a debug |
Debug | 5 | Logger.debug | For debugging purposes |
Verbose | 4 | Logger.verbose | For extended log messages |
Log | 3 | Logger.log | The standar logging |
Info | 2 | Logger.info | To show relevant information messages |
Logger.inform | Works as an alias for "info" | ||
Logger.information | Worksas an alias for "info" | ||
Warn | 1 | Logger.warn | To show application warnings |
Logger.warning | Works as an alias for "warn" | ||
Error | 0 | Logger.error | To output error generated by the application |
Logger.throw | Same as "error", but throws an Exception is configured to |
The lower we set the level whe instanciating, the less methods will output something. Eg.:
Logger.debug("Not shown"); // -> outputs nothing, as the default is log (2)
Logger.info("Some informative message"); // -> INFO: Some informative message
// Can use interpolation
Logger.warn("Be careful %s!", "Ryan"); // -> WARN: Be careful Ryan!
Logger.error("Line %d doesn't compile", 562); // -> ERROR: Line 562 doesn't compile
MIT