@kofile/log
Advanced tools
Comparing version 1.1.0 to 1.2.0
47
Log.js
const Adapters = require('./adapters') | ||
const Timer = require('./Timer') | ||
const formatMessageFor = level => message => { | ||
const formatMessageFor = (level, meta) => message => { | ||
const time = new Date().toISOString() | ||
const baseMessage = `${time} [${level}] ${message || ''}` | ||
if (meta) { | ||
return `${baseMessage} -- ${JSON.stringify(meta)}` | ||
} | ||
return baseMessage | ||
} | ||
const isValid = meta => { | ||
if (typeof meta === 'number') { | ||
return false | ||
} else if (typeof meta === 'string') { | ||
return false | ||
} else if (typeof meta === 'symbol') { | ||
return false | ||
} else if (typeof meta === 'boolean') { | ||
return false | ||
} else if (typeof meta === 'function') { | ||
return false | ||
} else if (Array.prototype.isPrototypeOf(meta)) { | ||
return false | ||
} | ||
return true | ||
} | ||
class Log { | ||
@@ -21,7 +43,13 @@ constructor (params) { | ||
this._meta = (params || {}).meta || null | ||
if (!isValid(this._meta)) { | ||
throw new Error('Meta must be an object with enumerable properties!') | ||
} | ||
this._adapter = (params || {}).adapter || new Adapters.Console(console) | ||
this._formatError = formatMessageFor(klass.LEVELS.ERROR) | ||
this._formatWarning = formatMessageFor(klass.LEVELS.WARN) | ||
this._formatInfo = formatMessageFor(klass.LEVELS.INFO) | ||
this._formatDebug = formatMessageFor(klass.LEVELS.DEBUG) | ||
this._formatError = formatMessageFor(klass.LEVELS.ERROR, this._meta) | ||
this._formatWarning = formatMessageFor(klass.LEVELS.WARN, this._meta) | ||
this._formatInfo = formatMessageFor(klass.LEVELS.INFO, this._meta) | ||
this._formatDebug = formatMessageFor(klass.LEVELS.DEBUG, this._meta) | ||
this._defaultTimerCallback = () => {} | ||
@@ -86,2 +114,11 @@ } | ||
} | ||
spawn (newMeta) { | ||
const adapter = this._adapter | ||
const level = this._level | ||
const parentMeta = this._meta | ||
const mergedMeta = Object.assign({}, parentMeta, newMeta) | ||
return new this.constructor({ adapter, level, meta: mergedMeta }) | ||
} | ||
} | ||
@@ -88,0 +125,0 @@ |
@@ -1,1 +0,1 @@ | ||
{"name":"@kofile/log","version":"1.1.0","description":"Node log wrapper","author":"Evan Sherwood <evan@sherwood.io>","license":"MIT","main":"index.js","files":["index.js","Timer.js","Log.js","adapters/Console/index.js","adapters/Adapter.js","adapters/index.js"],"devDependencies":{"ava":"^0.22.0","commitizen":"^2.9.6","coveralls":"^2.13.1","cz-cli":"^1.0.0","cz-conventional-changelog":"^2.0.0","husky":"^0.13.4","lint-staged":"^3.6.0","nyc":"^11.1.0","prettier-standard":"^6.0.0","semantic-release":"^8.2.0","sinon":"^4.0.1","snazzy":"^7.0.0","standard":"^10.0.3"},"lint-staged":{"*.js":["prettier-standard","git add"]},"scripts":{"precommit":"lint-staged","commit":"git-cz","lint":"standard | snazzy","test":"nyc ava","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"config":{"commitizen":{"path":"node_modules/cz-conventional-changelog"}},"nyc":{"include":["index.js","Timer.js","Log.js","adapters/Console/index.js","adapters/Adapter.js","adapters/index.js"]},"repository":{"type":"git","url":"https://github.com/kofile/log.git"}} | ||
{"name":"@kofile/log","version":"1.2.0","description":"Node log wrapper","author":"Evan Sherwood <evan@sherwood.io>","license":"MIT","main":"index.js","files":["index.js","Timer.js","Log.js","adapters/Console/index.js","adapters/Adapter.js","adapters/index.js"],"devDependencies":{"ava":"^0.22.0","commitizen":"^2.9.6","coveralls":"^2.13.1","cz-cli":"^1.0.0","cz-conventional-changelog":"^2.0.0","husky":"^0.13.4","lint-staged":"^3.6.0","nyc":"^11.1.0","prettier-standard":"^6.0.0","semantic-release":"^8.2.0","sinon":"^4.0.1","snazzy":"^7.0.0","standard":"^10.0.3"},"lint-staged":{"*.js":["prettier-standard","git add"]},"scripts":{"precommit":"lint-staged","commit":"git-cz","lint":"standard | snazzy","test":"nyc ava","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"config":{"commitizen":{"path":"node_modules/cz-conventional-changelog"}},"nyc":{"include":["index.js","Timer.js","Log.js","adapters/Console/index.js","adapters/Adapter.js","adapters/index.js"]},"repository":{"type":"git","url":"https://github.com/kofile/log.git"}} |
@@ -68,2 +68,14 @@ # Log | ||
You can also pass in a `meta` object, which will be rendered with every log message as stringified `JSON`: | ||
```js | ||
const meta = { foo: 'bar' } | ||
const log = makeLog({ meta }) | ||
log.info('test message') | ||
//=> `1984-10-04T06:12:43.453Z [DEBUG] test message -- {"foo":"bar"}` | ||
``` | ||
Meta must be an object. | ||
<a name="api"></a> | ||
@@ -145,2 +157,6 @@ ## API | ||
#### `log.spawn([meta])` | ||
Returns a new instance of `log` with the same initialization parameters as its parent, and merges in `meta` if `meta` is given. | ||
<a name="timer"></a> | ||
@@ -147,0 +163,0 @@ ### Timer |
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
10789
178
184