Skip to content

Commit 8199198

Browse files
authored
Merge pull request #40 from fabrix-app/v1.6
feat(loggerProxy): Out of the box confiuration of logger events with …
2 parents 7d2a1c8 + e2d8918 commit 8199198

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

lib/LoggerProxy.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ import { FabrixGeneric } from './common/Generic'
4040

4141
export class LoggerProxy extends FabrixGeneric {
4242
public app: FabrixApp
43+
public error
4344
public warn
44-
public debug
4545
public info
46-
public error
46+
public debug
4747
public silly
48+
49+
public levelHierarchy = ['silly', 'debug', 'info', 'warn', 'error']
50+
4851
/**
4952
* Instantiate Proxy; bind log events to default console.log
5053
*/
@@ -79,7 +82,11 @@ export class LoggerProxy extends FabrixGeneric {
7982
* Emit fabrix:log, pass the "level" parameter to the event handler as the
8083
* first argument.
8184
*/
82-
emitLogEvent (level: string) {
83-
return (...msg: any[]) => this.app.emit('fabrix:log', level, msg)
85+
emitLogEvent (level: string, current: string = 'silly') {
86+
const currentLevel = this.app && this.app.config ? this.app.config.get('log.level') || current : current
87+
level = level || current
88+
89+
const log = this.levelHierarchy.indexOf(currentLevel) <= this.levelHierarchy.indexOf(level) ? 'fabrix:log' : 'fabrix:log:ignored'
90+
return (...msg: any[]) => this.app.emit(log, level, msg)
8491
}
8592
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fabrix/fabrix",
3-
"version": "1.6.2",
3+
"version": "1.6.3",
44
"description": "Strongly Typed Modern Web Application Framework for Node.js",
55
"keywords": [
66
"framework",

test/integration/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const App = {
3434
testdir: path.resolve(__dirname, 'testdir')
3535
}
3636
},
37+
log: {
38+
level: 'silly'
39+
},
3740
i18n: {
3841
lng: 'en',
3942
resources: {

test/integration/testapp.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
]
2020
},
2121
log: {
22+
level: 'info',
2223
logger: new smokesignals.Logger('silent')
2324
}
2425
},

test/lib/LoggerProxy.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ describe('lib.LoggerProxy', () => {
2121
})
2222
logger.silly('hello')
2323
})
24+
it('should emit fabrix:log:ignored with level=info on invocation of log.silly', done => {
25+
emitter.once('fabrix:log:ignored', level => {
26+
assert.equal(level, 'silly')
27+
done()
28+
})
29+
emitter.emit('fabrix:log:ignored', 'silly', 'hello')
30+
})
31+
it('should emit fabrix:log:ignored with level=info on invocation of log.silly', done => {
32+
33+
emitter.once('fabrix:log:ignored', emitLogEvent => {
34+
done()
35+
})
36+
logger.emitLogEvent('silly', 'info')('hello')
37+
})
2438
it('should emit fabrix:log with level=debug on invocation of log.debug', done => {
2539
emitter.once('fabrix:log', level => {
2640
assert.equal(level, 'debug')

test/lib/common/spools/Spool.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ describe('spool', () => {
4343
spool.log('info', 'hello from spool')
4444
})
4545
})
46+
47+
describe('#log silent', () => {
48+
it('is a convenience method that simply invokes app.log', done => {
49+
const spool= new Testspool(app)
50+
51+
app.once('fabrix:log:ignored', (level, [ msg ]) => {
52+
done()
53+
})
54+
55+
spool.log('silly', 'hello ignored from spool')
56+
})
57+
})
58+
4659
describe('#app', () => {
4760
it('is a convenience method that should show the app', () => {
4861
const app = new Fabrix(testApp)

0 commit comments

Comments
 (0)