Comparing version 1.1.35 to 1.2.4
@@ -1,2 +0,2 @@ | ||
/* Brolog version 1.1.35 */ | ||
/* Brolog version 1.2.4 */ | ||
(function (global, factory) { | ||
@@ -8,12 +8,2 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
/*! | ||
* Brolog JavaScript Library v1.1.0 | ||
* https://github.com/zixia/brolog | ||
* | ||
* Copyright Huan LI <zixia@zixia.net> | ||
* Released under the ISC license | ||
* https://github.com/zixia/brolog/blob/master/LICENSE | ||
* | ||
* Date: 2016-07 | ||
*/ | ||
var LogLevel; | ||
@@ -28,9 +18,9 @@ (function (LogLevel) { | ||
})(LogLevel || (LogLevel = {})); | ||
const nullLogger = { | ||
error() { }, | ||
warn() { }, | ||
info() { }, | ||
verbose() { }, | ||
silly() { }, | ||
}; | ||
// export const nullLogger: Loggable = { | ||
// error() { /* nop */ }, | ||
// warn() { /* nop */ }, | ||
// info() { /* nop */ }, | ||
// verbose() { /* nop */ }, | ||
// silly() { /* nop */ }, | ||
// } | ||
class Brolog { | ||
@@ -41,2 +31,3 @@ constructor() { | ||
this.prefix(Brolog.globalPrefix); | ||
this.printText = this.printTextDefault; | ||
} | ||
@@ -60,45 +51,52 @@ /** | ||
} | ||
static enableLogging(log) { | ||
return Brolog.instance().enableLogging(log); | ||
static enableLogging(logSetting) { | ||
return Brolog.instance().enableLogging(logSetting); | ||
} | ||
enableLogging(log) { | ||
this.verbose('Brolog', 'enableLogging(%s)', log); | ||
const loggerMethodList = [ | ||
'error', | ||
'warn', | ||
'info', | ||
'verbose', | ||
'silly', | ||
]; | ||
if (log === false) { | ||
enableLogging(logSetting) { | ||
this.verbose('Brolog', 'enableLogging(%s)', logSetting); | ||
// const loggerMethodList = [ | ||
// 'error', | ||
// 'warn', | ||
// 'info', | ||
// 'verbose', | ||
// 'silly', | ||
// ] | ||
if (logSetting === false) { | ||
this.silly('Brolog', 'enableLogging() disabled'); | ||
loggerMethodList.forEach(m => { | ||
this[m] = nullLogger[m]; | ||
}); | ||
// loggerMethodList.forEach(m => { | ||
// this[m] = nullLogger[m] | ||
// }) | ||
this.printText = function () { }; | ||
} | ||
else if (log === true) { | ||
else if (logSetting === true) { | ||
this.silly('Brolog', 'enableLogging() enabled: restore Brolog instance'); | ||
const restore = new Brolog(); | ||
loggerMethodList.forEach(m => { | ||
this[m] = restore[m]; | ||
}); | ||
// const restore = new Brolog() | ||
// loggerMethodList.forEach(m => { | ||
// this[m] = restore[m] | ||
// }) | ||
this.printText = this.printTextDefault; | ||
// } else if (typeof log.verbose === 'function') { | ||
// this.silly('Brolog', 'enableLogging() enabled: using provided logger') | ||
// for (const method of loggerMethodList) { | ||
// this[method] = () => { | ||
// // In order to compatible with winston, | ||
// // we need to change the args from | ||
// // brolog.info('Main', 'Hello %s', 'world') | ||
// // to | ||
// // log.info('Main Hello %s', 'world') | ||
// const argList: string[] = Array.from(arguments) | ||
// if (argList.length > 1) { | ||
// const module = argList.shift() | ||
// argList[0] = `${module} ` + argList[0] | ||
// } | ||
// return Reflect.apply(log[method], log, argList) | ||
// } | ||
// } | ||
} | ||
else if (typeof log.verbose === 'function') { | ||
this.silly('Brolog', 'enableLogging() enabled: using provided logger'); | ||
loggerMethodList.forEach(method => { | ||
const newMethod = log[method].bind(log); | ||
this[method] = () => { | ||
// In order to compatible with winston, | ||
// we need to change the args from | ||
// brolog.info('Main', 'Hello %s', 'world') | ||
// to | ||
// log.info('Main Hello %s', 'world') | ||
const argList = Array.from(arguments); | ||
if (argList.length > 1) { | ||
const module = argList.shift(); | ||
argList[0] = `${module} ` + argList[0]; | ||
} | ||
return Reflect.apply(newMethod, this, argList); | ||
}; | ||
}); | ||
else if (typeof logSetting === 'function') { | ||
this.silly('Brolog', 'enableLogging() enabled: using provided log function'); | ||
this.printText = function (levelTitle, text) { | ||
logSetting(text); | ||
return; | ||
}; | ||
} | ||
@@ -157,2 +155,6 @@ else { | ||
// args[0] = this.timestamp() + args[0] | ||
const text = Reflect.apply(sprintf, null, args); | ||
this.printText(levelTitle, text); | ||
} | ||
printTextDefault(levelTitle, text) { | ||
// Use Reflect at: | ||
@@ -163,11 +165,14 @@ // https://www.keithcirkel.co.uk/metaprogramming-in-es6-part-2-reflect/ | ||
// console.error.apply(console, args) | ||
Reflect.apply(console.error, console, args); | ||
// Reflect.apply(console.error, console, args) | ||
console.error(text); | ||
break; | ||
case 'WARN': | ||
// console.warn.apply(console, args) | ||
Reflect.apply(console.warn, console, args); | ||
// Reflect.apply(console.warn, console, args) | ||
console.warn(text); | ||
break; | ||
case 'INFO': | ||
// console.info.apply(console, args) | ||
Reflect.apply(console.info, console, args); | ||
// Reflect.apply(console.info, console, args) | ||
console.info(text); | ||
break; | ||
@@ -178,3 +183,4 @@ default: | ||
// console.log.apply(console, args) | ||
Reflect.apply(console.log, console, args); | ||
// Reflect.apply(console.log, console, args) | ||
console.log(text); | ||
} | ||
@@ -275,8 +281,34 @@ } | ||
Brolog.globalPrefix = /.*/; // Match all by default | ||
// Credit: https://stackoverflow.com/a/4795914/1123955 | ||
function sprintf() { | ||
const args = arguments; | ||
const text = args[0]; | ||
let i = 1; | ||
return text.replace(/%((%)|s|d)/g, function (m) { | ||
// m is the matched format, e.g. %s, %d | ||
let val = null; | ||
if (m[2]) { | ||
val = m[2]; | ||
} | ||
else { | ||
val = args[i]; | ||
// A switch statement so that the formatter can be extended. Default is %s | ||
switch (m) { | ||
case '%d': | ||
val = parseFloat(val); | ||
if (isNaN(val)) { | ||
val = 0; | ||
} | ||
break; | ||
} | ||
i++; | ||
} | ||
return val; | ||
}); | ||
} | ||
const log = Brolog.instance(); | ||
exports.nullLogger = nullLogger; | ||
exports.Brolog = Brolog; | ||
exports.log = log; | ||
exports['default'] = log; | ||
exports['default'] = Brolog; | ||
@@ -283,0 +315,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,2 +0,2 @@ | ||
/* Brolog version 1.1.35 */ | ||
/* Brolog version 1.2.4 */ | ||
(function (global, factory) { | ||
@@ -8,12 +8,2 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
'use strict'; | ||
/*! | ||
* Brolog JavaScript Library v1.1.0 | ||
* https://github.com/zixia/brolog | ||
* | ||
* Copyright Huan LI <zixia@zixia.net> | ||
* Released under the ISC license | ||
* https://github.com/zixia/brolog/blob/master/LICENSE | ||
* | ||
* Date: 2016-07 | ||
*/ | ||
var LogLevel; | ||
@@ -28,9 +18,9 @@ (function (LogLevel) { | ||
})(LogLevel || (LogLevel = {})); | ||
var nullLogger = { | ||
error: function () { }, | ||
warn: function () { }, | ||
info: function () { }, | ||
verbose: function () { }, | ||
silly: function () { }, | ||
}; | ||
// export const nullLogger: Loggable = { | ||
// error() { /* nop */ }, | ||
// warn() { /* nop */ }, | ||
// info() { /* nop */ }, | ||
// verbose() { /* nop */ }, | ||
// silly() { /* nop */ }, | ||
// } | ||
var Brolog = /** @class */ (function () { | ||
@@ -41,2 +31,3 @@ function Brolog() { | ||
this.prefix(Brolog.globalPrefix); | ||
this.printText = this.printTextDefault; | ||
} | ||
@@ -60,46 +51,52 @@ /** | ||
}; | ||
Brolog.enableLogging = function (log) { | ||
return Brolog.instance().enableLogging(log); | ||
Brolog.enableLogging = function (logSetting) { | ||
return Brolog.instance().enableLogging(logSetting); | ||
}; | ||
Brolog.prototype.enableLogging = function (log) { | ||
var _this = this; | ||
this.verbose('Brolog', 'enableLogging(%s)', log); | ||
var loggerMethodList = [ | ||
'error', | ||
'warn', | ||
'info', | ||
'verbose', | ||
'silly', | ||
]; | ||
if (log === false) { | ||
Brolog.prototype.enableLogging = function (logSetting) { | ||
this.verbose('Brolog', 'enableLogging(%s)', logSetting); | ||
// const loggerMethodList = [ | ||
// 'error', | ||
// 'warn', | ||
// 'info', | ||
// 'verbose', | ||
// 'silly', | ||
// ] | ||
if (logSetting === false) { | ||
this.silly('Brolog', 'enableLogging() disabled'); | ||
loggerMethodList.forEach(function (m) { | ||
_this[m] = nullLogger[m]; | ||
}); | ||
// loggerMethodList.forEach(m => { | ||
// this[m] = nullLogger[m] | ||
// }) | ||
this.printText = function () { }; | ||
} | ||
else if (log === true) { | ||
else if (logSetting === true) { | ||
this.silly('Brolog', 'enableLogging() enabled: restore Brolog instance'); | ||
var restore_1 = new Brolog(); | ||
loggerMethodList.forEach(function (m) { | ||
_this[m] = restore_1[m]; | ||
}); | ||
// const restore = new Brolog() | ||
// loggerMethodList.forEach(m => { | ||
// this[m] = restore[m] | ||
// }) | ||
this.printText = this.printTextDefault; | ||
// } else if (typeof log.verbose === 'function') { | ||
// this.silly('Brolog', 'enableLogging() enabled: using provided logger') | ||
// for (const method of loggerMethodList) { | ||
// this[method] = () => { | ||
// // In order to compatible with winston, | ||
// // we need to change the args from | ||
// // brolog.info('Main', 'Hello %s', 'world') | ||
// // to | ||
// // log.info('Main Hello %s', 'world') | ||
// const argList: string[] = Array.from(arguments) | ||
// if (argList.length > 1) { | ||
// const module = argList.shift() | ||
// argList[0] = `${module} ` + argList[0] | ||
// } | ||
// return Reflect.apply(log[method], log, argList) | ||
// } | ||
// } | ||
} | ||
else if (typeof log.verbose === 'function') { | ||
this.silly('Brolog', 'enableLogging() enabled: using provided logger'); | ||
loggerMethodList.forEach(function (method) { | ||
var newMethod = log[method].bind(log); | ||
_this[method] = function () { | ||
// In order to compatible with winston, | ||
// we need to change the args from | ||
// brolog.info('Main', 'Hello %s', 'world') | ||
// to | ||
// log.info('Main Hello %s', 'world') | ||
var argList = Array.from(arguments); | ||
if (argList.length > 1) { | ||
var module_1 = argList.shift(); | ||
argList[0] = module_1 + " " + argList[0]; | ||
} | ||
return Reflect.apply(newMethod, _this, argList); | ||
}; | ||
}); | ||
else if (typeof logSetting === 'function') { | ||
this.silly('Brolog', 'enableLogging() enabled: using provided log function'); | ||
this.printText = function (levelTitle, text) { | ||
logSetting(text); | ||
return; | ||
}; | ||
} | ||
@@ -158,2 +155,6 @@ else { | ||
// args[0] = this.timestamp() + args[0] | ||
var text = Reflect.apply(sprintf, null, args); | ||
this.printText(levelTitle, text); | ||
}; | ||
Brolog.prototype.printTextDefault = function (levelTitle, text) { | ||
// Use Reflect at: | ||
@@ -164,11 +165,14 @@ // https://www.keithcirkel.co.uk/metaprogramming-in-es6-part-2-reflect/ | ||
// console.error.apply(console, args) | ||
Reflect.apply(console.error, console, args); | ||
// Reflect.apply(console.error, console, args) | ||
console.error(text); | ||
break; | ||
case 'WARN': | ||
// console.warn.apply(console, args) | ||
Reflect.apply(console.warn, console, args); | ||
// Reflect.apply(console.warn, console, args) | ||
console.warn(text); | ||
break; | ||
case 'INFO': | ||
// console.info.apply(console, args) | ||
Reflect.apply(console.info, console, args); | ||
// Reflect.apply(console.info, console, args) | ||
console.info(text); | ||
break; | ||
@@ -179,3 +183,4 @@ default: | ||
// console.log.apply(console, args) | ||
Reflect.apply(console.log, console, args); | ||
// Reflect.apply(console.log, console, args) | ||
console.log(text); | ||
} | ||
@@ -317,7 +322,33 @@ }; | ||
Brolog.globalPrefix = /.*/; // Match all by default | ||
// Credit: https://stackoverflow.com/a/4795914/1123955 | ||
function sprintf() { | ||
var args = arguments; | ||
var text = args[0]; | ||
var i = 1; | ||
return text.replace(/%((%)|s|d)/g, function (m) { | ||
// m is the matched format, e.g. %s, %d | ||
var val = null; | ||
if (m[2]) { | ||
val = m[2]; | ||
} | ||
else { | ||
val = args[i]; | ||
// A switch statement so that the formatter can be extended. Default is %s | ||
switch (m) { | ||
case '%d': | ||
val = parseFloat(val); | ||
if (isNaN(val)) { | ||
val = 0; | ||
} | ||
break; | ||
} | ||
i++; | ||
} | ||
return val; | ||
}); | ||
} | ||
var log = Brolog.instance(); | ||
exports.nullLogger = nullLogger; | ||
exports.Brolog = Brolog; | ||
exports.log = log; | ||
exports['default'] = log; | ||
exports['default'] = Brolog; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
@@ -324,0 +355,0 @@ }))); |
@@ -1,13 +0,4 @@ | ||
/*! | ||
* Brolog JavaScript Library v1.1.0 | ||
* https://github.com/zixia/brolog | ||
* | ||
* Copyright Huan LI <zixia@zixia.net> | ||
* Released under the ISC license | ||
* https://github.com/zixia/brolog/blob/master/LICENSE | ||
* | ||
* Date: 2016-07 | ||
*/ | ||
export declare type LogLevelName = 'silent' | 'error' | 'warn' | 'info' | 'verbose' | 'silly'; | ||
export declare type LogLevelTitle = 'ERR' | 'WARN' | 'INFO' | 'VERB' | 'SILL'; | ||
export declare type PrintTextFunc = (text: string) => void; | ||
export interface Loggable { | ||
@@ -20,3 +11,2 @@ error(prefix: string, message: string, ...args: any[]): void; | ||
} | ||
export declare const nullLogger: Loggable; | ||
export declare class Brolog implements Loggable { | ||
@@ -29,2 +19,3 @@ private static globalInstance; | ||
private prefixFilter; | ||
printText: (levelTitle: LogLevelTitle, text: string) => void; | ||
constructor(); | ||
@@ -35,4 +26,4 @@ /** | ||
static instance(levelName?: LogLevelName, prefix?: string | RegExp): Brolog; | ||
static enableLogging(log: boolean | Loggable): Brolog; | ||
enableLogging(log: boolean | Loggable): Brolog; | ||
static enableLogging(logSetting: boolean | PrintTextFunc): Brolog; | ||
enableLogging(logSetting: boolean | PrintTextFunc): Brolog; | ||
static prefix(filter?: string | RegExp): RegExp; | ||
@@ -43,2 +34,3 @@ prefix(filter?: string | RegExp): RegExp; | ||
private log(levelTitle, prefix, message); | ||
printTextDefault(levelTitle: LogLevelTitle, text: string): void; | ||
static error(prefix: string, ...args: any[]): void; | ||
@@ -58,2 +50,2 @@ error(prefix: string, ...args: any[]): void; | ||
export declare const log: Brolog; | ||
export default log; | ||
export default Brolog; |
@@ -1,11 +0,1 @@ | ||
/*! | ||
* Brolog JavaScript Library v1.1.0 | ||
* https://github.com/zixia/brolog | ||
* | ||
* Copyright Huan LI <zixia@zixia.net> | ||
* Released under the ISC license | ||
* https://github.com/zixia/brolog/blob/master/LICENSE | ||
* | ||
* Date: 2016-07 | ||
*/ | ||
var LogLevel; | ||
@@ -20,9 +10,9 @@ (function (LogLevel) { | ||
})(LogLevel || (LogLevel = {})); | ||
export const nullLogger = { | ||
error() { }, | ||
warn() { }, | ||
info() { }, | ||
verbose() { }, | ||
silly() { }, | ||
}; | ||
// export const nullLogger: Loggable = { | ||
// error() { /* nop */ }, | ||
// warn() { /* nop */ }, | ||
// info() { /* nop */ }, | ||
// verbose() { /* nop */ }, | ||
// silly() { /* nop */ }, | ||
// } | ||
export class Brolog { | ||
@@ -33,2 +23,3 @@ constructor() { | ||
this.prefix(Brolog.globalPrefix); | ||
this.printText = this.printTextDefault; | ||
} | ||
@@ -52,45 +43,52 @@ /** | ||
} | ||
static enableLogging(log) { | ||
return Brolog.instance().enableLogging(log); | ||
static enableLogging(logSetting) { | ||
return Brolog.instance().enableLogging(logSetting); | ||
} | ||
enableLogging(log) { | ||
this.verbose('Brolog', 'enableLogging(%s)', log); | ||
const loggerMethodList = [ | ||
'error', | ||
'warn', | ||
'info', | ||
'verbose', | ||
'silly', | ||
]; | ||
if (log === false) { | ||
enableLogging(logSetting) { | ||
this.verbose('Brolog', 'enableLogging(%s)', logSetting); | ||
// const loggerMethodList = [ | ||
// 'error', | ||
// 'warn', | ||
// 'info', | ||
// 'verbose', | ||
// 'silly', | ||
// ] | ||
if (logSetting === false) { | ||
this.silly('Brolog', 'enableLogging() disabled'); | ||
loggerMethodList.forEach(m => { | ||
this[m] = nullLogger[m]; | ||
}); | ||
// loggerMethodList.forEach(m => { | ||
// this[m] = nullLogger[m] | ||
// }) | ||
this.printText = function () { }; | ||
} | ||
else if (log === true) { | ||
else if (logSetting === true) { | ||
this.silly('Brolog', 'enableLogging() enabled: restore Brolog instance'); | ||
const restore = new Brolog(); | ||
loggerMethodList.forEach(m => { | ||
this[m] = restore[m]; | ||
}); | ||
// const restore = new Brolog() | ||
// loggerMethodList.forEach(m => { | ||
// this[m] = restore[m] | ||
// }) | ||
this.printText = this.printTextDefault; | ||
// } else if (typeof log.verbose === 'function') { | ||
// this.silly('Brolog', 'enableLogging() enabled: using provided logger') | ||
// for (const method of loggerMethodList) { | ||
// this[method] = () => { | ||
// // In order to compatible with winston, | ||
// // we need to change the args from | ||
// // brolog.info('Main', 'Hello %s', 'world') | ||
// // to | ||
// // log.info('Main Hello %s', 'world') | ||
// const argList: string[] = Array.from(arguments) | ||
// if (argList.length > 1) { | ||
// const module = argList.shift() | ||
// argList[0] = `${module} ` + argList[0] | ||
// } | ||
// return Reflect.apply(log[method], log, argList) | ||
// } | ||
// } | ||
} | ||
else if (typeof log.verbose === 'function') { | ||
this.silly('Brolog', 'enableLogging() enabled: using provided logger'); | ||
loggerMethodList.forEach(method => { | ||
const newMethod = log[method].bind(log); | ||
this[method] = () => { | ||
// In order to compatible with winston, | ||
// we need to change the args from | ||
// brolog.info('Main', 'Hello %s', 'world') | ||
// to | ||
// log.info('Main Hello %s', 'world') | ||
const argList = Array.from(arguments); | ||
if (argList.length > 1) { | ||
const module = argList.shift(); | ||
argList[0] = `${module} ` + argList[0]; | ||
} | ||
return Reflect.apply(newMethod, this, argList); | ||
}; | ||
}); | ||
else if (typeof logSetting === 'function') { | ||
this.silly('Brolog', 'enableLogging() enabled: using provided log function'); | ||
this.printText = function (levelTitle, text) { | ||
logSetting(text); | ||
return; | ||
}; | ||
} | ||
@@ -149,2 +147,6 @@ else { | ||
// args[0] = this.timestamp() + args[0] | ||
const text = Reflect.apply(sprintf, null, args); | ||
this.printText(levelTitle, text); | ||
} | ||
printTextDefault(levelTitle, text) { | ||
// Use Reflect at: | ||
@@ -155,11 +157,14 @@ // https://www.keithcirkel.co.uk/metaprogramming-in-es6-part-2-reflect/ | ||
// console.error.apply(console, args) | ||
Reflect.apply(console.error, console, args); | ||
// Reflect.apply(console.error, console, args) | ||
console.error(text); | ||
break; | ||
case 'WARN': | ||
// console.warn.apply(console, args) | ||
Reflect.apply(console.warn, console, args); | ||
// Reflect.apply(console.warn, console, args) | ||
console.warn(text); | ||
break; | ||
case 'INFO': | ||
// console.info.apply(console, args) | ||
Reflect.apply(console.info, console, args); | ||
// Reflect.apply(console.info, console, args) | ||
console.info(text); | ||
break; | ||
@@ -170,3 +175,4 @@ default: | ||
// console.log.apply(console, args) | ||
Reflect.apply(console.log, console, args); | ||
// Reflect.apply(console.log, console, args) | ||
console.log(text); | ||
} | ||
@@ -267,4 +273,31 @@ } | ||
Brolog.globalPrefix = /.*/; // Match all by default | ||
// Credit: https://stackoverflow.com/a/4795914/1123955 | ||
function sprintf() { | ||
const args = arguments; | ||
const text = args[0]; | ||
let i = 1; | ||
return text.replace(/%((%)|s|d)/g, function (m) { | ||
// m is the matched format, e.g. %s, %d | ||
let val = null; | ||
if (m[2]) { | ||
val = m[2]; | ||
} | ||
else { | ||
val = args[i]; | ||
// A switch statement so that the formatter can be extended. Default is %s | ||
switch (m) { | ||
case '%d': | ||
val = parseFloat(val); | ||
if (isNaN(val)) { | ||
val = 0; | ||
} | ||
break; | ||
} | ||
i++; | ||
} | ||
return val; | ||
}); | ||
} | ||
export const log = Brolog.instance(); | ||
export default log; | ||
export default Brolog; | ||
//# sourceMappingURL=brolog.js.map |
{ | ||
"name": "brolog", | ||
"version": "1.1.35", | ||
"version": "1.2.4", | ||
"description": "Npmlog like logger for Browser", | ||
@@ -41,12 +41,14 @@ "main": "bundles/brolog.umd.js", | ||
"devDependencies": { | ||
"@types/node": "^8.0.17", | ||
"@types/node": "^8.0.28", | ||
"@types/sinon": "^2.3.4", | ||
"@types/sinon-test": "^1.0.2", | ||
"jasmine-spec-reporter": "^4.0.0", | ||
"protractor": "^5.1.1", | ||
"rollup": "^0.41.6", | ||
"sinon": "^2.1.0", | ||
"sinon-test": "^1.0.1", | ||
"tap": "^10.3.0", | ||
"ts-node": "^3.0.2", | ||
"tslint": "^5.1.0", | ||
"typescript": "^2.3.2" | ||
"sinon": "^2.4.1", | ||
"sinon-test": "git://github.com/zixia/sinon-test.git#patch-1", | ||
"tap": "^10.7.2", | ||
"ts-node": "^3.3.0", | ||
"tslint": "^5.7.0", | ||
"typescript": "^2.5.2" | ||
}, | ||
@@ -53,0 +55,0 @@ "files": [ |
@@ -11,3 +11,2 @@ /*! | ||
*/ | ||
export type LogLevelName = 'silent' | ||
@@ -26,2 +25,4 @@ | 'error' | ||
export type PrintTextFunc = (text: string) => void | ||
enum LogLevel { | ||
@@ -44,9 +45,9 @@ silent = 0, | ||
export const nullLogger: Loggable = { | ||
error() { /* nop */ }, | ||
warn() { /* nop */ }, | ||
info() { /* nop */ }, | ||
verbose() { /* nop */ }, | ||
silly() { /* nop */ }, | ||
} | ||
// export const nullLogger: Loggable = { | ||
// error() { /* nop */ }, | ||
// warn() { /* nop */ }, | ||
// info() { /* nop */ }, | ||
// verbose() { /* nop */ }, | ||
// silly() { /* nop */ }, | ||
// } | ||
@@ -62,5 +63,9 @@ export class Brolog implements Loggable { | ||
public printText: (levelTitle: LogLevelTitle, text: string) => void | ||
constructor() { | ||
this.level(Brolog.globalLogLevelName) | ||
this.prefix(Brolog.globalPrefix) | ||
this.printText = this.printTextDefault | ||
} | ||
@@ -91,49 +96,56 @@ | ||
public static enableLogging(log: boolean | Loggable): Brolog { | ||
return Brolog.instance().enableLogging(log) | ||
public static enableLogging(logSetting: boolean | PrintTextFunc): Brolog { | ||
return Brolog.instance().enableLogging(logSetting) | ||
} | ||
public enableLogging(log: boolean | Loggable): Brolog { | ||
this.verbose('Brolog', 'enableLogging(%s)', log) | ||
public enableLogging(logSetting: boolean | PrintTextFunc): Brolog { | ||
this.verbose('Brolog', 'enableLogging(%s)', logSetting) | ||
const loggerMethodList = [ | ||
'error', | ||
'warn', | ||
'info', | ||
'verbose', | ||
'silly', | ||
] | ||
// const loggerMethodList = [ | ||
// 'error', | ||
// 'warn', | ||
// 'info', | ||
// 'verbose', | ||
// 'silly', | ||
// ] | ||
if (log === false) { | ||
if (logSetting === false) { | ||
this.silly('Brolog', 'enableLogging() disabled') | ||
loggerMethodList.forEach(m => { | ||
this[m] = nullLogger[m] | ||
}) | ||
// loggerMethodList.forEach(m => { | ||
// this[m] = nullLogger[m] | ||
// }) | ||
this.printText = function () { /* null logger */ } | ||
} else if (log === true) { | ||
} else if (logSetting === true) { | ||
this.silly('Brolog', 'enableLogging() enabled: restore Brolog instance') | ||
const restore = new Brolog() | ||
loggerMethodList.forEach(m => { | ||
this[m] = restore[m] | ||
}) | ||
// const restore = new Brolog() | ||
// loggerMethodList.forEach(m => { | ||
// this[m] = restore[m] | ||
// }) | ||
this.printText = this.printTextDefault | ||
} else if (typeof log.verbose === 'function') { | ||
this.silly('Brolog', 'enableLogging() enabled: using provided logger') | ||
loggerMethodList.forEach(method => { | ||
const newMethod = log[method].bind(log) | ||
this[method] = () => { | ||
// In order to compatible with winston, | ||
// we need to change the args from | ||
// brolog.info('Main', 'Hello %s', 'world') | ||
// to | ||
// log.info('Main Hello %s', 'world') | ||
const argList: string[] = Array.from(arguments) | ||
if (argList.length > 1) { | ||
const module = argList.shift() | ||
argList[0] = `${module} ` + argList[0] | ||
} | ||
return Reflect.apply(newMethod, this, argList) | ||
} | ||
}) | ||
// } else if (typeof log.verbose === 'function') { | ||
// this.silly('Brolog', 'enableLogging() enabled: using provided logger') | ||
// for (const method of loggerMethodList) { | ||
// this[method] = () => { | ||
// // In order to compatible with winston, | ||
// // we need to change the args from | ||
// // brolog.info('Main', 'Hello %s', 'world') | ||
// // to | ||
// // log.info('Main Hello %s', 'world') | ||
// const argList: string[] = Array.from(arguments) | ||
// if (argList.length > 1) { | ||
// const module = argList.shift() | ||
// argList[0] = `${module} ` + argList[0] | ||
// } | ||
// return Reflect.apply(log[method], log, argList) | ||
// } | ||
// } | ||
} else if (typeof logSetting === 'function') { | ||
this.silly('Brolog', 'enableLogging() enabled: using provided log function') | ||
this.printText = function (levelTitle: LogLevelTitle, text: string): void { | ||
logSetting(text) | ||
return | ||
} | ||
} else { | ||
@@ -196,2 +208,7 @@ throw new Error('got invalid logger') | ||
const text = Reflect.apply(sprintf, null, args) | ||
this.printText(levelTitle, text) | ||
} | ||
public printTextDefault(levelTitle: LogLevelTitle, text: string): void { | ||
// Use Reflect at: | ||
@@ -202,11 +219,14 @@ // https://www.keithcirkel.co.uk/metaprogramming-in-es6-part-2-reflect/ | ||
// console.error.apply(console, args) | ||
Reflect.apply(console.error, console, args) | ||
// Reflect.apply(console.error, console, args) | ||
console.error(text) | ||
break | ||
case 'WARN': | ||
// console.warn.apply(console, args) | ||
Reflect.apply(console.warn, console, args) | ||
// Reflect.apply(console.warn, console, args) | ||
console.warn(text) | ||
break | ||
case 'INFO': | ||
// console.info.apply(console, args) | ||
Reflect.apply(console.info, console, args) | ||
// Reflect.apply(console.info, console, args) | ||
console.info(text) | ||
break | ||
@@ -218,6 +238,7 @@ | ||
// console.log.apply(console, args) | ||
Reflect.apply(console.log, console, args) | ||
// Reflect.apply(console.log, console, args) | ||
console.log(text) | ||
} | ||
} | ||
public static error(prefix: string, ...args: any[]): void { | ||
@@ -329,3 +350,30 @@ const instance = Brolog.instance() | ||
// Credit: https://stackoverflow.com/a/4795914/1123955 | ||
function sprintf() { | ||
const args = arguments | ||
const text = args[0] | ||
let i = 1 | ||
return text.replace(/%((%)|s|d)/g, function (m) { | ||
// m is the matched format, e.g. %s, %d | ||
let val = null as any | ||
if (m[2]) { | ||
val = m[2]; | ||
} else { | ||
val = args[i]; | ||
// A switch statement so that the formatter can be extended. Default is %s | ||
switch (m) { | ||
case '%d': | ||
val = parseFloat(val) | ||
if (isNaN(val)) { | ||
val = 0 | ||
} | ||
break | ||
} | ||
i++ | ||
} | ||
return val | ||
}) | ||
} | ||
export const log = Brolog.instance() | ||
export default log | ||
export default Brolog |
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
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
83547
1323
12