log-debug
Provide logger written in TypeScript for TypeScript or JavaScript.
Features
- Prints info, debug, warn or error message with colorization.
- Prints stack trace with Logger.trace() or Logger.withTrace().
- Change stdout & stderr WritableStream.
Installation
$ npm install -g typings
$ npm install log-debug
API
Class: Logger
The Logger class can be used to create a simple logger with configurable output streams and can be accessed using either import * as $log from 'log-debug'
or var $log = require('debug-log')
.
In TypeScript
import * as $log from "log-debug";
$log.debug('Test').
In JavaScript
var $log = require('log-debug');
$log.debug('Test').
new Logger(stdout[, stderr [, noColors]])
new Logger(options)
stdout: NodeJS.WritableStream
stderr: NodeJS.WritableStream
noColors: boolean
options: ILoggerOptions
Creates a new Logger
by passing one or two writable stream instances. stdout
is a writable stream to print log or info output. stderr
is used for warning or error output. If stderr
isn't passed, the warning and error output will be sent to the stdout`.
import * as Fs from "fs";
import * as $log from "log-debug";
const output = Fs.createWriteStream('./stdout.log');
const errorOutput = Fs.createWriteStream('./stderr.log');
const logger = new $log.Logger(output, errorOutput);
var count = 5;
logger.debug('count: %d', count);
The $log
is a special Logger
whose output is sent to process.stdout
and process.stderr
. It is equivalent to calling:
new $log.Logger(process.stdout, process.stderr);
ILoggerOptions
- printDate : Print a date before log your message. (Date format : YYYY-MM-DD HH:MM:SS)
- noColors : Enable colors
- stdout :
NodeJS.WritableStream
- stderr :
NodeJS.WritableStream
- repporting : Object to enable or disable message printing for each output type (info, debug, trace, warn, error). By default all outputs are set to true.
import * as $log from 'log-debug';
let Logger = new $log.Logger({
printDate: true,
repporting: {
debug: false,
trace: false,
warn: false
}
});
if(env == 'test'){
Logger.setSettings({
printDate: false,
repporting: {
debug: true,
error: true,
warn: true
}
});
}
Logger.debug(...args)
args: any[]
Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf() (the arguments are all passed to util.format()).
import * as $log from 'log-debug';
$log.debug('test');
$log.debug({test:'test'});
$log.debug('Format %j', {test:'test'});
$log.debug('test').withTrace();
$log.debug('test').withLine();
If formatting elements (e.g. %j) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated. See util.format() for more information.
Logger.info(...args)
args: any[]
The Logger.info() function is an alias for Logger.debug().
Logger.trace(...args)
args: any[]
Prints to stderr the string '[TRACE]', followed by the util.format() formatted message and stack trace to the current position in the code.
$log.trace('Show me');
Logger.error(...args)
args: any[]
Prints to stderr with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf() (the arguments are all passed to util.format()).
import * as $log from 'log-debug';
$log.error('test');
$log.error({test:'test'});
$log.error('Format %j', {test:'test'});
$log.error('test').withTrace();
$log.error('test').withLine();
If formatting elements (e.g. %j) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated. See util.format() for more information.
Logger.warn(...args)
args: any[]
The Logger.warn() function is an alias for Logger.error().
Test
$ npm install -g mocha
$ npm test
License
The MIT License (MIT)
Copyright (c) 2016 Romain Lenzotti
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.