Skip to content

Commit 61fca7d

Browse files
chris-pardychris-pardy-newfire
authored andcommitted
Improvements To Debug
Change the call signature, leverage built-in formatting on console. Move debug enablement checks to file load. Replaces #363 Resolves #362 Co-Author Nirmal Patel <nirmal259907@gmail.com>
1 parent b22e7d9 commit 61fca7d

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

src/almanac.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default class Almanac {
106106
} else {
107107
fact = new Fact(id, valueOrMethod, options)
108108
}
109-
debug(`almanac::addFact id:${factId}`)
109+
debug('almanac::addFact', { id: factId })
110110
this.factMap.set(factId, fact)
111111
if (fact.isConstant()) {
112112
this._setFactValue(fact, {}, fact.value)
@@ -121,7 +121,7 @@ export default class Almanac {
121121
* @param {Mixed} value - constant value of the fact
122122
*/
123123
addRuntimeFact (factId, value) {
124-
debug(`almanac::addRuntimeFact id:${factId}`)
124+
debug('almanac::addRuntimeFact', { id: factId })
125125
const fact = new Fact(factId, value)
126126
return this._addConstantFact(fact)
127127
}
@@ -151,22 +151,22 @@ export default class Almanac {
151151
const cacheVal = cacheKey && this.factResultsCache.get(cacheKey)
152152
if (cacheVal) {
153153
factValuePromise = Promise.resolve(cacheVal)
154-
debug(`almanac::factValue cache hit for fact:${factId}`)
154+
debug('almanac::factValue cache hit for fact', { id: factId })
155155
} else {
156-
debug(`almanac::factValue cache miss for fact:${factId}; calculating`)
156+
debug('almanac::factValue cache miss, calculating', { id: factId })
157157
factValuePromise = this._setFactValue(fact, params, fact.calculate(params, this))
158158
}
159159
}
160160
if (path) {
161-
debug(`condition::evaluate extracting object property ${path}`)
161+
debug('condition::evaluate extracting object', { property: path })
162162
return factValuePromise
163163
.then(factValue => {
164164
if (isObjectLike(factValue)) {
165165
const pathValue = this.pathResolver(factValue, path)
166-
debug(`condition::evaluate extracting object property ${path}, received: ${JSON.stringify(pathValue)}`)
166+
debug('condition::evaluate extracting object', { property: path, received: pathValue })
167167
return pathValue
168168
} else {
169-
debug(`condition::evaluate could not compute object path(${path}) of non-object: ${factValue} <${typeof factValue}>; continuing with ${factValue}`)
169+
debug('condition::evaluate could not compute object path of non-object', { path, factValue, type: typeof factValue })
170170
return factValue
171171
}
172172
})

src/condition.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ export default class Condition {
101101
]).then(([rightHandSideValue, leftHandSideValue]) => {
102102
const result = op.evaluate(leftHandSideValue, rightHandSideValue)
103103
debug(
104-
`condition::evaluate <${JSON.stringify(leftHandSideValue)} ${
105-
this.operator
106-
} ${JSON.stringify(rightHandSideValue)}?> (${result})`
104+
'condition::evaluate', {
105+
leftHandSideValue,
106+
operator: this.operator,
107+
rightHandSideValue,
108+
result
109+
}
107110
)
108111
return {
109112
result,

src/debug.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
export default function debug (message) {
1+
2+
function createDebug () {
23
try {
34
if ((typeof process !== 'undefined' && process.env && process.env.DEBUG && process.env.DEBUG.match(/json-rules-engine/)) ||
45
(typeof window !== 'undefined' && window.localStorage && window.localStorage.debug && window.localStorage.debug.match(/json-rules-engine/))) {
5-
console.log(message)
6+
return console.debug.bind(console)
67
}
78
} catch (ex) {
89
// Do nothing
910
}
11+
return () => {}
1012
}
13+
14+
export default createDebug()

src/engine.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Engine extends EventEmitter {
133133
} else {
134134
operator = new Operator(operatorOrName, cb)
135135
}
136-
debug(`engine::addOperator name:${operator.name}`)
136+
debug('engine::addOperator', { name: operator.name })
137137
this.operators.set(operator.name, operator)
138138
}
139139

@@ -168,7 +168,7 @@ class Engine extends EventEmitter {
168168
} else {
169169
fact = new Fact(id, valueOrMethod, options)
170170
}
171-
debug(`engine::addFact id:${factId}`)
171+
debug('engine::addFact', { id: factId })
172172
this.facts.set(factId, fact)
173173
return this
174174
}
@@ -237,11 +237,11 @@ class Engine extends EventEmitter {
237237
evaluateRules (ruleArray, almanac) {
238238
return Promise.all(ruleArray.map((rule) => {
239239
if (this.status !== RUNNING) {
240-
debug(`engine::run status:${this.status}; skipping remaining rules`)
240+
debug('engine::run, skipping remaining rules', { status: this.status })
241241
return Promise.resolve()
242242
}
243243
return rule.evaluate(almanac).then((ruleResult) => {
244-
debug(`engine::run ruleResult:${ruleResult.result}`)
244+
debug('engine::run', { ruleResult: ruleResult.result })
245245
almanac.addResult(ruleResult)
246246
if (ruleResult.result) {
247247
almanac.addEvent(ruleResult.event, 'success')
@@ -282,7 +282,7 @@ class Engine extends EventEmitter {
282282
}
283283

284284
almanac.addFact(fact)
285-
debug(`engine::run initialized runtime fact:${fact.id} with ${fact.value}<${typeof fact.value}>`)
285+
debug('engine::run initialized runtime fact', { id: fact.id, value: fact.value, type: typeof fact.value })
286286
}
287287
const orderedSets = this.prioritizeRules()
288288
let cursor = Promise.resolve()

src/rule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class Rule extends EventEmitter {
249249
return Promise.all(
250250
conditions.map((condition) => evaluateCondition(condition))
251251
).then((conditionResults) => {
252-
debug('rule::evaluateConditions results', conditionResults)
252+
debug('rule::evaluateConditions', { results: conditionResults })
253253
return method.call(conditionResults, (result) => result === true)
254254
})
255255
}

0 commit comments

Comments
 (0)