Skip to content
This repository has been archived by the owner on Aug 1, 2019. It is now read-only.

Commit

Permalink
fixer replacer 0.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
John Stein committed Jun 15, 2019
1 parent f3ba598 commit d75752b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Output
{"time":"2019-06-15T09:02:20.144Z","some":"nice JSON here"}
```

NOTE: If you pass both a string message and a JSON object you can also
pass as a third parameter an array of `propsToSkip`. This will remove
any properties in the `propsToSkip` array from the JSON output.

## Options

**`out`**
Expand All @@ -64,12 +68,6 @@ Otherwise we use `label` as the message label.

Defaults to `false`. If `true` we add a 'level' element to the JSON output.

**`time`**

Defaults to a function that outputs `new Date().toISOString()`.
If `false`, we don't add a 'time' element to the JSON output.
You can also use your own function to format time however you like.

**`indent`**

Defaults to `0`. This is the number of spaces to indent the JSON output.
Expand All @@ -89,3 +87,21 @@ Defaults to the following levels. You can specify any levels you like.
off: 6
}
```

**`timeFn`**

Defaults to a function that outputs `new Date().toISOString()`.
If `false`, we don't add a 'time' element to the JSON output.
Use your own function to format time however you like.

**`fixerFn`**

Defaults to a function that gets own property names from your json.
This makes it easy to log objects (like `Error`) that aren't
strictly JSON simple. If `false` we don't mess with your JSON.

**`replacerFn`**

Defaults to a function that avoids `TypeError: Converting circular
structure to JSON`. If `false`, you may get that error in certain
cases.
65 changes: 43 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

'use strict'
const isObject = (a) =>
!!a && a.constructor === Object
const isString = (o) =>
typeof o === 'string' || o instanceof String
class Logger {
constructor(options) {
options = options || {}
Expand All @@ -11,9 +11,6 @@ class Logger {
this.label = 'message'
this.levelAsLabel = true
this.levelElement = false
this.time = () => {
return new Date().toISOString()
}
this.indent = 0
this.levels = {
trace: 0,
Expand All @@ -24,27 +21,50 @@ class Logger {
fatal: 5,
off: 6
}
this.timeFn = () => {
return new Date().toISOString()
}
this.fixerFn = (o, propsToSkip) => {
const fixed = {}
Object.getOwnPropertyNames(o).forEach(function(key) {
if (!propsToSkip || propsToSkip.indexOf(key) === -1) {
fixed[key] = o[key]
}
});
return fixed
}
this.replacerFn = (key, value) => {
if (typeof value === 'object' && value !== null) {
if (this.cache.indexOf(value) !== -1) {
return
}
this.cache.push(value)
}
return value
}
Object.assign(this, options)
for (let level of Object.keys(this.levels)) {
this[level] = (message, json) => {
this[level] = (message, json, propsToSkip) => {
if (this.levels[level] >= this.levels[this.level]) {
let options = {}
if (this.time) {
options.time = this.time()
if (this.timeFn) {
options.time = this.timeFn()
}
if (this.levelElement) {
options.level = level
}
const lbl = this.levelAsLabel ? level : this.label
if (json) {
options[lbl] = message
options = Object.assign(options, json)
} else if (isObject(message)) {
options = Object.assign(options, message)
options = Object.assign(options, this.fixerFn ? this.fixerFn(json, propsToSkip) : json)
} else if (!isString(message)) {
options = Object.assign(options, this.fixerFn ? this.fixerFn(message, propsToSkip) : message)
} else {
options[lbl] = message
}
this.out.write(`${JSON.stringify(options, null, this.indent)}\n`)
this.cache = []
this.out.write(`${JSON.stringify(options, this.replacerFn, this.indent)}\n`)
this.cache = null
}
}
}
Expand All @@ -56,17 +76,20 @@ if (require.main === module) {
const log = new Logger({
// out: fs.createWriteStream('./logfile'),
level: 'trace',
// label: 'niceMessage',
// levelAsLabel: false,
// levelElement: true,
label: 'msg',
levelAsLabel: false,
levelElement: true,
// time: false,
// indent: 2,
indent: 3,
// levels: {
// bat: 0,
// zoo: 1,
// monkey: 2
// }
// },
// fixer: false,
// replacer: false,
})
log.info('This is our log.', log, ['out'])
log.trace('You probably won\'t ever use this.')
log.debug('This is quite a bit of detail.')
log.info('Hello log world!')
Expand All @@ -76,10 +99,8 @@ if (require.main === module) {
log.info({
some: 'nice JSON here'
})
log.warn('Geting low on memory.')
log.error('Could not get the eggs.', {
message: 'oops',
code: 39
})
// log.warn('Geting low on memory.', log, ['out'])
log.error('Could not get the eggs.', new Error('The truck is out of gas.'), ['stack'])
log.error(new Error('hard times!'))
log.fatal('We crashed!')
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-json-log",
"version": "0.0.8",
"version": "0.0.9",
"keywords": "log logger logging node Node.js JSON simple easy",
"description": "Simple JSON logger for Node.js",
"main": "index.js",
Expand Down

0 comments on commit d75752b

Please sign in to comment.