Skip to content

Commit f4ed36e

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 CacheControl#363 Resolves CacheControl#362 Co-Author Nirmal Patel <nirmal259907@gmail.com>
1 parent bf91c0a commit f4ed36e

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
@@ -105,7 +105,7 @@ export default class Almanac {
105105
} else {
106106
fact = new Fact(id, valueOrMethod, options)
107107
}
108-
debug(`almanac::addFact id:${factId}`)
108+
debug('almanac::addFact', { id: factId })
109109
this.factMap.set(factId, fact)
110110
if (fact.isConstant()) {
111111
this._setFactValue(fact, {}, fact.value)
@@ -120,7 +120,7 @@ export default class Almanac {
120120
* @param {Mixed} value - constant value of the fact
121121
*/
122122
addRuntimeFact (factId, value) {
123-
debug(`almanac::addRuntimeFact id:${factId}`)
123+
debug('almanac::addRuntimeFact', { id: factId })
124124
const fact = new Fact(factId, value)
125125
return this._addConstantFact(fact)
126126
}
@@ -150,22 +150,22 @@ export default class Almanac {
150150
const cacheVal = cacheKey && this.factResultsCache.get(cacheKey)
151151
if (cacheVal) {
152152
factValuePromise = Promise.resolve(cacheVal)
153-
debug(`almanac::factValue cache hit for fact:${factId}`)
153+
debug('almanac::factValue cache hit for fact', { id: factId })
154154
} else {
155-
debug(`almanac::factValue cache miss for fact:${factId}; calculating`)
155+
debug('almanac::factValue cache miss, calculating', { id: factId })
156156
factValuePromise = this._setFactValue(fact, params, fact.calculate(params, this))
157157
}
158158
}
159159
if (path) {
160-
debug(`condition::evaluate extracting object property ${path}`)
160+
debug('condition::evaluate extracting object', { property: path })
161161
return factValuePromise
162162
.then(factValue => {
163163
if (factValue != null && typeof factValue === 'object') {
164164
const pathValue = this.pathResolver(factValue, path)
165-
debug(`condition::evaluate extracting object property ${path}, received: ${JSON.stringify(pathValue)}`)
165+
debug('condition::evaluate extracting object', { property: path, received: pathValue })
166166
return pathValue
167167
} else {
168-
debug(`condition::evaluate could not compute object path(${path}) of non-object: ${factValue} <${typeof factValue}>; continuing with ${factValue}`)
168+
debug('condition::evaluate could not compute object path of non-object', { path, factValue, type: typeof factValue })
169169
return factValue
170170
}
171171
})

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)