Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

log-debug

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

log-debug

Provide logger written in TypeScript for TypeScript or JavaScript.

  • 1.1.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by400%
Maintainers
1
Weekly downloads
 
Created
Source

log-debug

Build Status Coverage Status

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

// custom simple logger
const logger = new $log.Logger(output, errorOutput);

// use it like console
var count = 5;
logger.debug('count: %d', count);
// in stdout.log: count 5

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

// OR

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');                        //[DEBUG] test
$log.debug({test:'test'});                 //[DEBUG] {'test':'test}
$log.debug('Format %j', {test:'test'});    //[DEBUG] Format {'test':'test}
   
//With stacktrace
$log.debug('test').withTrace();            //[DEBUG] test
                                           //        at Context.<anonymous> (/directory/file.ts:80:10)
                                           //        at ...
//With line only
$log.debug('test').withLine();             //[DEBUG] test
                                           //        at (/directory/file.ts:80:10)

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');
  //[TRACE] Show me
  //    at Context.<anonymous> (/projects/log-debug/test/spec/log.spec.ts:251:45)
  //    at callFn (/projects/log-debug/node_modules/mocha/lib/runnable.js:315:21)
  //    at Test.Runnable.run (/projects/log-debug/node_modules/mocha/lib/runnable.js:308:7)
  //    at Runner.runTest (/projects/log-debug/node_modules/mocha/lib/runner.js:422:10)
  //    at /projects/log-debug/node_modules/mocha/lib/runner.js:533:12
  //    at next (/projects/log-debug/node_modules/mocha/lib/runner.js:342:14)
  //    at /projects/log-debug/node_modules/mocha/lib/runner.js:352:7
  //    at next (/projects/log-debug/node_modules/mocha/lib/runner.js:284:14)
  //    at Immediate._onImmediate (/projects/log-debug/node_modules/mocha/lib/runner.js:320:5)
  //    at processImmediate [as _immediateCallback] (timers.js:383:17)
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');                        //[ERROR] test
$log.error({test:'test'});                 //[ERROR] {'test':'test}
$log.error('Format %j', {test:'test'});    //[ERROR] Format {'test':'test}
   
//With stacktrace
$log.error('test').withTrace();            //[ERROR] test
                                           //        at Context.<anonymous> (/directory/file.ts:80:10)
                                           //        at ...
//With line only
$log.error('test').withLine();             //[ERROR] test
                                           //        at (/directory/file.ts:80:10)

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.

Keywords

FAQs

Package last updated on 22 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc