Skip to content

Commit

Permalink
[slf4j]: Use SLF4J (#114)
Browse files Browse the repository at this point in the history
Use SLF4J instead of JUL
  • Loading branch information
sanmibuh authored and fmbenhassine committed Sep 26, 2017
1 parent a342bc2 commit a074f93
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 146 deletions.
4 changes: 4 additions & 0 deletions easy-rules-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
</licenses>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,42 @@
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.RuleListener;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class DefaultRuleListener implements RuleListener {

private static final Logger LOGGER = Logger.getLogger(DefaultRuleListener.class.getName());
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRuleListener.class);

@Override
public boolean beforeEvaluate(Rule rule, Facts facts) {
public boolean beforeEvaluate(final Rule rule, final Facts facts) {
return true;
}

@Override
public void afterEvaluate(Rule rule, Facts facts, boolean evaluationResult) {
String ruleName = rule.getName();
public void afterEvaluate(final Rule rule, final Facts facts, final boolean evaluationResult) {
final String ruleName = rule.getName();
if (evaluationResult) {
LOGGER.log(Level.INFO, "Rule ''{0}'' triggered", ruleName);
LOGGER.info("Rule ''{}'' triggered", ruleName);
} else {
LOGGER.log(Level.INFO, "Rule ''{0}'' has been evaluated to false, it has not been executed", ruleName);
LOGGER.info("Rule ''{}'' has been evaluated to false, it has not been executed", ruleName);
}
}

@Override
public void beforeExecute(Rule rule, Facts facts) {
public void beforeExecute(final Rule rule, final Facts facts) {

}

@Override
public void onSuccess(Rule rule, Facts facts) {
LOGGER.log(Level.INFO, "Rule ''{0}'' performed successfully", rule.getName());
public void onSuccess(final Rule rule, final Facts facts) {
LOGGER.info("Rule ''{}'' performed successfully", rule.getName());
}

@Override
public void onFailure(Rule rule, Facts facts, Exception exception) {
LOGGER.log(Level.SEVERE, String.format("Rule '%s' performed with error", rule.getName()), exception);
public void onFailure(final Rule rule, final Facts facts, final Exception exception) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Rule '" + rule.getName() + "' performed with error", exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
*/
package org.jeasy.rules.core;

import org.jeasy.rules.api.*;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.lang.String.format;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.RuleListener;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Default {@link RulesEngine} implementation.
Expand All @@ -42,7 +46,7 @@
*/
public final class DefaultRulesEngine implements RulesEngine {

private static final Logger LOGGER = Logger.getLogger(RulesEngine.class.getName());
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRuleListener.class);

/**
* The engine parameters
Expand Down Expand Up @@ -81,9 +85,6 @@ public DefaultRulesEngine(final RulesEngineParameters parameters, final List<Rul
this.ruleListeners = new ArrayList<>();
this.ruleListeners.add(new DefaultRuleListener());
this.ruleListeners.addAll(ruleListeners);
if (parameters.isSilentMode()) {
Utils.muteLoggers();
}
}

@Override
Expand All @@ -99,7 +100,7 @@ public List<RuleListener> getRuleListeners() {
@Override
public void fire(Rules rules, Facts facts) {
if (rules.isEmpty()) {
LOGGER.warning("No rules registered! Nothing to apply");
LOGGER.warn("No rules registered! Nothing to apply");
return;
}
logEngineParameters();
Expand All @@ -126,21 +127,22 @@ private void apply(Rules rules, Facts facts) {
final String name = rule.getName();
final int priority = rule.getPriority();
if (priority > parameters.getPriorityThreshold()) {
LOGGER.log(Level.INFO,
"Rule priority threshold ({0}) exceeded at rule ''{1}'' with priority={2}, next rules will be skipped",
new Object[]{parameters.getPriorityThreshold(), name, priority});
LOGGER.info("Rule priority threshold ({}) exceeded at rule ''{}'' with priority={}, next rules will be skipped",
parameters.getPriorityThreshold(), name, priority);
break;
}
if (!shouldBeEvaluated(rule, facts)) {
LOGGER.log(Level.INFO, "Rule ''{0}'' has been skipped before being evaluated", name);
LOGGER.info("Rule ''{}'' has been skipped before being evaluated",
name);
continue;
}
boolean evaluationResult;
try {
evaluationResult = rule.evaluate(facts);
} catch (NoSuchFactException e) {
if (parameters.isSkipOnMissingFact()) {
LOGGER.log(Level.INFO, "Rule ''{0}'' has been skipped due to missing fact ''{1}''", new Object[]{name, e.getMissingFact()});
LOGGER.info("Rule ''{}'' has been skipped due to missing fact ''{}''",
name, e.getMissingFact());
continue;
} else {
throw new RuntimeException(e);
Expand Down Expand Up @@ -211,29 +213,37 @@ private boolean shouldBeEvaluated(Rule rule, Facts facts) {
}

private void logEngineParameters() {
LOGGER.log(Level.INFO, "Rule priority threshold: {0}", parameters.getPriorityThreshold());
LOGGER.log(Level.INFO, "Skip on first applied rule: {0}", parameters.isSkipOnFirstAppliedRule());
LOGGER.log(Level.INFO, "Skip on first non triggered rule: {0}", parameters.isSkipOnFirstNonTriggeredRule());
LOGGER.log(Level.INFO, "Skip on first failed rule: {0}", parameters.isSkipOnFirstFailedRule());
LOGGER.log(Level.INFO, "Skip on missing fact: {0}", parameters.isSkipOnMissingFact());
LOGGER.info("Rule priority threshold: {}",
parameters.getPriorityThreshold());
LOGGER.info("Skip on first applied rule: {}",
parameters.isSkipOnFirstAppliedRule());
LOGGER.info("Skip on first non triggered rule: {}",
parameters.isSkipOnFirstNonTriggeredRule());
LOGGER.info("Skip on first failed rule: {}",
parameters.isSkipOnFirstFailedRule());
LOGGER.info("Skip on missing fact: {}",
parameters.isSkipOnMissingFact());
}

private void log(Rules rules) {
LOGGER.log(Level.INFO, "Registered rules:");
for (Rule rule : rules) {
LOGGER.log(Level.INFO, format("Rule { name = '%s', description = '%s', priority = '%s'}",
rule.getName(), rule.getDescription(), rule.getPriority()));
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Registered rules:");
for (Rule rule : rules) {
LOGGER.info("Rule { name = '{}', description = '{}', priority = '{}'}",
rule.getName(), rule.getDescription(), rule.getPriority());
}
}
}

private void log(Facts facts) {
LOGGER.log(Level.INFO, "Known facts:");
for (Map.Entry<String, Object> fact : facts) {
LOGGER.log(Level.INFO, format("Fact { %s : %s }",
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Known facts:");
for (Map.Entry<String, Object> fact : facts) {
LOGGER.info("Fact { {} : {} }",
fact.getKey(),
fact.getValue() == null ? "null" : fact.getValue().toString())
);
fact.getValue() == null ? "null" : fact.getValue().toString()
);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
*/
public class RulesEngineBuilder {

private RulesEngineParameters parameters;
private final RulesEngineParameters parameters;

private List<RuleListener> ruleListeners;
private final List<RuleListener> ruleListeners;

/**
* Create a new rules engine builder.
Expand All @@ -50,7 +50,7 @@ public static RulesEngineBuilder aNewRulesEngine() {
}

private RulesEngineBuilder() {
parameters = new RulesEngineParameters(false, false, false, false, RulesEngineParameters.DEFAULT_RULE_PRIORITY_THRESHOLD, false);
parameters = new RulesEngineParameters(false, false, false, false, RulesEngineParameters.DEFAULT_RULE_PRIORITY_THRESHOLD);
ruleListeners = new ArrayList<>();
}

Expand Down Expand Up @@ -121,13 +121,11 @@ public RulesEngineBuilder withRuleListener(final RuleListener ruleListener) {
}

/**
* Set silent mode to mute all loggers.
*
* @param silentMode to set
* @return the rules engine builder
* @deprecated Silent mode is now log implementation config. Now it uses slf4j facade
* <strong>This will be removed in v3.2</strong>
*/
@Deprecated
public RulesEngineBuilder withSilentMode(final boolean silentMode) {
parameters.setSilentMode(silentMode);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RulesEngineParameters {
/**
* Default rule priority threshold.
*/
public static int DEFAULT_RULE_PRIORITY_THRESHOLD = Integer.MAX_VALUE;
public static final int DEFAULT_RULE_PRIORITY_THRESHOLD = Integer.MAX_VALUE;

/**
* Parameter to skip next applicable rules when a rule is applied.
Expand All @@ -60,11 +60,6 @@ public class RulesEngineParameters {
*/
private int priorityThreshold;

/**
* Parameter to mute loggers.
*/
private boolean silentMode;

/**
* Create a new {@link RulesEngineParameters} with default values.
*/
Expand All @@ -74,15 +69,24 @@ public RulesEngineParameters() {

/**
* Create a new {@link RulesEngineParameters}
* @deprecated Use {@link RulesEngineParameters#RulesEngineParameters(boolean, boolean, boolean, boolean, int, boolean)} instead.
* @deprecated Use {@link RulesEngineParameters#RulesEngineParameters(boolean, boolean, boolean, boolean, int)} instead.
* <strong>This constructor will be removed in v3.2</strong>
*/
@Deprecated
public RulesEngineParameters(boolean skipOnFirstAppliedRule, boolean skipOnFirstFailedRule, int priorityThreshold, boolean silentMode) {
public RulesEngineParameters(final boolean skipOnFirstAppliedRule, final boolean skipOnFirstFailedRule, final int priorityThreshold, final boolean silentMode) {
this.skipOnFirstAppliedRule = skipOnFirstAppliedRule;
this.skipOnFirstFailedRule = skipOnFirstFailedRule;
this.priorityThreshold = priorityThreshold;
this.silentMode = silentMode;
}

/**
* Create a new {@link RulesEngineParameters}.
* @deprecated Use {@link RulesEngineParameters#RulesEngineParameters(boolean, boolean, boolean, boolean, int)} instead.
* <strong>This constructor will be removed in v3.2</strong>
*/
@Deprecated
public RulesEngineParameters(final boolean skipOnFirstAppliedRule, final boolean skipOnFirstFailedRule, final boolean skipOnFirstNonTriggeredRule, final boolean skipOnMissingFact, final int priorityThreshold, final boolean silentMode) {
this(skipOnFirstAppliedRule, skipOnFirstFailedRule, skipOnFirstNonTriggeredRule, skipOnMissingFact, priorityThreshold);
}

/**
Expand All @@ -93,62 +97,70 @@ public RulesEngineParameters(boolean skipOnFirstAppliedRule, boolean skipOnFirst
* @param skipOnFirstNonTriggeredRule parameter to skip next applicable rules on first non triggered rule.
* @param skipOnMissingFact parameter to skip a rule if a declared fact is missing.
* @param priorityThreshold threshold after which rules should be skipped.
* @param silentMode parameter to mute all loggers.
*/
public RulesEngineParameters(boolean skipOnFirstAppliedRule, boolean skipOnFirstFailedRule, boolean skipOnFirstNonTriggeredRule, boolean skipOnMissingFact, int priorityThreshold, boolean silentMode) {
public RulesEngineParameters(final boolean skipOnFirstAppliedRule, final boolean skipOnFirstFailedRule, final boolean skipOnFirstNonTriggeredRule, final boolean skipOnMissingFact, final int priorityThreshold) {
this.skipOnFirstAppliedRule = skipOnFirstAppliedRule;
this.skipOnFirstFailedRule = skipOnFirstFailedRule;
this.skipOnFirstNonTriggeredRule = skipOnFirstNonTriggeredRule;
this.skipOnMissingFact = skipOnMissingFact;
this.priorityThreshold = priorityThreshold;
this.silentMode = silentMode;
}

public int getPriorityThreshold() {
return priorityThreshold;
}

public void setPriorityThreshold(int priorityThreshold) {
this.priorityThreshold = priorityThreshold;
/**
* @deprecated Silent mode is now log implementation config. Now it uses slf4j facade
* <strong>This will be removed in v3.2</strong>
*/
@Deprecated
public boolean isSilentMode() {
return false;
}

public boolean isSilentMode() {
return silentMode;
/**
* @deprecated Silent mode is now log implementation config. Now it uses slf4j facade
* <strong>This will be removed in v3.2</strong>
*/
@Deprecated
public void setSilentMode(final boolean silentMode) {

}

public void setSilentMode(boolean silentMode) {
this.silentMode = silentMode;
public void setPriorityThreshold(final int priorityThreshold) {
this.priorityThreshold = priorityThreshold;
}

public boolean isSkipOnFirstAppliedRule() {
return skipOnFirstAppliedRule;
}

public void setSkipOnFirstAppliedRule(boolean skipOnFirstAppliedRule) {
public void setSkipOnFirstAppliedRule(final boolean skipOnFirstAppliedRule) {
this.skipOnFirstAppliedRule = skipOnFirstAppliedRule;
}

public boolean isSkipOnFirstNonTriggeredRule() {
return skipOnFirstNonTriggeredRule;
}

public void setSkipOnFirstNonTriggeredRule(boolean skipOnFirstNonTriggeredRule) {
public void setSkipOnFirstNonTriggeredRule(final boolean skipOnFirstNonTriggeredRule) {
this.skipOnFirstNonTriggeredRule = skipOnFirstNonTriggeredRule;
}

public boolean isSkipOnFirstFailedRule() {
return skipOnFirstFailedRule;
}

public void setSkipOnFirstFailedRule(boolean skipOnFirstFailedRule) {
public void setSkipOnFirstFailedRule(final boolean skipOnFirstFailedRule) {
this.skipOnFirstFailedRule = skipOnFirstFailedRule;
}

public boolean isSkipOnMissingFact() {
return skipOnMissingFact;
}

public void setSkipOnMissingFact(boolean skipOnMissingFact) {
public void setSkipOnMissingFact(final boolean skipOnMissingFact) {
this.skipOnMissingFact = skipOnMissingFact;
}
}
Loading

0 comments on commit a074f93

Please sign in to comment.