Skip to content

Commit 2d94e00

Browse files
committed
Merge branch 'jeremiahjstacey-JLF_Always' from PR#572 into develop
2 parents 84db3c3 + f7cdb19 commit 2d94e00

File tree

6 files changed

+75
-5
lines changed

6 files changed

+75
-5
lines changed

src/main/java/org/owasp/esapi/Logger.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ public String toString() {
401401

402402
/**
403403
* Log an event regardless of what logging level is enabled.
404+
* <br>
405+
* Note that logging will not occur if the underlying logging implementation has logging disabled.
404406
*
405407
* @param type
406408
* the type of event
@@ -412,6 +414,8 @@ public String toString() {
412414
/**
413415
* Log an event regardless of what logging level is enabled
414416
* and also record the stack trace associated with the event.
417+
* <br>
418+
* Note that logging will not occur if the underlying logging implementation has logging disabled.
415419
*
416420
* @param type
417421
* the type of event
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* OWASP Enterprise Security API (ESAPI)
3+
*
4+
* This file is part of the Open Web Application Security Project (OWASP)
5+
* Enterprise Security API (ESAPI) project. For details, please see
6+
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
7+
*
8+
* Copyright (c) 2007 - The OWASP Foundation
9+
*
10+
* The ESAPI is published by OWASP under the BSD license. You should read and accept the
11+
* LICENSE before you use, modify, and/or redistribute this software.
12+
*
13+
* @created 2019
14+
*/
15+
16+
package org.owasp.esapi.logging.java;
17+
18+
import java.util.logging.Level;
19+
20+
/**
21+
* Definitions of customized Java Logging Level options to map ESAPI behavior to the desired Java Log output behaviors.
22+
*/
23+
public class ESAPICustomJavaLevel extends Level {
24+
25+
protected static final long serialVersionUID = 1L;
26+
27+
/**
28+
* Defines a custom error level below SEVERE but above WARNING since this level isn't defined directly
29+
* by java.util.Logger already.
30+
*/
31+
public static final Level ERROR_LEVEL = new ESAPICustomJavaLevel( "ERROR", Level.SEVERE.intValue() - 1);
32+
33+
/**
34+
* Defines a custom level that should result in content always being recorded, unless the Java Logging configuration is set to OFF.
35+
*/
36+
public static final Level ALWAYS_LEVEL = new ESAPICustomJavaLevel( "ALWAYS", Level.OFF.intValue() - 1);
37+
38+
/**
39+
* Constructs an instance of a JavaLoggerLevel which essentially provides a mapping between the name of
40+
* the defined level and its numeric value.
41+
*
42+
* @param name The name of the JavaLoggerLevel
43+
* @param value The associated numeric value
44+
*/
45+
private ESAPICustomJavaLevel(String name, int value) {
46+
super(name, value);
47+
}
48+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
/**
2121
* A custom logging level defined between Level.SEVERE and Level.WARNING in logger.
22+
* @deprecated 10/24/2020 : References should use ESAPICustomJavaLevel.ERROR_LEVEL
23+
* @see ESAPICustomJavaLevel#ERROR_LEVEL
2224
*/
2325
public class ESAPIErrorJavaLevel extends Level {
2426

@@ -27,8 +29,10 @@ public class ESAPIErrorJavaLevel extends Level {
2729
/**
2830
* Defines a custom error level below SEVERE but above WARNING since this level isn't defined directly
2931
* by java.util.Logger already.
32+
* @deprecated
33+
* @see ESAPICustomJavaLevel#ERROR_LEVEL
3034
*/
31-
public static final Level ERROR_LEVEL = new ESAPIErrorJavaLevel( "ERROR", Level.SEVERE.intValue() - 1);
35+
public static final Level ERROR_LEVEL = new ESAPIErrorJavaLevel( ESAPICustomJavaLevel.ERROR_LEVEL.getName(),ESAPICustomJavaLevel.ERROR_LEVEL.intValue());
3236

3337
/**
3438
* Constructs an instance of a JavaLoggerLevel which essentially provides a mapping between the name of

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class JavaLogFactory implements LogFactory {
6767
JAVA_LOG_APPENDER = createLogAppender(logUserInfo, logClientInfo, logServerIp, logApplicationName, appName);
6868

6969
Map<Integer, JavaLogLevelHandler> levelLookup = new HashMap<>();
70-
levelLookup.put(Logger.ALL, JavaLogLevelHandlers.ALL);
70+
levelLookup.put(Logger.ALL, JavaLogLevelHandlers.ALWAYS);
7171
levelLookup.put(Logger.TRACE, JavaLogLevelHandlers.FINEST);
7272
levelLookup.put(Logger.DEBUG, JavaLogLevelHandlers.FINE);
7373
levelLookup.put(Logger.INFO, JavaLogLevelHandlers.INFO);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public enum JavaLogLevelHandlers implements JavaLogLevelHandler {
2626
FINE(Level.FINE),
2727
FINER(Level.FINER),
2828
FINEST(Level.FINEST),
29-
ALL(Level.ALL),
30-
ERROR(ESAPIErrorJavaLevel.ERROR_LEVEL);
29+
ALWAYS(ESAPICustomJavaLevel.ALWAYS_LEVEL),
30+
ERROR(ESAPICustomJavaLevel.ERROR_LEVEL);
3131

3232
private final Level level;
3333

src/test/java/org/owasp/esapi/logging/java/JavaLogLevelHandlersTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,28 @@ public void testErrorDelegation() {
3636
JavaLogLevelHandlers.ERROR.log(mockLogger, testName.getMethodName());
3737
JavaLogLevelHandlers.ERROR.log(mockLogger, testName.getMethodName(), testException);
3838

39-
Level expectedJavaLevel = ESAPIErrorJavaLevel.ERROR_LEVEL;
39+
Level expectedJavaLevel = ESAPICustomJavaLevel.ERROR_LEVEL;
4040

4141
Mockito.verify(mockLogger, Mockito.times(1)).isLoggable(expectedJavaLevel);
4242
Mockito.verify(mockLogger, Mockito.times(1)).log(expectedJavaLevel, testName.getMethodName());
4343
Mockito.verify(mockLogger, Mockito.times(1)).log(expectedJavaLevel, testName.getMethodName(), testException);
4444
Mockito.verifyNoMoreInteractions(mockLogger);
4545
}
4646

47+
@Test
48+
public void testAlwaysDelegation() {
49+
JavaLogLevelHandlers.ALWAYS.isEnabled(mockLogger);
50+
JavaLogLevelHandlers.ALWAYS.log(mockLogger, testName.getMethodName());
51+
JavaLogLevelHandlers.ALWAYS.log(mockLogger, testName.getMethodName(), testException);
52+
53+
Level expectedJavaLevel = ESAPICustomJavaLevel.ALWAYS_LEVEL;
54+
55+
Mockito.verify(mockLogger, Mockito.times(1)).isLoggable(expectedJavaLevel);
56+
Mockito.verify(mockLogger, Mockito.times(1)).log(expectedJavaLevel, testName.getMethodName());
57+
Mockito.verify(mockLogger, Mockito.times(1)).log(expectedJavaLevel, testName.getMethodName(), testException);
58+
Mockito.verifyNoMoreInteractions(mockLogger);
59+
}
60+
4761
@Test
4862
public void testWarnDelegation() {
4963
JavaLogLevelHandlers.WARNING.isEnabled(mockLogger);

0 commit comments

Comments
 (0)