Skip to content

Commit 16f7ee1

Browse files
Defaulting ESAPI to Java Logger
Updating ESAPI.properties to use the Java Logger. Providing a test-scope property file to improve the log content and format for the java implementation. Updating the JavaLogFactory to read and apply the properties file.
1 parent e553829 commit 16f7ee1

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/main/java/org/owasp/esapi/logging/java/JavaLogFactory.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
*/
1515
package org.owasp.esapi.logging.java;
1616

17+
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.io.OutputStream;
20+
import java.io.PrintStream;
1721
import java.util.ArrayList;
1822
import java.util.HashMap;
1923
import java.util.List;
2024
import java.util.Map;
25+
import java.util.logging.LogManager;
2126

2227
import org.owasp.esapi.ESAPI;
2328
import org.owasp.esapi.LogFactory;
@@ -67,6 +72,29 @@ public class JavaLogFactory implements LogFactory {
6772
//LEVEL.OFF not used. If it's off why would we try to log it?
6873

6974
LOG_BRIDGE = new JavaLogBridgeImpl(JAVA_LOG_APPENDER, JAVA_LOG_SCRUBBER, levelLookup);
75+
76+
/*
77+
* This will load the logging properties file to control the format of the output for Java logs.
78+
*/
79+
try (InputStream stream = JavaLogFactory.class.getClassLoader().
80+
getResourceAsStream("esapi-java-logging.properties")) {
81+
LogManager.getLogManager().readConfiguration(stream);
82+
} catch (IOException ioe) {
83+
ioe.printStackTrace();
84+
}
85+
86+
/*
87+
* This is a convenience for me -- turns off all System.out.println cruft in the terminal so I can view the logs.
88+
* FIXME: Probably need to remove this before the end of the effort.
89+
*/
90+
/*
91+
System.setOut(new PrintStream(new OutputStream() {
92+
public void write(int b) {
93+
//DO NOTHING
94+
}
95+
}));
96+
97+
*/
7098
}
7199

72100
/**
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
handlers= java.util.logging.ConsoleHandler
2+
.level= INFO
3+
java.util.logging.ConsoleHandler.level = INFO
4+
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
5+
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%3$-7s] %5$s %n
6+
#https://www.logicbig.com/tutorials/core-java-tutorial/logging/customizing-default-format.html

src/test/resources/esapi/ESAPI.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ ESAPI.Executor=org.owasp.esapi.reference.DefaultExecutor
9797
ESAPI.HTTPUtilities=org.owasp.esapi.reference.DefaultHTTPUtilities
9898
ESAPI.IntrusionDetector=org.owasp.esapi.reference.DefaultIntrusionDetector
9999
# Log4JFactory Requires log4j.xml or log4j.properties in classpath - http://www.laliluna.de/log4j-tutorial.html
100-
ESAPI.Logger=org.owasp.esapi.reference.Log4JLogFactory
100+
ESAPI.Logger=org.owasp.esapi.logging.java.JavaLogFactory
101101
#ESAPI.Logger=org.owasp.esapi.reference.JavaLogFactory
102102
#ESAPI.Logger=org.owasp.esapi.reference.ExampleExtendedLog4JLogFactory
103103
# To use the new SLF4J logger in ESAPI (see GitHub issue #129), set

0 commit comments

Comments
 (0)