-
-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add DefaultLogger feature (#371)
* fix: sync log form go to java * feat: sync log from go to java
- Loading branch information
Showing
6 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
src/main/java/org/casbin/jcasbin/log/DefaultLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2024 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.casbin.jcasbin.log; | ||
|
||
import java.util.Map; | ||
|
||
public class DefaultLogger implements Logger { | ||
private boolean enabled; | ||
|
||
@Override | ||
public void enableLog(boolean enable) { | ||
this.enabled = enable; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
@Override | ||
public void logModel(String[][] model) { | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
StringBuilder str = new StringBuilder("Model: "); | ||
for (String[] v : model) { | ||
str.append(String.format("%s\n", String.join(", ", v))); | ||
} | ||
|
||
System.out.println(str.toString()); | ||
} | ||
|
||
@Override | ||
public void logEnforce(String matcher, Object[] request, boolean result, String[][] explains) { | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
StringBuilder reqStr = new StringBuilder("Request: "); | ||
for (int i = 0; i < request.length; i++) { | ||
reqStr.append(i != request.length - 1 ? String.format("%s, ", request[i]) : request[i]); | ||
} | ||
reqStr.append(String.format(" ---> %b\n", result)); | ||
|
||
reqStr.append("Hit Policy: "); | ||
for (int i = 0; i < explains.length; i++) { | ||
reqStr.append(i != explains.length - 1 ? String.format("%s, ", String.join(", ", explains[i])) : String.join(", ", explains[i])); | ||
} | ||
|
||
System.out.println(reqStr.toString()); | ||
} | ||
|
||
@Override | ||
public void logPolicy(Map<String, String[][]> policy) { | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
StringBuilder str = new StringBuilder("Policy: "); | ||
for (Map.Entry<String, String[][]> entry : policy.entrySet()) { | ||
str.append(String.format("%s : %s\n", entry.getKey(), arrayToString(entry.getValue()))); | ||
} | ||
|
||
System.out.println(str.toString()); | ||
} | ||
|
||
/** | ||
* tool for logPolicy | ||
* [][] -> String | ||
*/ | ||
private String arrayToString(String[][] array) { | ||
StringBuilder result = new StringBuilder("["); | ||
for (int i = 0; i < array.length; i++) { | ||
result.append("[") | ||
.append(String.join(", ", array[i])) | ||
.append("]"); | ||
if (i < array.length - 1) { | ||
result.append(", "); | ||
} | ||
} | ||
result.append("]"); | ||
return result.toString(); | ||
} | ||
|
||
|
||
@Override | ||
public void logRole(String[] roles) { | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
System.out.println("Roles: " + String.join("\n", roles)); | ||
} | ||
|
||
@Override | ||
public void logError(Throwable err, String... msg) { | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
System.out.println(String.join(" ", msg) + " " + err.getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.casbin.jcasbin.log; | ||
|
||
import java.util.Map; | ||
|
||
public class LogUtil { | ||
private static Logger logger = new DefaultLogger(); | ||
|
||
public static void setLogger(Logger l) { | ||
logger = l; | ||
} | ||
|
||
public static Logger getLogger() { | ||
return logger; | ||
} | ||
|
||
public static void logModel(String[][] model) { | ||
logger.logModel(model); | ||
} | ||
|
||
public static void logEnforce(String matcher, Object[] request, boolean result, String[][] explains) { | ||
logger.logEnforce(matcher, request, result, explains); | ||
} | ||
|
||
public static void logRole(String[] roles) { | ||
logger.logRole(roles); | ||
} | ||
|
||
public static void logPolicy(Map<String, String[][]> policy) { | ||
logger.logPolicy(policy); | ||
} | ||
|
||
public static void logError(Throwable err, String... msg) { | ||
logger.logError(err, msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.casbin.jcasbin.log; | ||
|
||
import java.util.Map; | ||
|
||
public interface Logger { | ||
void enableLog(boolean enable); | ||
|
||
boolean isEnabled(); | ||
|
||
void logModel(String[][] model); | ||
|
||
void logEnforce(String matcher, Object[] request, boolean result, String[][] explains); | ||
|
||
void logRole(String[] roles); | ||
|
||
void logPolicy(Map<String, String[][]> policy); | ||
|
||
void logError(Throwable err, String... msg); | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/org/casbin/jcasbin/log/mocks/MockLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2024 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.casbin.jcasbin.log.mocks; | ||
|
||
import org.casbin.jcasbin.log.Logger; | ||
|
||
import java.util.Map; | ||
|
||
public class MockLogger implements Logger { | ||
private boolean enabled; | ||
|
||
public MockLogger() { | ||
this.enabled = true; | ||
} | ||
|
||
@Override | ||
public void enableLog(boolean enable) { | ||
this.enabled = enable; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
@Override | ||
public void logModel(String[][] model) { | ||
System.out.println("MockLogger - logModel"); | ||
} | ||
|
||
@Override | ||
public void logEnforce(String matcher, Object[] request, boolean result, String[][] explains) { | ||
System.out.println("MockLogger - logEnforce"); | ||
} | ||
|
||
@Override | ||
public void logRole(String[] roles) { | ||
System.out.println("MockLogger - logRole"); | ||
} | ||
|
||
@Override | ||
public void logPolicy(Map<String, String[][]> policy) { | ||
System.out.println("MockLogger - logPolicy"); | ||
} | ||
|
||
@Override | ||
public void logError(Throwable err, String... msg) { | ||
System.out.println("MockLogger - logError"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2024 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.casbin.jcasbin.main; | ||
|
||
import org.casbin.jcasbin.log.LogUtil; | ||
import org.casbin.jcasbin.log.mocks.MockLogger; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentMatchers; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class LogUtilTest { | ||
|
||
@Test | ||
public void testLogUtil() { | ||
MockLogger mockLogger = Mockito.mock(MockLogger.class); | ||
|
||
LogUtil.setLogger(mockLogger); | ||
|
||
Mockito.when(mockLogger.isEnabled()).thenReturn(true); | ||
|
||
LogUtil.logModel(new String[][]{{"data1", "data2"}}); | ||
LogUtil.logEnforce("matcher", new Object[]{"request"}, true, new String[][]{{"explain1", "explain2"}}); | ||
LogUtil.logRole(new String[]{"role1", "role2"}); | ||
|
||
Map<String, String[][]> policy = new HashMap<>(); | ||
policy.put("key1", new String[][]{{"value1"}}); | ||
policy.put("key2", new String[][]{{"value2"}}); | ||
|
||
LogUtil.logPolicy(policy); | ||
LogUtil.logError(new RuntimeException("Test Error"), "Error Message"); | ||
|
||
Mockito.verify(mockLogger).logModel(ArgumentMatchers.eq(new String[][]{{"data1", "data2"}})); | ||
Mockito.verify(mockLogger).logEnforce( | ||
ArgumentMatchers.eq("matcher"), | ||
ArgumentMatchers.eq(new Object[]{"request"}), | ||
ArgumentMatchers.eq(true), | ||
ArgumentMatchers.eq(new String[][]{{"explain1", "explain2"}}) | ||
); | ||
Mockito.verify(mockLogger).logRole(ArgumentMatchers.eq(new String[]{"role1", "role2"})); | ||
Mockito.verify(mockLogger).logPolicy(ArgumentMatchers.eq(policy)); | ||
Mockito.verify(mockLogger).logError( | ||
Mockito.any(RuntimeException.class), | ||
ArgumentMatchers.eq("Error Message") | ||
); | ||
} | ||
} |