Skip to content

Commit 97a3bdd

Browse files
authored
feat: optimizations when logging is disabled (#453)
1 parent 697daa0 commit 97a3bdd

File tree

9 files changed

+46
-22
lines changed

9 files changed

+46
-22
lines changed

src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,7 @@ public void loadPolicy() {
284284
model.sortPoliciesBySubjectHieraichy();
285285

286286
clearRmMap();
287-
if (Util.enableLog) {
288-
model.printPolicy();
289-
}
287+
model.printPolicy();
290288
if (autoBuildRoleLinks) {
291289
buildRoleLinks();
292290
buildConditionalRoleLinks();
@@ -315,9 +313,7 @@ public void loadFilteredPolicy(Object filter) {
315313
model.sortPoliciesBySubjectHieraichy();
316314

317315
initRmMap();
318-
if (Util.enableLog) {
319-
model.printPolicy();
320-
}
316+
model.printPolicy();
321317
if (autoBuildRoleLinks) {
322318
buildRoleLinks();
323319
buildConditionalRoleLinks();

src/main/java/org/casbin/jcasbin/main/DistributedEnforcer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public List<List<String>> addPolicySelf(BooleanSupplier shouldPersist, String se
121121
((BatchAdapter) adapter).addPolicies(sec, ptype, rules);
122122
}
123123
} catch (UnsupportedOperationException ignored) {
124-
Util.logPrintf("Method not implemented");
124+
Util.logPrint("Method not implemented");
125125
} catch (Exception e) {
126126
Util.logPrint("An exception occurred:" + e.getMessage());
127127
return null;
@@ -159,7 +159,7 @@ public List<List<String>> removePolicySelf(BooleanSupplier shouldPersist, String
159159
((BatchAdapter) adapter).removePolicies(sec, ptype, rules);
160160
}
161161
} catch (UnsupportedOperationException ignored) {
162-
Util.logPrintf("Method not implemented");
162+
Util.logPrint("Method not implemented");
163163
} catch (Exception e) {
164164
Util.logPrint("An exception occurred:" + e.getMessage());
165165
return null;
@@ -197,7 +197,7 @@ public List<List<String>> removeFilteredPolicySelf(BooleanSupplier shouldPersist
197197
try {
198198
adapter.removeFilteredPolicy(sec, ptype, fieldIndex, fieldValues);
199199
} catch (UnsupportedOperationException ignored) {
200-
Util.logPrintf("Method not implemented");
200+
Util.logPrint("Method not implemented");
201201
} catch (Exception e) {
202202
Util.logPrint("An exception occurred:" + e.getMessage());
203203
return null;
@@ -227,7 +227,7 @@ public void clearPolicySelf(BooleanSupplier shouldPersist) {
227227
try {
228228
adapter.savePolicy(null);
229229
} catch (UnsupportedOperationException ignored) {
230-
Util.logPrintf("Method not implemented");
230+
Util.logPrint("Method not implemented");
231231
} catch (Exception e) {
232232
Util.logPrint("An exception occurred:" + e.getMessage());
233233
return;
@@ -253,7 +253,7 @@ public boolean updatePolicySelf(BooleanSupplier shouldPersist, String sec, Strin
253253
((UpdatableAdapter) adapter).updatePolicy(sec, ptype, oldRule, newRule);
254254
}
255255
} catch (UnsupportedOperationException ignored) {
256-
Util.logPrintf("Method not implemented");
256+
Util.logPrint("Method not implemented");
257257
} catch (Exception e) {
258258
Util.logPrint("An exception occurred:" + e.getMessage());
259259
return false;

src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ boolean addPolicy(String sec, String ptype, List<String> rule) {
8686
try {
8787
adapter.addPolicy(sec, ptype, rule);
8888
} catch (UnsupportedOperationException ignored) {
89-
Util.logPrintf("Method not implemented");
89+
Util.logPrint("Method not implemented");
9090
} catch (Exception e) {
9191
Util.logPrint("An exception occurred:" + e.getMessage());
9292
return false;
@@ -128,7 +128,7 @@ boolean addPolicies(String sec, String ptype, List<List<String>> rules, boolean
128128
((BatchAdapter) adapter).addPolicies(sec, ptype, rules);
129129
}
130130
} catch (UnsupportedOperationException ignored) {
131-
Util.logPrintf("Method not implemented");
131+
Util.logPrint("Method not implemented");
132132
} catch (Exception e) {
133133
Util.logPrint("An exception occurred:" + e.getMessage());
134134
return false;
@@ -165,7 +165,7 @@ boolean removePolicy(String sec, String ptype, List<String> rule) {
165165
try {
166166
adapter.removePolicy(sec, ptype, rule);
167167
} catch (UnsupportedOperationException ignored) {
168-
Util.logPrintf("Method not implemented");
168+
Util.logPrint("Method not implemented");
169169
} catch (Exception e) {
170170
Util.logPrint("An exception occurred:" + e.getMessage());
171171
return false;
@@ -203,7 +203,7 @@ boolean updatePolicy(String sec, String ptype, List<String> oldRule, List<String
203203
try {
204204
((UpdatableAdapter) adapter).updatePolicy(sec, ptype, oldRule, newRule);
205205
} catch (UnsupportedOperationException ignored) {
206-
Util.logPrintf("Method not implemented");
206+
Util.logPrint("Method not implemented");
207207
} catch (Exception e) {
208208
Util.logPrint("An exception occurred:" + e.getMessage());
209209
return false;
@@ -274,7 +274,7 @@ boolean removePolicies(String sec, String ptype, List<List<String>> rules) {
274274
((BatchAdapter) adapter).removePolicies(sec, ptype, rules);
275275
}
276276
} catch (UnsupportedOperationException ignored) {
277-
Util.logPrintf("Method not implemented");
277+
Util.logPrint("Method not implemented");
278278
} catch (Exception e) {
279279
Util.logPrint("An exception occurred:" + e.getMessage());
280280
return false;
@@ -310,7 +310,7 @@ boolean removeFilteredPolicy(String sec, String ptype, int fieldIndex, String...
310310
try {
311311
adapter.removeFilteredPolicy(sec, ptype, fieldIndex, fieldValues);
312312
} catch (UnsupportedOperationException ignored) {
313-
Util.logPrintf("Method not implemented");
313+
Util.logPrint("Method not implemented");
314314
} catch (Exception e) {
315315
Util.logPrint("An exception occurred:" + e.getMessage());
316316
return false;

src/main/java/org/casbin/jcasbin/model/Assertion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected void buildRoleLinks(RoleManager rm) {
7272
this.rm.addLink(rule.get(0), rule.get(1), rule.subList(2, rule.size()).toArray(new String[0]));
7373
}
7474

75-
Util.logPrint("Role links for: " + key);
75+
Util.logPrintfInfo("Role links for: {}", key);
7676
rm.printRoles();
7777
}
7878

@@ -165,7 +165,7 @@ public void buildConditionalRoleLinks(ConditionalRoleManager condRM){
165165
addConditionalRoleLink(rule, domainRule);
166166
}
167167

168-
Util.logPrint("Role links for: " + key);
168+
Util.logPrintfInfo("Role links for: {}", key);
169169
condRM.printRoles();
170170
}
171171

src/main/java/org/casbin/jcasbin/model/Model.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,13 @@ public String saveModelToText() {
316316
* printModel prints the model to the log.
317317
*/
318318
public void printModel() {
319+
if (!Util.isLogPrintEnabled()) {
320+
return;
321+
}
319322
Util.logPrint("Model:");
320323
for (Map.Entry<String, Map<String, Assertion>> entry : model.entrySet()) {
321324
for (Map.Entry<String, Assertion> entry2 : entry.getValue().entrySet()) {
322-
Util.logPrintf("%s.%s: %s", entry.getKey(), entry2.getKey(), entry2.getValue().value);
325+
Util.logPrintfInfo("{}.{}: {}", entry.getKey(), entry2.getKey(), entry2.getValue().value);
323326
}
324327
}
325328
}

src/main/java/org/casbin/jcasbin/model/Policy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public void buildRoleLinks(Map<String, RoleManager> rmMap) {
5151
* printPolicy prints the policy to log.
5252
*/
5353
public void printPolicy() {
54+
if (!Util.isLogPrintEnabled()) {
55+
return;
56+
}
5457
Util.logPrint("Policy:");
5558
if (model.containsKey("p")) {
5659
for (Map.Entry<String, Assertion> entry : model.get("p").entrySet()) {

src/main/java/org/casbin/jcasbin/rbac/DefaultRoleManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,6 @@ public String toString() {
266266
*/
267267
@Override
268268
public void printRoles() {
269-
Util.logPrint(toString());
269+
Util.logPrintfInfo("{}", this);
270270
}
271271
}

src/main/java/org/casbin/jcasbin/rbac/DomainManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,6 @@ public String toString() {
177177

178178
@Override
179179
public void printRoles() {
180-
Util.logPrint(toString());
180+
Util.logPrintfInfo("{}", this);
181181
}
182182
}

src/main/java/org/casbin/jcasbin/util/Util.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ public class Util {
4343

4444
private static final String md5AlgorithmName = "MD5";
4545

46+
/**
47+
* isLogPrintEnabled checks if {@code logPrint} or {@code logPrintfInfo} are enabled.
48+
*
49+
* @return {@code true} if INFO logs are enabled.
50+
*/
51+
public static boolean isLogPrintEnabled() {
52+
return enableLog && LOGGER.isInfoEnabled();
53+
}
54+
4655
/**
4756
* logPrint prints the log.
4857
*
@@ -54,12 +63,25 @@ public static void logPrint(String v) {
5463
}
5564
}
5665

66+
/**
67+
* logPrintfInfo prints the log with the SLF4J format.
68+
*
69+
* @param format the format of the log.
70+
* @param v the log.
71+
*/
72+
public static void logPrintfInfo(String format, Object... v) {
73+
if (enableLog) {
74+
LOGGER.info(format, v);
75+
}
76+
}
77+
5778
/**
5879
* logPrintf prints the log with the format.
5980
*
6081
* @param format the format of the log.
6182
* @param v the log.
6283
*/
84+
@Deprecated
6385
public static void logPrintf(String format, String... v) {
6486
if (enableLog) {
6587
String tmp = String.format(format, (Object[]) v);

0 commit comments

Comments
 (0)