Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
43e7ccd6
Commit
43e7ccd6
authored
May 05, 2020
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Detect log4j2-test.* files when using log4J2
Fixes gh-17001
parent
98d27db7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
9 deletions
+41
-9
Log4J2LoggingSystem.java
...ingframework/boot/logging/log4j2/Log4J2LoggingSystem.java
+12
-0
Log4J2LoggingSystemTests.java
...amework/boot/logging/log4j2/Log4J2LoggingSystemTests.java
+29
-9
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java
View file @
43e7ccd6
...
@@ -109,6 +109,7 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
...
@@ -109,6 +109,7 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
private
String
[]
getCurrentlySupportedConfigLocations
()
{
private
String
[]
getCurrentlySupportedConfigLocations
()
{
List
<
String
>
supportedConfigLocations
=
new
ArrayList
<>();
List
<
String
>
supportedConfigLocations
=
new
ArrayList
<>();
addTestFiles
(
supportedConfigLocations
);
supportedConfigLocations
.
add
(
"log4j2.properties"
);
supportedConfigLocations
.
add
(
"log4j2.properties"
);
if
(
isClassAvailable
(
"com.fasterxml.jackson.dataformat.yaml.YAMLParser"
))
{
if
(
isClassAvailable
(
"com.fasterxml.jackson.dataformat.yaml.YAMLParser"
))
{
Collections
.
addAll
(
supportedConfigLocations
,
"log4j2.yaml"
,
"log4j2.yml"
);
Collections
.
addAll
(
supportedConfigLocations
,
"log4j2.yaml"
,
"log4j2.yml"
);
...
@@ -120,6 +121,17 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
...
@@ -120,6 +121,17 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
return
StringUtils
.
toStringArray
(
supportedConfigLocations
);
return
StringUtils
.
toStringArray
(
supportedConfigLocations
);
}
}
private
void
addTestFiles
(
List
<
String
>
supportedConfigLocations
)
{
supportedConfigLocations
.
add
(
"log4j2-test.properties"
);
if
(
isClassAvailable
(
"com.fasterxml.jackson.dataformat.yaml.YAMLParser"
))
{
Collections
.
addAll
(
supportedConfigLocations
,
"log4j2-test.yaml"
,
"log4j2-test.yml"
);
}
if
(
isClassAvailable
(
"com.fasterxml.jackson.databind.ObjectMapper"
))
{
Collections
.
addAll
(
supportedConfigLocations
,
"log4j2-test.json"
,
"log4j2-test.jsn"
);
}
supportedConfigLocations
.
add
(
"log4j2-test.xml"
);
}
protected
boolean
isClassAvailable
(
String
className
)
{
protected
boolean
isClassAvailable
(
String
className
)
{
return
ClassUtils
.
isPresent
(
className
,
getClassLoader
());
return
ClassUtils
.
isPresent
(
className
,
getClassLoader
());
}
}
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java
View file @
43e7ccd6
...
@@ -42,6 +42,7 @@ import org.springframework.boot.logging.LoggingSystem;
...
@@ -42,6 +42,7 @@ import org.springframework.boot.logging.LoggingSystem;
import
org.springframework.boot.logging.LoggingSystemProperties
;
import
org.springframework.boot.logging.LoggingSystemProperties
;
import
org.springframework.boot.testsupport.system.CapturedOutput
;
import
org.springframework.boot.testsupport.system.CapturedOutput
;
import
org.springframework.boot.testsupport.system.OutputCaptureExtension
;
import
org.springframework.boot.testsupport.system.OutputCaptureExtension
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
@@ -59,6 +60,7 @@ import static org.mockito.Mockito.verify;
...
@@ -59,6 +60,7 @@ import static org.mockito.Mockito.verify;
* @author Phillip Webb
* @author Phillip Webb
* @author Andy Wilkinson
* @author Andy Wilkinson
* @author Ben Hale
* @author Ben Hale
* @author Madhura Bhave
*/
*/
@ExtendWith
(
OutputCaptureExtension
.
class
)
@ExtendWith
(
OutputCaptureExtension
.
class
)
class
Log4J2LoggingSystemTests
extends
AbstractLoggingSystemTests
{
class
Log4J2LoggingSystemTests
extends
AbstractLoggingSystemTests
{
...
@@ -101,7 +103,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
...
@@ -101,7 +103,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
void
withFile
(
CapturedOutput
output
)
{
void
withFile
(
CapturedOutput
output
)
{
this
.
loggingSystem
.
beforeInitialize
();
this
.
loggingSystem
.
beforeInitialize
();
this
.
logger
.
info
(
"Hidden"
);
this
.
logger
.
info
(
"Hidden"
);
this
.
loggingSystem
.
initialize
(
null
,
null
,
getLogFile
(
null
,
tmpDir
()));
this
.
loggingSystem
.
initialize
(
null
,
getRelativeClasspathLocation
(
"log4j2-file.xml"
),
getLogFile
(
null
,
tmpDir
()));
this
.
logger
.
info
(
"Hello world"
);
this
.
logger
.
info
(
"Hello world"
);
Configuration
configuration
=
this
.
loggingSystem
.
getConfiguration
();
Configuration
configuration
=
this
.
loggingSystem
.
getConfiguration
();
assertThat
(
output
).
contains
(
"Hello world"
).
doesNotContain
(
"Hidden"
);
assertThat
(
output
).
contains
(
"Hello world"
).
doesNotContain
(
"Hidden"
);
...
@@ -197,39 +200,47 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
...
@@ -197,39 +200,47 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@Test
@Test
void
configLocationsWithNoExtraDependencies
()
{
void
configLocationsWithNoExtraDependencies
()
{
assertThat
(
this
.
loggingSystem
.
getStandardConfigLocations
()).
contains
(
"log4j2.properties"
,
"log4j2.xml"
);
assertThat
(
this
.
loggingSystem
.
getStandardConfigLocations
()).
contains
(
"log4j2-test.properties"
,
"log4j2-test.xml"
,
"log4j2.properties"
,
"log4j2.xml"
);
}
}
@Test
@Test
void
configLocationsWithJacksonDatabind
()
{
void
configLocationsWithJacksonDatabind
()
{
this
.
loggingSystem
.
availableClasses
(
ObjectMapper
.
class
.
getName
());
this
.
loggingSystem
.
availableClasses
(
ObjectMapper
.
class
.
getName
());
assertThat
(
this
.
loggingSystem
.
getStandardConfigLocations
()).
contains
(
"log4j2.json"
,
"log4j2.jsn"
,
"log4j2.xml"
);
assertThat
(
this
.
loggingSystem
.
getStandardConfigLocations
()).
containsExactly
(
"log4j2-test.properties"
,
"log4j2-test.json"
,
"log4j2-test.jsn"
,
"log4j2-test.xml"
,
"log4j2.properties"
,
"log4j2.json"
,
"log4j2.jsn"
,
"log4j2.xml"
);
}
}
@Test
@Test
void
configLocationsWithJacksonDataformatYaml
()
{
void
configLocationsWithJacksonDataformatYaml
()
{
this
.
loggingSystem
.
availableClasses
(
"com.fasterxml.jackson.dataformat.yaml.YAMLParser"
);
this
.
loggingSystem
.
availableClasses
(
"com.fasterxml.jackson.dataformat.yaml.YAMLParser"
);
assertThat
(
this
.
loggingSystem
.
getStandardConfigLocations
()).
contains
(
"log4j2.yaml"
,
"log4j2.yml"
,
"log4j2.xml"
);
assertThat
(
this
.
loggingSystem
.
getStandardConfigLocations
()).
containsExactly
(
"log4j2-test.properties"
,
"log4j2-test.yaml"
,
"log4j2-test.yml"
,
"log4j2-test.xml"
,
"log4j2.properties"
,
"log4j2.yaml"
,
"log4j2.yml"
,
"log4j2.xml"
);
}
}
@Test
@Test
void
configLocationsWithJacksonDatabindAndDataformatYaml
()
{
void
configLocationsWithJacksonDatabindAndDataformatYaml
()
{
this
.
loggingSystem
.
availableClasses
(
"com.fasterxml.jackson.dataformat.yaml.YAMLParser"
,
this
.
loggingSystem
.
availableClasses
(
"com.fasterxml.jackson.dataformat.yaml.YAMLParser"
,
ObjectMapper
.
class
.
getName
());
ObjectMapper
.
class
.
getName
());
assertThat
(
this
.
loggingSystem
.
getStandardConfigLocations
()).
contains
(
"log4j2.yaml"
,
"log4j2.yml"
,
"log4j2.json"
,
assertThat
(
this
.
loggingSystem
.
getStandardConfigLocations
()).
containsExactly
(
"log4j2-test.properties"
,
"log4j2.jsn"
,
"log4j2.xml"
);
"log4j2-test.yaml"
,
"log4j2-test.yml"
,
"log4j2-test.json"
,
"log4j2-test.jsn"
,
"log4j2-test.xml"
,
"log4j2.properties"
,
"log4j2.yaml"
,
"log4j2.yml"
,
"log4j2.json"
,
"log4j2.jsn"
,
"log4j2.xml"
);
}
}
@Test
@Test
void
springConfigLocations
()
{
void
springConfigLocations
()
{
String
[]
locations
=
getSpringConfigLocations
(
this
.
loggingSystem
);
String
[]
locations
=
getSpringConfigLocations
(
this
.
loggingSystem
);
assertThat
(
locations
).
containsExactly
(
"log4j2-spring.properties"
,
"log4j2-spring.xml"
);
assertThat
(
locations
).
containsExactly
(
"log4j2-test-spring.properties"
,
"log4j2-test-spring.xml"
,
"log4j2-spring.properties"
,
"log4j2-spring.xml"
);
}
}
@Test
@Test
void
exceptionsIncludeClassPackaging
(
CapturedOutput
output
)
{
void
exceptionsIncludeClassPackaging
(
CapturedOutput
output
)
{
this
.
loggingSystem
.
beforeInitialize
();
this
.
loggingSystem
.
beforeInitialize
();
this
.
loggingSystem
.
initialize
(
null
,
null
,
getLogFile
(
null
,
tmpDir
()));
this
.
loggingSystem
.
initialize
(
null
,
getRelativeClasspathLocation
(
"log4j2-file.xml"
),
getLogFile
(
null
,
tmpDir
()));
this
.
logger
.
warn
(
"Expected exception"
,
new
RuntimeException
(
"Expected"
));
this
.
logger
.
warn
(
"Expected exception"
,
new
RuntimeException
(
"Expected"
));
String
fileContents
=
contentOf
(
new
File
(
tmpDir
()
+
"/spring.log"
));
String
fileContents
=
contentOf
(
new
File
(
tmpDir
()
+
"/spring.log"
));
assertThat
(
fileContents
).
contains
(
"[junit-"
);
assertThat
(
fileContents
).
contains
(
"[junit-"
);
...
@@ -249,7 +260,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
...
@@ -249,7 +260,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
try
{
try
{
this
.
loggingSystem
.
beforeInitialize
();
this
.
loggingSystem
.
beforeInitialize
();
this
.
logger
.
info
(
"Hidden"
);
this
.
logger
.
info
(
"Hidden"
);
this
.
loggingSystem
.
initialize
(
null
,
null
,
getLogFile
(
null
,
tmpDir
()));
this
.
loggingSystem
.
initialize
(
null
,
getRelativeClasspathLocation
(
"log4j2-file.xml"
),
getLogFile
(
null
,
tmpDir
()));
this
.
logger
.
warn
(
"Expected exception"
,
new
RuntimeException
(
"Expected"
,
new
RuntimeException
(
"Cause"
)));
this
.
logger
.
warn
(
"Expected exception"
,
new
RuntimeException
(
"Expected"
,
new
RuntimeException
(
"Cause"
)));
String
fileContents
=
contentOf
(
new
File
(
tmpDir
()
+
"/spring.log"
));
String
fileContents
=
contentOf
(
new
File
(
tmpDir
()
+
"/spring.log"
));
assertThat
(
fileContents
).
contains
(
"java.lang.RuntimeException: Expected"
).
doesNotContain
(
"Wrapped by:"
);
assertThat
(
fileContents
).
contains
(
"java.lang.RuntimeException: Expected"
).
doesNotContain
(
"Wrapped by:"
);
...
@@ -278,6 +290,14 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
...
@@ -278,6 +290,14 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
verify
(
listener
,
times
(
4
)).
propertyChange
(
any
(
PropertyChangeEvent
.
class
));
verify
(
listener
,
times
(
4
)).
propertyChange
(
any
(
PropertyChangeEvent
.
class
));
}
}
private
String
getRelativeClasspathLocation
(
String
fileName
)
{
String
defaultPath
=
ClassUtils
.
getPackageName
(
getClass
());
defaultPath
=
defaultPath
.
replace
(
'.'
,
'/'
);
defaultPath
=
defaultPath
+
"/"
+
fileName
;
defaultPath
=
"classpath:"
+
defaultPath
;
return
defaultPath
;
}
static
class
TestLog4J2LoggingSystem
extends
Log4J2LoggingSystem
{
static
class
TestLog4J2LoggingSystem
extends
Log4J2LoggingSystem
{
private
List
<
String
>
availableClasses
=
new
ArrayList
<>();
private
List
<
String
>
availableClasses
=
new
ArrayList
<>();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment