better-logging-base
v1.3.0
Published
> Utility to produce better logging statements including level management
Downloads
743
Readme
#better-logging-base
- Installing - Bower - Manually
- Getting Started
- Applying Patterns - Prefix pattern - Datetime stamp patterns - Logging patterns
- Managing logging priority
better-logging-base has optional dependencies on momentjs and sprintf.js: without moment you can't pattern a nicely readable datetime stamp and without sprintf you can't pattern your logging input lines. Default fixed patterns are applied if either they are missing.
Will be implemented
Include logger.js, momentjs and sprintf.js in your web app.
todo...
By default, the prefix is formatted like so:
datetime here::[context's name here]>your logging input here
However, you can change this as follows:
todo...
If you have included moment.js in your webapp, you can start using datetime stamp patterns with better-logging-base. The default pattern is dddd h:mm:ss a
, which translates to Sunday 12:55:07 am. You customize the pattern as follows:
todo...
This way you can switch to a 24h format this way as well, for example, or use your locale-specific format.
- For all options, see moment.js
If you have included sprintf.js in your webapp, you can start using patterns with better-logging-base.
Traditional style with console
:
console.error ("Error uploading document [" + filename + "], Error: '" + err.message + "'. Try again later.")
// Error uploading document [contract.pdf], Error: 'Service currently down'. Try again later. "{ ... }"
Modern style with better-logging-base enhanced console:
console.error("Error uploading document [%s], Error: '%s'. Try again later.", filename, err.message)
// Sunday 12:13:06 pm::[myapp.file-upload]> Error uploading document [contract.pdf], Error: 'Service currently down'. Try again later.
You can even combine pattern input and normal input:
logger.warn("This %s pattern %j", "is", "{ 'in': 'put' }", "but this is not!", ['this', 'is', ['handled'], 'by the browser'], { 'including': 'syntax highlighting', 'and': 'console interaction' });
// 17-5-2015 00:16:08::[test]> This is pattern "{ 'in': 'put' }" but this is not! ["this", "is handled", "by the browser"] Object {including: "syntax highlighting", and: "console interaction"}
To log an Object
, you now have three ways of doing it, but the combined solution shown above has best integration with the browser.
logger.warn("Do it yourself: " + JSON.stringify(obj)); // json string with stringify's limitations
logger.warn("Let sprintf handle it: %j", obj); // json string with sprintf's limitations
logger.warn("Let the browser handle it: ", obj); // interactive tree in the browser with syntax highlighting
logger.warn("Or combine all!: %s, %j", JSON.stringify(obj), obj, obj);
- For all options, see sprintf.js
Using logging levels, we can manage output on several levels. Contexts can be named using dot '.' notation, where the names before dots are intepreted as groups or packages.
For example for 'a.b'
and a.c
we can define a general log level for a
and have a different log level for only 'a.c'.
todo...
The level's order are as follows:
1. TRACE: displays all levels, is the finest output and only recommended during debugging
2. DEBUG: display all but the finest logs, only recommended during develop stages
3. INFO : Show info, warn and error messages
4. WARN : Show warn and error messages
5. ERROR: Show only error messages.
6. OFF : Disable all logging, recommended for silencing noisy logging during debugging. *will* surpress errors logging.
Example:
config.prefixPattern = '%s::[%s]> ';
config.logLevels = {
'a.b.c': logEnhancerProvider.LEVEL.TRACE, // trace + debug + info + warn + error
'a.b.d': logEnhancerProvider.LEVEL.ERROR, // error
'a.b': logEnhancerProvider.LEVEL.DEBUG, // debug + info + warn + error
'a': logEnhancerProvider.LEVEL.WARN, // warn + error
'*': logEnhancerProvider.LEVEL.INFO // info + warn + error
};
// globally only INFO and more important are logged
// for group 'a' default is WARN and ERROR
// a.b.c and a.b.d override logging everything-with-TRACE and least-with-ERROR respectively
// modify later:
config.logLevels['a.b.c'] = $log.LEVEL.ERROR;
config.logLevels['*'] = $log.LEVEL.OFF;