General purpose JavaScript logger, supports namespaces, colors, compact time and 8 log levels.
var Log = require('compact-log');
var log = new Log({
path: __dirname + '/log',
levelMode: 'smartNoBrackets'
});
log.emergency('emergency');
log.alert('alert');
log.critical('critical');
log.error('error');
log.warning('warning');
log.separator();
log.notice('notice');
log.info('info');
log.debug('debug');
log.separator('Separators can use text.', 'info');
log.info('It\'s able to use %s formats!', 'stuff');
log.separator('emergency separator', 'emergency');
log.debug('You can even dump objects.', {
test: true,
woah: 'affirmative'
});
log.de('You can use');
log.dbug('some shorter');
log.debu('variations to log!');
log.warning('This is a really long line, which will probably overflow the width of this console window.');
var ns = log.createNamespace({
name: 'test'
});
ns.sepa('Even separators can use short functions.');
ns.info('Namespaces, yay!');
ns.se(false, 'alert');
var time = log.createNamespace({
name: 'compressed time test'
});
var n = 0;
setInterval(function () {
time.debug(n++);
}, 1000);
Just type npm install compact-log
.
If you want to save it as a dependency in package.json
you should use npm install compact-log --save
.
var Log = require('compact-log');
var log = new Log({/*options*/});
log.info('Hello world!');
You can pass options when calling new Log(options)
. Options is an object which can contain the following attributes.
attribute | default value | description |
---|---|---|
logLevel | 'debug' | sets the logLevel for console & file logs |
consoleLogLevel | =logLevel | overrides the logLevel for consoleLogs only |
fileLogLevel | =logLevel | overrides the logLevel for fileLogs only |
separatorLogLevel | =7 | sets the default logLevel for separators |
compressedTime | 'day' | shortens the timestamp of each output and logs a time-update output when the given time period is exceeded (e.g. every day) and a log is issued |
levelMode | 'shortNoBrackets' | allows changing of the level names (e.g. ERROR -> E) |
clear | false | clears the console when a new log instance is created |
path | false | sets the log folder |
compressedTimeAsSeparator | true | allows disabling the separator layout for compressedTime-messages |
prettyJSON | true | prints the log file's JSON with tab characters and newlines |
alternativeColumnCount | 100 | sets the amount of console columns if process.stdout.columns is not defined |
- logLevel: none, emergency, alert, critical, error, warning, notice, info, debug, 0-8
- consoleLogLevel: none, emergency, alert, critical, error, warning, notice, info, debug, 0-8
- fileLogLevel: none, emergency, alert, critical, error, warning, notice, info, debug, 0-8
- separatorLogLevel: none, emergency, alert, critical, error, warning, notice, info, debug, 0-8
- compressedTime: none, year, month, day, hour, minute, second
- levelMode: short, smart, full, tiny, shortNoBrackets, smartNoBrackets, fullNoBrackets, tinyNoBrackets, numbers, single
- clear false, true
- path: false or any path to a (existing or non-existing) folder
- compressedTimeAsSeparator: true, false
- prettyJSON: true, false
Logs a message with logLevel 1 = 'emergency'.
Logs a message with logLevel 2 = 'alert'.
Logs a message with logLevel 3 = 'critical'.
Logs a message with logLevel 4 = 'error'.
Logs a message with logLevel 5 = 'warning'.
Logs a message with logLevel 6 = 'notice'.
Logs a message with logLevel 7 = 'info'.
Logs a message with logLevel 8 = 'debug'.
Creates a separator consisting of a dashed line (-----). If text is specified it will show this text in the line's center. logLevel
will override the default separatorLogLevel
.
Creates a namespace. Namespaces will automatically append their name to the log message. Options supports name: 'enter name here'
and colors: ['array of colors']
. The supported colors are based on cli-color.
Example:
var ns = log.createNamespace({
name: 'test',
colors: ['bgYellowBright', 'black']
});
ns.info('It worked!');
// INFO 12:34:56 test It worked!
This is the log output generated by the example used in the beginning of this document. If you need any other output format for your project - please file an issue on github.
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413798},"level":"emergency","message":"emergency"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413799},"level":"alert","message":"alert"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413799},"level":"critical","message":"critical"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413800},"level":"error","message":"error"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413800},"level":"warning","message":"warning"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413802},"level":"notice","message":"notice"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413803},"level":"info","message":"info"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413803},"level":"debug","message":"debug"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413805},"level":"info","message":"It's able to use stuff formats!"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413806},"level":"debug","message":"You can even dump objects.","dump":[{"test":true,"woah":"affirmative"}]}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413807},"level":"debug","message":"You can use"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413807},"level":"debug","message":"some shorter"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413808},"level":"debug","message":"variations to log!"}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413809},"level":"warning","message":"This is a really long line, which will probably overflow the width of this console window."}
{"time":{"human":"2015-03-03 13:40:13 +01:00","stamp":1425386413812},"level":"info","namespace":"test","message":"Namespaces, yay!"}
{"time":{"human":"2015-03-03 13:40:14 +01:00","stamp":1425386414816},"level":"debug","namespace":"compressed time test","message":"0"}
{"time":{"human":"2015-03-03 13:40:15 +01:00","stamp":1425386415817},"level":"debug","namespace":"compressed time test","message":"1"}
This one is shortened, because it takes up much more space.
{
"time": {
"human": "2015-03-03 13:33:43 +01:00",
"stamp": 1425386023343
},
"level": "emergency",
"message": "emergency"
}
{
"time": {
"human": "2015-03-03 13:33:43 +01:00",
"stamp": 1425386023348
},
"level": "debug",
"message": "You can even dump objects.",
"dump": [
{
"test": true,
"woah": "affirmative"
}
]
}
{
"time": {
"human": "2015-03-03 13:33:43 +01:00",
"stamp": 1425386023350
},
"level": "info",
"namespace": "test",
"message": "Namespaces, yay!"
}
{
"time": {
"human": "2015-03-03 13:33:44 +01:00",
"stamp": 1425386024337
},
"level": "debug",
"namespace": "compressed time test",
"message": "0"
}