Skip to content

Commit

Permalink
allow tagging loggers (#6)
Browse files Browse the repository at this point in the history
* allow tagging loggers

* update config
  • Loading branch information
alextremp authored May 12, 2021
1 parent db5d974 commit e6ecbf2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.21.0
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dist: trusty
language: node_js
node_js:
- "12.14.1"
- "12.21.0"
cache:
directories:
- node_modules
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@ The logger should be initialized with a custom key to identify the logger.
```code
import Piko from '@adv-ui/piko-logger'
const logger = Piko.logger(key)
const logger = Piko.logger('MyLibrary')
....
// optionally use a tagged instance of logger to fine grain where the logged action occurs
const taggedLogger = logger.tag('MyFunction')
// using the logger methods, logs will show a reference to 'MyLibrary'
// using the taggedLogger methods, logs will show a reference to 'MyLibrary # MyFunction'
```

### Log
Expand Down
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@
"author": "One Punch Team",
"license": "MIT",
"devDependencies": {
"@babel/cli": "7.8.0",
"@babel/core": "7.8.0",
"@babel/plugin-transform-modules-commonjs": "7.8.0",
"@babel/register": "7.8.0",
"@babel/runtime": "7.8.0",
"@babel/cli": "7.13.16",
"@babel/core": "7.13.16",
"@babel/plugin-transform-modules-commonjs": "7.13.8",
"@babel/register": "7.13.16",
"@babel/runtime": "7.13.17",
"@s-ui/lint": "3",
"@s-ui/test": "2",
"@s-ui/test": "4",
"babel-plugin-transform-define": "2.0.0",
"babel-preset-sui": "3",
"chai": "4.2.0",
"codecov": "3.6.5",
"html-webpack-plugin": "4.0.4",
"mocha": "5.2.0",
"nyc": "15.0.0",
"chai": "4.3.4",
"codecov": "3.8.2",
"html-webpack-plugin": "5.3.1",
"mocha": "8.4.0",
"nyc": "15.1.0",
"versiona": "4",
"jsdom": "16.2.2",
"sinon": "9.0.2"
"jsdom": "16.5.3",
"sinon": "10.0.0"
},
"eslintConfig": {
"extends": [
Expand Down
10 changes: 10 additions & 0 deletions src/integration-test/PikoTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,5 +347,15 @@ describe('Piko', () => {
expect(logger1._level._level).to.equal(DEFAULT_LEVEL)
expect(logger2._level._level).to.equal(DEFAULT_LEVEL)
})
it('should create tagged loggers', () => {
const baseKey = 'base'
window.localStorage.setItem(`piko.level.${baseKey}`, 'debug')
const base = Piko.logger(baseKey)
const tagged = base.tag('tagged')
const aDebugMessage = 'debug testing'
tagged.debug(() => aDebugMessage)
expect(debugSpy.args[0][0]).to.equal('DEBUG | base # tagged | ')
expect(debugSpy.args[0][1]).to.equal(aDebugMessage)
})
})
})
6 changes: 5 additions & 1 deletion src/main/domain/Logger.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
export class Logger {
constructor({level, key = 'DEFAULT'}) {
constructor({level, key = ''}) {
this._key = key
this._level = level
this._console = console
}

tag(tag = 'default') {
return new Logger({level: this._level, key: `${this._key} # ${tag}`})
}

trace(log) {
if (this._level.shouldLogTrace()) {
this._log({
Expand Down

0 comments on commit e6ecbf2

Please sign in to comment.