Comparing version 1.1.4 to 1.1.5
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
function Debug() { | ||
@@ -2,0 +3,0 @@ } |
export function Debug(){ | ||
} | ||
} |
146
index.js
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
require('source-map-support').install(); | ||
@@ -7,3 +8,3 @@ var util = require('util'); | ||
DEBUG: 'cyan', | ||
'WARN ': 'yellow', | ||
WARN: 'yellow', | ||
TRACE: 'magenta', | ||
@@ -15,14 +16,48 @@ ERROR: 'red' | ||
* | ||
* @param stdout | ||
* @param options | ||
* @param stderr | ||
* @param noColors | ||
*/ | ||
function Logger(stdout, stderr, noColors) { | ||
function Logger(options, stderr, noColors) { | ||
/** | ||
* noColors | ||
* @type {boolean} | ||
*/ | ||
this.noColors = false; | ||
/** | ||
* printDate | ||
* @type {boolean} | ||
*/ | ||
this.printDate = false; | ||
/** | ||
* | ||
* @type {boolean} | ||
*/ | ||
this.logEnable = true; | ||
this.noColors = false; | ||
this.stderr = stderr || process.stderr; | ||
this.stdout = stdout || process.stdout; | ||
this.previousStd = this.stdout; | ||
if (noColors != undefined) { | ||
/** | ||
* | ||
* @type {{info: boolean, debug: boolean, trace: boolean, error: boolean, warn: boolean}} | ||
*/ | ||
this.repporting = { | ||
info: true, | ||
debug: true, | ||
trace: true, | ||
error: true, | ||
warn: true, | ||
}; | ||
this.setStderr(stderr || process.stderr); | ||
this.setStdout(process.stdout); | ||
if (typeof options === "object") { | ||
if (typeof options.write === "function") { | ||
var stdout = options; | ||
this.setStdout(stdout); | ||
} | ||
else { | ||
this.setSettings(options); | ||
} | ||
} | ||
if (noColors !== undefined) { | ||
this.setNoColors(noColors); | ||
} | ||
this.previousStd = this.stdout; | ||
} | ||
@@ -35,2 +70,3 @@ /** | ||
Logger.prototype.debug = function (data) { | ||
//if(!this.repporting.debug) return this; | ||
var args = []; | ||
@@ -40,4 +76,2 @@ for (var _i = 1; _i < arguments.length; _i++) { | ||
} | ||
if (!this.logEnable) | ||
return this; | ||
return this.write(this.stdout, 'DEBUG', data, args); | ||
@@ -51,2 +85,3 @@ }; | ||
Logger.prototype.info = function (data) { | ||
//if(!this.repporting.info) return this; | ||
var args = []; | ||
@@ -56,4 +91,2 @@ for (var _i = 1; _i < arguments.length; _i++) { | ||
} | ||
if (!this.logEnable) | ||
return this; | ||
return this.write(this.stdout, 'INFO', data, args); | ||
@@ -67,2 +100,3 @@ }; | ||
Logger.prototype.warn = function (data) { | ||
//if(!this.repporting.warn) return this; | ||
var args = []; | ||
@@ -72,5 +106,3 @@ for (var _i = 1; _i < arguments.length; _i++) { | ||
} | ||
if (!this.logEnable) | ||
return this; | ||
return this.write(this.stderr, 'WARN ', data, args); | ||
return this.write(this.stderr, 'WARN', data, args); | ||
}; | ||
@@ -85,2 +117,3 @@ /** | ||
Logger.prototype.error = function (data) { | ||
//if(!this.repporting.error) return this; | ||
var args = []; | ||
@@ -90,4 +123,2 @@ for (var _i = 1; _i < arguments.length; _i++) { | ||
} | ||
if (!this.logEnable) | ||
return this; | ||
return this.write(this.stderr, "ERROR", data, args); | ||
@@ -102,2 +133,3 @@ }; | ||
Logger.prototype.trace = function (data) { | ||
//if(!this.repporting.trace) return this; | ||
var args = []; | ||
@@ -107,4 +139,2 @@ for (var _i = 1; _i < arguments.length; _i++) { | ||
} | ||
if (!this.logEnable) | ||
return this; | ||
var stack = '\n' + this.colorize(Logger.createStack(), LOG_COLORS.TRACE) + "\n"; | ||
@@ -121,2 +151,4 @@ args.push(stack); //push StackTrace | ||
return this; | ||
if (this.repporting[this.previousTypeTrace] === false) | ||
return this; | ||
this.previousStd.write(this.colorize(Logger.createStack(), "gray") + '\n\n'); | ||
@@ -132,4 +164,7 @@ return this; | ||
return this; | ||
if (this.repporting[this.previousTypeTrace] === false) | ||
return this; | ||
var stackTrace = Logger.createStack(); | ||
var stackLine = stackTrace.split('\n')[0]; | ||
/* istanbul ignore else */ | ||
if (stackLine.indexOf('(') > -1) { | ||
@@ -149,2 +184,3 @@ var line = '\tat (' + stackTrace.split('\n')[0].split('(')[1]; | ||
this.logEnable = true; | ||
return this; | ||
}; | ||
@@ -156,2 +192,3 @@ /** | ||
this.logEnable = false; | ||
return this; | ||
}; | ||
@@ -194,4 +231,20 @@ /** | ||
Logger.prototype.write = function (std, name, data, args) { | ||
this.previousTypeTrace = name.toLocaleLowerCase(); | ||
this.previousStd = std; | ||
name = this.colorize('[' + name + ']', LOG_COLORS[name]); | ||
if (this.logEnable === false) | ||
return this; | ||
if (this.repporting[this.previousTypeTrace] === false) | ||
return this; | ||
var date = ''; | ||
if (this.printDate) { | ||
var currentDate = new Date(); | ||
var year = currentDate.getFullYear(); | ||
var month = ('0' + (currentDate.getMonth() + 1)).slice(-2); | ||
var day = ('0' + (currentDate.getDate())).slice(-2); | ||
var hours = ('0' + (currentDate.getHours())).slice(-2); | ||
var minutes = ('0' + (currentDate.getMinutes())).slice(-2); | ||
var seconds = ('0' + (currentDate.getSeconds())).slice(-2); | ||
date = "[" + year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "]"; | ||
} | ||
name = this.colorize(date + '[' + (name + ' ').slice(0, 5) + ']', LOG_COLORS[name]); | ||
var message = Logger.createMessage(data, args); | ||
@@ -203,2 +256,51 @@ this.previousStd.write(name + message + '\n'); | ||
* | ||
* @param options | ||
*/ | ||
Logger.prototype.setSettings = function (options) { | ||
/* istanbul ignore else */ | ||
if (options.noColors !== undefined) { | ||
this.setNoColors(options.noColors); | ||
} | ||
/* istanbul ignore else */ | ||
if (options.printDate !== undefined) { | ||
this.setPrintDate(options.printDate); | ||
} | ||
/* istanbul ignore else */ | ||
if (options.repporting !== undefined) { | ||
this.setRepporting(options.repporting); | ||
} | ||
/* istanbul ignore else */ | ||
if (options.stdout !== undefined) { | ||
this.setStdout(options.stdout); | ||
} | ||
/* istanbul ignore else */ | ||
if (options.stderr !== undefined) { | ||
this.setStderr(options.stderr); | ||
} | ||
return this; | ||
}; | ||
/** | ||
* | ||
* @param print | ||
* @returns {Logger} | ||
*/ | ||
Logger.prototype.setPrintDate = function (print) { | ||
this.printDate = !!print; | ||
return this; | ||
}; | ||
/** | ||
* Change error repporting. | ||
* @param options | ||
* @returns {Logger} | ||
*/ | ||
Logger.prototype.setRepporting = function (options) { | ||
for (var key in this.repporting) { | ||
if (options[key] !== undefined) { | ||
this.repporting[key] = !!options[key]; | ||
} | ||
} | ||
return this; | ||
}; | ||
/** | ||
* | ||
* @param name | ||
@@ -244,5 +346,7 @@ * @param data | ||
var array = stack.split('\n'); | ||
/* istanbul ignore else */ | ||
if (array[0].indexOf("Logger.") > -1) { | ||
array.splice(0, 1); | ||
} | ||
/* istanbul ignore else */ | ||
if (array[0].indexOf("Logger.") > -1) { | ||
@@ -263,3 +367,3 @@ array.splice(0, 1); | ||
return Logger; | ||
})(); | ||
}()); | ||
var $log = new Logger(); | ||
@@ -266,0 +370,0 @@ $log.Logger = Logger; |
218
index.ts
@@ -11,3 +11,3 @@ require('source-map-support').install(); | ||
DEBUG: 'cyan', | ||
'WARN ':'yellow', | ||
WARN: 'yellow', | ||
TRACE: 'magenta', | ||
@@ -17,27 +17,78 @@ ERROR: 'red' | ||
class Logger{ | ||
class Logger { | ||
/** | ||
* Logger instance | ||
*/ | ||
public Logger; //Logger Class reference | ||
/** | ||
* noColors | ||
* @type {boolean} | ||
*/ | ||
private noColors: boolean = false; | ||
/** | ||
* printDate | ||
* @type {boolean} | ||
*/ | ||
private printDate: boolean = false; | ||
/** | ||
* | ||
*/ | ||
private stderr: WritableStream; | ||
/** | ||
* | ||
*/ | ||
private stdout: WritableStream; | ||
/** | ||
* | ||
*/ | ||
private previousStd: WritableStream; | ||
/** | ||
* | ||
*/ | ||
private previousTypeTrace: string; | ||
/** | ||
* | ||
* @type {boolean} | ||
*/ | ||
private logEnable: boolean = true; | ||
/** | ||
* | ||
* @type {{info: boolean, debug: boolean, trace: boolean, error: boolean, warn: boolean}} | ||
*/ | ||
private repporting: ILoggerRepporting = { | ||
info: true, | ||
debug: true, | ||
trace: true, | ||
error: true, | ||
warn: true, | ||
}; | ||
private logEnable = true; | ||
private noColors = false; | ||
private stderr:WritableStream; | ||
private stdout:WritableStream; | ||
private previousStd:WritableStream; | ||
/** | ||
* | ||
* @param stdout | ||
* @param options | ||
* @param stderr | ||
* @param noColors | ||
*/ | ||
constructor(stdout?:WritableStream, stderr?:WritableStream, noColors?:boolean){ | ||
constructor(options?: WritableStream | ILoggerOptions, stderr?: WritableStream, noColors?: boolean){ | ||
this.stderr = stderr || process.stderr; | ||
this.stdout = stdout || process.stdout; | ||
this.setStderr(stderr || process.stderr); | ||
this.setStdout(process.stdout); | ||
this.previousStd = this.stdout; | ||
if (typeof options === "object") { | ||
if(noColors != undefined){ | ||
if (typeof (<any>options).write === "function") { | ||
let stdout = <WritableStream> options; | ||
this.setStdout(stdout); | ||
} else { | ||
this.setSettings(<ILoggerOptions> options); | ||
} | ||
} | ||
if (noColors !== undefined) { | ||
this.setNoColors(noColors); | ||
} | ||
this.previousStd = this.stdout; | ||
} | ||
@@ -50,4 +101,4 @@ | ||
*/ | ||
public debug(data, ...args):Logger{ | ||
if(!this.logEnable) return this; | ||
public debug(data, ...args): Logger { | ||
//if(!this.repporting.debug) return this; | ||
@@ -62,4 +113,4 @@ return this.write(this.stdout, 'DEBUG', data, args); | ||
*/ | ||
public info(data, ...args):Logger{ | ||
if(!this.logEnable) return this; | ||
public info(data, ...args): Logger { | ||
//if(!this.repporting.info) return this; | ||
@@ -74,6 +125,6 @@ return this.write(this.stdout, 'INFO', data, args); | ||
*/ | ||
public warn(data, ...args):Logger{ | ||
if(!this.logEnable) return this; | ||
public warn(data, ...args): Logger { | ||
//if(!this.repporting.warn) return this; | ||
return this.write(this.stderr, 'WARN ', data, args); | ||
return this.write(this.stderr, 'WARN', data, args); | ||
} | ||
@@ -88,4 +139,4 @@ | ||
*/ | ||
public error(data, ...args):Logger{ | ||
if(!this.logEnable) return this; | ||
public error(data, ...args): Logger { | ||
//if(!this.repporting.error) return this; | ||
@@ -101,4 +152,4 @@ return this.write(this.stderr, "ERROR", data, args); | ||
*/ | ||
public trace(data, ...args):Logger{ | ||
if(!this.logEnable) return this; | ||
public trace(data, ...args): Logger { | ||
//if(!this.repporting.trace) return this; | ||
@@ -116,4 +167,5 @@ var stack = '\n' + this.colorize(Logger.createStack(), LOG_COLORS.TRACE) + "\n"; | ||
*/ | ||
public withTrace():Logger{ | ||
public withTrace(): Logger { | ||
if(!this.logEnable) return this; | ||
if(this.repporting[this.previousTypeTrace] === false) return this; | ||
@@ -129,4 +181,5 @@ this.previousStd.write(this.colorize(Logger.createStack(), "gray") + '\n\n'); | ||
*/ | ||
public withLine():Logger{ | ||
if(!this.logEnable) return this; | ||
public withLine(): Logger { | ||
if (!this.logEnable) return this; | ||
if (this.repporting[this.previousTypeTrace] === false) return this; | ||
@@ -136,5 +189,6 @@ var stackTrace = Logger.createStack(); | ||
/* istanbul ignore else */ | ||
if(stackLine.indexOf('(') > -1){ | ||
var line = '\tat (' + stackTrace.split('\n')[0].split('(')[1]; | ||
}else{ | ||
} else { | ||
line = stackLine; | ||
@@ -150,4 +204,5 @@ } | ||
*/ | ||
public start(){ | ||
public start(): Logger { | ||
this.logEnable = true; | ||
return this; | ||
} | ||
@@ -158,4 +213,5 @@ | ||
*/ | ||
public stop(){ | ||
public stop(): Logger { | ||
this.logEnable = false; | ||
return this; | ||
} | ||
@@ -168,3 +224,3 @@ | ||
*/ | ||
public setNoColors (value:boolean):Logger { | ||
public setNoColors(value: boolean): Logger { | ||
this.noColors = value; | ||
@@ -179,3 +235,3 @@ return this; | ||
*/ | ||
public setStdout(std:WritableStream):Logger{ | ||
public setStdout(std: WritableStream): Logger { | ||
this.stdout = std; | ||
@@ -190,3 +246,3 @@ return this; | ||
*/ | ||
public setStderr(std:WritableStream):Logger{ | ||
public setStderr(std: WritableStream):Logger{ | ||
this.stderr = std; | ||
@@ -204,7 +260,27 @@ return this; | ||
*/ | ||
private write(std:WritableStream, name:string, data:any, args:any[]):Logger{ | ||
private write(std: WritableStream, name: string, data: any, args: any[]): Logger { | ||
this.previousTypeTrace = name.toLocaleLowerCase(); | ||
this.previousStd = std; | ||
name = this.colorize('['+ name +']', LOG_COLORS[name]); | ||
if (this.logEnable === false) return this; | ||
if (this.repporting[this.previousTypeTrace] === false) return this; | ||
let date = ''; | ||
if (this.printDate) { | ||
let currentDate = new Date(); | ||
let year = currentDate.getFullYear(); | ||
let month = ('0' + (currentDate.getMonth()+1)).slice(-2); | ||
let day = ('0' + (currentDate.getDate())).slice(-2); | ||
let hours = ('0' + (currentDate.getHours())).slice(-2); | ||
let minutes = ('0' + (currentDate.getMinutes())).slice(-2); | ||
let seconds = ('0' + (currentDate.getSeconds())).slice(-2); | ||
date = `[${year}-${month}-${day} ${hours}:${minutes}:${seconds}]`; | ||
} | ||
name = this.colorize(date + '['+ (name + ' ').slice(0, 5) +']', LOG_COLORS[name]); | ||
var message = Logger.createMessage(data, args); | ||
@@ -219,2 +295,64 @@ | ||
* | ||
* @param options | ||
*/ | ||
public setSettings(options: ILoggerOptions): Logger { | ||
/* istanbul ignore else */ | ||
if (options.noColors !== undefined) { | ||
this.setNoColors(options.noColors); | ||
} | ||
/* istanbul ignore else */ | ||
if (options.printDate !== undefined){ | ||
this.setPrintDate(options.printDate); | ||
} | ||
/* istanbul ignore else */ | ||
if (options.repporting !== undefined) { | ||
this.setRepporting(options.repporting); | ||
} | ||
/* istanbul ignore else */ | ||
if (options.stdout !== undefined){ | ||
this.setStdout(options.stdout); | ||
} | ||
/* istanbul ignore else */ | ||
if (options.stderr !== undefined){ | ||
this.setStderr(options.stderr); | ||
} | ||
return this; | ||
} | ||
/** | ||
* | ||
* @param print | ||
* @returns {Logger} | ||
*/ | ||
public setPrintDate(print: boolean): Logger { | ||
this.printDate = !!print; | ||
return this; | ||
} | ||
/** | ||
* Change error repporting. | ||
* @param options | ||
* @returns {Logger} | ||
*/ | ||
public setRepporting(options: ILoggerRepporting): Logger { | ||
for(let key in this.repporting) { | ||
if(options[key] !== undefined) { | ||
this.repporting[key] = !!options[key]; | ||
} | ||
} | ||
return this; | ||
} | ||
/** | ||
* | ||
* @param name | ||
@@ -260,6 +398,7 @@ * @param data | ||
*/ | ||
public static createStack():string{ | ||
public static createStack(): string { | ||
var stack:string = new Error().stack.replace('Error\n',''); | ||
var array:string[] = stack.split('\n'); | ||
/* istanbul ignore else */ | ||
if(array[0].indexOf("Logger.") > -1){ //remove current function | ||
@@ -269,2 +408,3 @@ array.splice(0, 1); | ||
/* istanbul ignore else */ | ||
if(array[0].indexOf("Logger.") > -1){ //remove caller | ||
@@ -288,4 +428,4 @@ array.splice(0, 1); | ||
var $log = new Logger(); | ||
let $log = new Logger(); | ||
export = $log; | ||
$log.Logger = Logger; |
{ | ||
"name": "log-debug", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "Provide logger written in TypeScript for TypeScript or JavaScript.", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "typings install && tsc", | ||
"preinstall": "typings install", | ||
"install": "tsc", | ||
"pretest": "typings install && tsc", | ||
"test": "mocha" | ||
"postinstall": "tsc", | ||
"test": "mocha --reporter spec --check-leaks --bail test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" | ||
}, | ||
@@ -30,2 +30,3 @@ "repository": { | ||
"chai": "^2.1.0", | ||
"istanbul": "^0.4.2", | ||
"mocha": "^2.4.5", | ||
@@ -32,0 +33,0 @@ "typescript": "^1.7.5" |
@@ -1,3 +0,6 @@ | ||
# log-debug [![Build Status](https://travis-ci.org/Romakita/httpexceptions.svg?branch=master)](https://travis-ci.org/Romakita/httpexceptions) | ||
# log-debug | ||
[![Build Status](https://travis-ci.org/Romakita/log-debug.svg?branch=master)](https://travis-ci.org/Romakita/log-debug) | ||
[![Coverage Status](https://coveralls.io/repos/github/Romakita/log-debug/badge.svg?branch=master)](https://coveralls.io/github/Romakita/log-debug?branch=master) | ||
> Provide logger written in TypeScript for TypeScript or JavaScript. | ||
@@ -37,5 +40,7 @@ | ||
### new Logger(stdout[, stderr [, noColors]]) | ||
### new Logger(options) | ||
**stdout**: `NodeJS.WritableStream` | ||
**stderr**: `NodeJS.WritableStream` | ||
**noColors**: `boolean` | ||
**options**: `ILoggerOptions` | ||
@@ -66,2 +71,36 @@ 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`. | ||
##### 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. | ||
``` typescript | ||
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) | ||
@@ -68,0 +107,0 @@ **args**: `any[]` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
42415
13
992
199
4