Skip to content

Commit

Permalink
enhancement: add prefix filter support
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Apr 30, 2017
1 parent 2e2296a commit 8c6a4e2
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 8 deletions.
25 changes: 24 additions & 1 deletion src/brolog.ts
Expand Up @@ -33,16 +33,35 @@ export enum LogLevel {

export class Brolog {
private static logLevel = LogLevel.info
private static prefixFilter: RegExp = /.*/ // Match all by default

constructor() {
// console.log('constructor')
}

public static instance(levelName?: LevelName): Brolog {
public static instance(
levelName?: LevelName,
prefix?: string | RegExp,
): Brolog {
Brolog.level(levelName)
Brolog.prefix(prefix)
return preSetInstance
}

public prefix(filter?: string | RegExp): RegExp { return Brolog.prefix(filter) }
public static prefix(filter?: string | RegExp): RegExp {
if (filter) {
if (typeof filter === 'string') {
Brolog.prefixFilter = new RegExp(filter, 'i')
} else if (filter instanceof RegExp) {
Brolog.prefixFilter = filter
} else {
throw new Error('unsupported prefix filter')
}
}
return Brolog.prefixFilter
}

public level(levelName?: LevelName) { return Brolog.level(levelName) }
public static level(levelName?: LevelName): LevelName {
if (levelName) {
Expand All @@ -62,6 +81,10 @@ export class Brolog {

// private log(levelTitle: LevelTitle, prefix: string, message: string) { return Brolog.log(levelTitle, prefix, message) }
private static log(levelTitle: LevelTitle, prefix: string, message: string) {
if (!Brolog.prefixFilter.test(prefix)) {
return // skip message not match prefix filter
}

const args = Array.prototype.slice.call(arguments, 3) || []
args.unshift(Brolog.timestamp() + ' ' + levelTitle + ' ' + prefix + ' ' + (message || ''))

Expand Down
85 changes: 78 additions & 7 deletions test/unit/brolog.spec.ts
Expand Up @@ -24,7 +24,7 @@ test('Brolog factory/service/function init test', (t: any) => {
* Raw
*
*/
var l = Brolog.level()
let l = Brolog.level()
t.equal(l, 'info', 'should has default level `info`')

l = Brolog.level(EXPECTED_LEVEL)
Expand All @@ -35,11 +35,11 @@ test('Brolog factory/service/function init test', (t: any) => {
* Instance
*
*/
var LogInstance = Brolog.instance(EXPECTED_LEVEL)
t.equal(typeof LogInstance, 'object', 'should return a object class when we call Brolog as instance')
t.equal(LogInstance, log, 'should return a instance as default exported log')
const logInstance = Brolog.instance(EXPECTED_LEVEL)
t.equal(typeof logInstance, 'object', 'should return a object class when we call Brolog as instance')
t.equal(logInstance, log, 'should return a instance as default exported log')

var ll = log.level()
let ll = log.level()
t.equal(ll, EXPECTED_LEVEL, 'should has current level as EXPECTED_LEVEL after factory & class init')

/**
Expand Down Expand Up @@ -93,12 +93,12 @@ test('Brolog log level test', t => {
* because monkey patch is not recover when it finish
*
*/
test('Brolog filter test', t => {
test('Brolog level filter test', t => {
const logFuncList = [
'error'
, 'warn'
, 'info'
, 'log'
, 'log',
]

let log2: Brolog
Expand Down Expand Up @@ -160,3 +160,74 @@ test('Brolog filter test', t => {
logger.silly('Test', 'silly message')
}
})

test('Brolog prefix filter test', t => {
const logFuncList = [
'error'
, 'warn'
, 'info'
, 'log',
]

log.level('info')
log.prefix(/Show/)

sinon.test(function() {
logFuncList.forEach(logFunc => this.stub(console, logFunc))
doLogHide(log)
t.equal(console.error['callCount'] , 0, 'should call error 0 time with prefix Hide')
t.equal(console.warn['callCount'] , 0, 'should call warn 0 time with prefix Hide')
t.equal(console.info['callCount'] , 0, 'should call info 0 time with prefix Hide')
t.equal(console.log['callCount'] , 0, 'should call log2(verbose + silly) 0 time with prefix Hide')
}).apply(log)

sinon.test(function() {
logFuncList.forEach(logFunc => this.stub(console, logFunc))
doLogShow(log)
t.equal(console.error['callCount'] , 1, 'should call error 1 time with prefix Show')
t.equal(console.warn['callCount'] , 1, 'should call warn 1 time with prefix Show')
t.equal(console.info['callCount'] , 1, 'should call info 1 time with prefix Show')
t.equal(console.log['callCount'] , 0, 'should call log2(verbose + silly) 1 time with prefix Show')
}).apply(log)

log.level('silent')

sinon.test(function() {
logFuncList.forEach(logFunc => this.stub(console, logFunc))
doLogShow(log)
t.equal(console.error['callCount'] , 0, 'should call error 0 time with prefix Show with level silent')
t.equal(console.warn['callCount'] , 0, 'should call warn 0 time with prefix Show with level silent')
t.equal(console.info['callCount'] , 0, 'should call info 0 time with prefix Show with level silent')
t.equal(console.log['callCount'] , 0, 'should call log2(verbose + silly) 0 time with prefix Show with level silent')
}).apply(log)

log.level('silly')

sinon.test(function() {
logFuncList.forEach(logFunc => this.stub(console, logFunc))
doLogShow(log)
t.equal(console.error['callCount'] , 1, 'should call error 1 time with prefix Show with level silly')
t.equal(console.warn['callCount'] , 1, 'should call warn 1 time with prefix Show with level silly')
t.equal(console.info['callCount'] , 1, 'should call info 1 time with prefix Show with level silly')
t.equal(console.log['callCount'] , 2, 'should call log2(verbose + silly) 2 time with prefix Show with level silly')
}).apply(log)

t.end()

////////////////////////////////////////////
function doLogHide(logger) {
logger.error('Hide', 'error message')
logger.warn('Hide', 'warn message')
logger.info('Hide', 'info message')
logger.verbose('Hide', 'verbose message')
logger.silly('Hide', 'silly message')
}

function doLogShow(logger) {
logger.error('Show', 'error message')
logger.warn('Show', 'warn message')
logger.info('Show', 'info message')
logger.verbose('Show', 'verbose message')
logger.silly('Show', 'silly message')
}
})

0 comments on commit 8c6a4e2

Please sign in to comment.