Skip to content

Commit

Permalink
Merge pull request #40 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
feat(loggerProxy): Out of the box confiuration of logger events with …
  • Loading branch information
scott-wyatt authored Jun 17, 2019
2 parents 7d2a1c8 + e2d8918 commit 8199198
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
15 changes: 11 additions & 4 deletions lib/LoggerProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ import { FabrixGeneric } from './common/Generic'

export class LoggerProxy extends FabrixGeneric {
public app: FabrixApp
public error
public warn
public debug
public info
public error
public debug
public silly

public levelHierarchy = ['silly', 'debug', 'info', 'warn', 'error']

/**
* Instantiate Proxy; bind log events to default console.log
*/
Expand Down Expand Up @@ -79,7 +82,11 @@ export class LoggerProxy extends FabrixGeneric {
* Emit fabrix:log, pass the "level" parameter to the event handler as the
* first argument.
*/
emitLogEvent (level: string) {
return (...msg: any[]) => this.app.emit('fabrix:log', level, msg)
emitLogEvent (level: string, current: string = 'silly') {
const currentLevel = this.app && this.app.config ? this.app.config.get('log.level') || current : current
level = level || current

const log = this.levelHierarchy.indexOf(currentLevel) <= this.levelHierarchy.indexOf(level) ? 'fabrix:log' : 'fabrix:log:ignored'
return (...msg: any[]) => this.app.emit(log, level, msg)
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/fabrix",
"version": "1.6.2",
"version": "1.6.3",
"description": "Strongly Typed Modern Web Application Framework for Node.js",
"keywords": [
"framework",
Expand Down
3 changes: 3 additions & 0 deletions test/integration/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const App = {
testdir: path.resolve(__dirname, 'testdir')
}
},
log: {
level: 'silly'
},
i18n: {
lng: 'en',
resources: {
Expand Down
1 change: 1 addition & 0 deletions test/integration/testapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
]
},
log: {
level: 'info',
logger: new smokesignals.Logger('silent')
}
},
Expand Down
14 changes: 14 additions & 0 deletions test/lib/LoggerProxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ describe('lib.LoggerProxy', () => {
})
logger.silly('hello')
})
it('should emit fabrix:log:ignored with level=info on invocation of log.silly', done => {
emitter.once('fabrix:log:ignored', level => {
assert.equal(level, 'silly')
done()
})
emitter.emit('fabrix:log:ignored', 'silly', 'hello')
})
it('should emit fabrix:log:ignored with level=info on invocation of log.silly', done => {

emitter.once('fabrix:log:ignored', emitLogEvent => {
done()
})
logger.emitLogEvent('silly', 'info')('hello')
})
it('should emit fabrix:log with level=debug on invocation of log.debug', done => {
emitter.once('fabrix:log', level => {
assert.equal(level, 'debug')
Expand Down
13 changes: 13 additions & 0 deletions test/lib/common/spools/Spool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ describe('spool', () => {
spool.log('info', 'hello from spool')
})
})

describe('#log silent', () => {
it('is a convenience method that simply invokes app.log', done => {
const spool= new Testspool(app)

app.once('fabrix:log:ignored', (level, [ msg ]) => {
done()
})

spool.log('silly', 'hello ignored from spool')
})
})

describe('#app', () => {
it('is a convenience method that should show the app', () => {
const app = new Fabrix(testApp)
Expand Down

0 comments on commit 8199198

Please sign in to comment.