log-n-roll
Tiny 1kb logger with slim api and ability of limitless extension.
- Tiny: weighs less than kilobyte gzipped
- Pluggable: one built-in plugin for pretty formatting and limitless possibilities for extension
Note
This packege is Proof-of-Concept and should be considered as an unstable. Nevertheless, the basic idea has already been proved. Release coming soon.
Install
This project uses node and npm. Go check them out if you don't have them locally installed.
$ npm install log-n-roll
Then with a module bundler like rollup or webpack, use as you would anything else:
// using ES6 modules; // using CommonJS modulesvar log = ;
The UMD build is also available on unpkg:
You can find the library on window.log
.
Usage
const log = ; log; // Using the built-in pluginlog;log; log; // Loading the processorlet two = 2;for let i = 0; i < 1000000; i++ two *= two; two = Math; log;log;log;log; // Getting the named logger and setting its level to 'warn'const child = ;child; childlevel = 'info';child; ; ; ;;
Output:
Trace: Trace shows stacktrace
at Object.<anonymous> (C:\Users\u36\Dropbox\kutuluk\log-n-roll\examples\basic.js:3:5)
...
Trace: 17:08:01 [TRACE] default : Using any number of plugins adds to the stacktrace only one extra line
at Function.trace (C:\Users\u36\Dropbox\kutuluk\log-n-roll\dist\log-n-roll.js:1:730)
at Object.<anonymous> (C:\Users\u36\Dropbox\kutuluk\log-n-roll\examples\basic.js:7:5)
...
17:08:01 [ INFO] default : By default, the level of the default logger is 'trace'. All messages are displayed
+44ms [DEBUG] default : Debug shows the time difference from the last call of any logger method
17:08:01 [ INFO] default : Placeholders are supported. Two = 2
17:08:01 [ WARN] default : Warn message
17:08:01 [ERROR] default : Error message
17:08:01 [ INFO] child : The level of the logger can be changed at any time
17:08:01 [ INFO] default : Root logger can be obtained from any logger: child() === log() === log
17:08:01 [ INFO] any : Any logger can be obtained from any logger: child("any") === log("any")
+0ms [DEBUG] any : Any logger has extended the level from the default logger
17:08:01 [ INFO] any : Any logger has extended the plugins props from the default logger
Plugins
The resulted examples of plugins are simplified. Despite the fact that they are workable, it is not recommended to use in production.
log-stacktrace.js
{ try throw ; catch trace return tracestack; } module { // Return noop plugin if stacktrace support is absent if ! return {}; props = Object; return { if !propslevels return; let stacktrace = ; const lines = stacktrace; lines; const depth = props; if depth && lineslength !== depth + 1 const shrink = lines; stacktrace = shrink; if lineslength stacktrace += `\n and more`; else stacktrace = lines; statestacktrace = stacktrace; };};
log-meta.js
module { const meta = Object; const args = state; if argslength && typeof args0 === 'object' Object; statemeta = meta;};
log-message.js
const format = ; module { statemessage = ;};
log-json.js
module { const json = {}; const fields = Object; fields; statejson = JSON;};
to-file.js
const fs = ; module { props = Object; return { const fields = propsfields; fs; stateroll = propsroll; };};
An example of using these plugins
plugins.js
const log = ; const stacktrace = ;const meta = ;const message = ;const json = ; const toFile = ; log ; log; log; log; const child = ; child;
Console output:
17:06:19 [ INFO] default : Hello, console!
17:06:19 [ INFO] default : Hello, file!
my.log:
17:06:19 [ INFO] default : Hello, file!
{
"message": "Goodbye, console!",
"timestamp": "2018-02-07T13:06:19.333Z",
"level": "trace",
"logger": "child",
"meta": {
"source": "plagins.js",
"format": "json",
"tag": "message3"
},
"stacktrace": [
" at Object.<anonymous> (C:\\Users\\u36\\Dropbox\\kutuluk\\log-n-roll\\examples\\plugins.js:35:7)",
" at Module._compile (module.js:635:30)",
" at Object.Module._extensions..js (module.js:646:10)",
" and 4 more"
]
},