compact-log

A file and stdout logger, supports namespaces, colors, compressed time strings, json dumps and 8 log levels.

Usage no npm install needed!

<script type="module">
  import compactLog from 'https://cdn.skypack.dev/compact-log';
</script>

README

compact-log

MIT License Code Climate NPM Downloads NPM Dependencies

A file and stdout logger for node.js, supports namespaces, colors, compressed time strings and 8 log levels.

example

screenshot

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);

installation

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.

initialization

    var Log = require('compact-log');
    var log = new Log({/*options*/});
    log.info('Hello world!');

options

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

Accepted values for these options

  • 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

methods

.emergency(args), .emer, .mrgc, .em

Logs a message with logLevel 1 = 'emergency'.

.alert(args), .aler, .alrt, .al

Logs a message with logLevel 2 = 'alert'.

.critical(args), .crit, .crtc, .cr

Logs a message with logLevel 3 = 'critical'.

.error(args), .erro, .rror, .er

Logs a message with logLevel 4 = 'error'.

.warning(args), .warn, .wrng, .wa

Logs a message with logLevel 5 = 'warning'.

.notice(args), .noti, .notc, .no

Logs a message with logLevel 6 = 'notice'.

.info(args), .in

Logs a message with logLevel 7 = 'info'.

.debug(args), debu, .dbug, .de

Logs a message with logLevel 8 = 'debug'.

.separator(text, logLevel), .sepa, .se

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.

.createNamespace(options)

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!

Example File Log

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.

Usual JSON:

{"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"}

Pretty JSON:

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"
}

License

The MIT License (MIT)

Copyright (c) 2015 Florian Wendelborn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.