Skip to content

Convert to "debug" library for debug logging #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 122 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
},
"dependencies": {
"clone": "^2.1.2",
"debug": "^4.2.0",
"events": "^3.2.0",
"hash-it": "^4.0.5",
"jsonpath-plus": "^4.0.0",
Expand Down
22 changes: 12 additions & 10 deletions src/almanac.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import Fact from './fact'
import { UndefinedFactError } from './errors'
import debug from './debug'
import Debug from './debug'

import { JSONPath } from 'jsonpath-plus'
import isObjectLike from 'lodash.isobjectlike'

const debug = Debug('json-rules-engine:almanac')

/**
* Fact results lookup
* Triggers fact computations and saves the results
Expand All @@ -27,7 +29,7 @@ export default class Almanac {
}

this._addConstantFact(fact)
debug(`almanac::constructor initialized runtime fact:${fact.id} with ${fact.value}<${typeof fact.value}>`)
debug('constructor initialized runtime fact:%s with %o <%t>', fact.id, fact.value, fact.value)
}
}

Expand Down Expand Up @@ -70,7 +72,7 @@ export default class Almanac {
* @param {Mixed} value - constant value of the fact
*/
addRuntimeFact (factId, value) {
debug(`almanac::addRuntimeFact id:${factId}`)
debug('addRuntimeFact id:%s', factId)
const fact = new Fact(factId, value)
return this._addConstantFact(fact)
}
Expand Down Expand Up @@ -100,24 +102,24 @@ export default class Almanac {
const cacheVal = cacheKey && this.factResultsCache.get(cacheKey)
if (cacheVal) {
factValuePromise = Promise.resolve(cacheVal)
debug(`almanac::factValue cache hit for fact:${factId}`)
debug('factValue cache hit for fact:%s', factId)
} else {
debug(`almanac::factValue cache miss for fact:${factId}; calculating`)
debug('factValue cache miss for fact:%s; calculating', factId)
factValuePromise = this._setFactValue(fact, params, fact.calculate(params, this))
}
}
if (path) { // selectn supports arrays and strings as a 'path'
// strings starting with '$' denotes json path. otherwise fall back to deprecated 'selectn' syntax
if (typeof path === 'string' && path.startsWith('$')) {
debug(`condition::evaluate extracting object property ${path}`)
debug('condition::evaluate extracting object property %s', path)
return factValuePromise
.then(factValue => {
if (isObjectLike(factValue)) {
const pathValue = JSONPath({ path, json: factValue, wrap: false })
debug(`condition::evaluate extracting object property ${path}, received: ${pathValue}`)
debug('condition::evaluate extracting object property %s, received: %o', path, pathValue)
return pathValue
} else {
debug(`condition::evaluate could not compute object path(${path}) of non-object: ${factValue} <${typeof factValue}>; continuing with ${factValue}`)
debug('condition::evaluate could not compute object path(%s) of non-object: %s <%t>; continuing with %s', path, factValue, factValue, factValue)
return factValue
}
})
Expand All @@ -136,10 +138,10 @@ export default class Almanac {
.then(factValue => {
if (isObjectLike(factValue)) {
const pathValue = selectn(path)(factValue)
debug(`condition::evaluate extracting object property ${path}, received: ${pathValue}`)
debug('condition::evaluate extracting object property %s, received: %o', path, factValue)
return pathValue
} else {
debug(`condition::evaluate could not compute object path(${path}) of non-object: ${factValue} <${typeof factValue}>; continuing with ${factValue}`)
debug('condition::evaluate could not compute object path(%s) of non-object: %s <%t>; continuing with %s', path, factValue, factValue, factValue)
return factValue
}
})
Expand Down
7 changes: 5 additions & 2 deletions src/condition.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict'

import debug from './debug'
import Debug from './debug'

import isObjectLike from 'lodash.isobjectlike'

const debug = Debug('json-rules-engine:condition')

export default class Condition {
constructor (properties) {
if (!properties) throw new Error('Condition: constructor options required')
Expand Down Expand Up @@ -101,7 +104,7 @@ export default class Condition {
return almanac.factValue(this.fact, this.params, this.path)
.then(leftHandSideValue => {
const result = op.evaluate(leftHandSideValue, rightHandSideValue)
debug(`condition::evaluate <${leftHandSideValue} ${this.operator} ${rightHandSideValue}?> (${result})`)
debug('evaluate <%s %s %s?> (%s)', leftHandSideValue, this.operator, rightHandSideValue, result)
return { result, leftHandSideValue, rightHandSideValue, operator: this.operator }
})
})
Expand Down
17 changes: 7 additions & 10 deletions src/debug.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
export default function debug (message) {
try {
if ((typeof process !== 'undefined' && process.env && process.env.DEBUG && process.env.DEBUG.match(/json-rules-engine/)) ||
(typeof window !== 'undefined' && window.localStorage && window.localStorage.debug && window.localStorage.debug.match(/json-rules-engine/))) {
console.log(message)
}
} catch (ex) {
// Do nothing
}
}
'use strict'

import Debug from 'debug'

Debug.formatters.t = o => typeof o

export default Debug
Loading