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
1dbc94d0
Commit
1dbc94d0
authored
Sep 15, 2014
by
Daniel Fullarton
Committed by
Andy Wilkinson
Oct 08, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a logging system, starter and sample for Log4j 2
Closes gh-1565
parent
8e000f3d
Changes
25
Hide whitespace changes
Inline
Side-by-side
Showing
25 changed files
with
673 additions
and
15 deletions
+673
-15
pom.xml
spring-boot-dependencies/pom.xml
+21
-0
howto.adoc
spring-boot-docs/src/main/asciidoc/howto.adoc
+15
-11
pom.xml
spring-boot-samples/pom.xml
+1
-0
pom.xml
...ng-boot-samples/spring-boot-sample-actuator-log4j/pom.xml
+2
-2
pom.xml
...g-boot-samples/spring-boot-sample-actuator-log4j2/pom.xml
+60
-0
HelloWorldService.java
...c/main/java/sample/actuator/log4j2/HelloWorldService.java
+32
-0
SampleActuatorApplication.java
...ava/sample/actuator/log4j2/SampleActuatorApplication.java
+35
-0
SampleController.java
...rc/main/java/sample/actuator/log4j2/SampleController.java
+45
-0
ServiceProperties.java
...c/main/java/sample/actuator/log4j2/ServiceProperties.java
+36
-0
application.properties
...actuator-log4j2/src/main/resources/application.properties
+17
-0
log4j2.xml
...boot-sample-actuator-log4j2/src/main/resources/log4j2.xml
+22
-0
SampleActuatorApplicationTests.java
...ample/actuator/log4j2/SampleActuatorApplicationTests.java
+61
-0
pom.xml
spring-boot-starters/pom.xml
+1
-0
pom.xml
spring-boot-starters/spring-boot-starter-log4j2/pom.xml
+42
-0
spring.provides
...tarter-log4j2/src/main/resources/META-INF/spring.provides
+1
-0
pom.xml
spring-boot/pom.xml
+10
-0
LoggingSystem.java
.../java/org/springframework/boot/logging/LoggingSystem.java
+2
-0
Log4JLoggingSystem.java
...pringframework/boot/logging/log4j/Log4JLoggingSystem.java
+1
-1
package-info.java
.../org/springframework/boot/logging/log4j/package-info.java
+1
-1
Log4J2LoggingSystem.java
...ingframework/boot/logging/log4j2/Log4J2LoggingSystem.java
+74
-0
package-info.java
...org/springframework/boot/logging/log4j2/package-info.java
+21
-0
basic-log4j2.xml
.../org/springframework/boot/logging/log4j2/basic-log4j2.xml
+25
-0
log4j2.xml
...ources/org/springframework/boot/logging/log4j2/log4j2.xml
+36
-0
Log4J2LoggingSystemTests.java
...amework/boot/logging/log4j2/Log4J2LoggingSystemTests.java
+93
-0
log4j2-nondefault.xml
spring-boot/src/test/resources/log4j2-nondefault.xml
+19
-0
No files found.
spring-boot-dependencies/pom.xml
View file @
1dbc94d0
...
...
@@ -93,6 +93,7 @@
<junit.version>
4.11
</junit.version>
<liquibase.version>
3.0.8
</liquibase.version>
<log4j.version>
1.2.17
</log4j.version>
<log4j2.version>
2.0.2
</log4j2.version>
<logback.version>
1.1.2
</logback.version>
<mockito.version>
1.9.5
</mockito.version>
<mongodb.version>
2.12.3
</mongodb.version>
...
...
@@ -287,6 +288,11 @@
<artifactId>
spring-boot-starter-log4j
</artifactId>
<version>
1.2.0.BUILD-SNAPSHOT
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-log4j2
</artifactId>
<version>
1.2.0.BUILD-SNAPSHOT
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-logging
</artifactId>
...
...
@@ -562,6 +568,21 @@
<artifactId>
log4j
</artifactId>
<version>
${log4j.version}
</version>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-api
</artifactId>
<version>
${log4j2.version}
</version>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-core
</artifactId>
<version>
${log4j2.version}
</version>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-slf4j-impl
</artifactId>
<version>
${log4j2.version}
</version>
</dependency>
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
...
...
spring-boot-docs/src/main/asciidoc/howto.adoc
View file @
1dbc94d0
...
...
@@ -961,14 +961,15 @@ If Groovy is on the classpath you should be able to configure Logback with
[[howto-configure-log4j-for-logging]]
=== Configure Log4j for logging
Spring Boot supports http://logging.apache.org/log4j[Log4j] for logging
configuration, but it has to be on the classpath. If you are using the starter poms for
assembling dependencies that means you have to exclude logback and then include log4j
instead. If you aren't using the starter poms then you need to provide `commons-logging`
(at least) in addition to Log4j.
Spring Boot also supports either http://logging.apache.org/log4j/1.2[Log4j] or
http://logging.apache.org/log4j/2.x[Log4j 2] for logging configuration, but only if one
of them is on the classpath. If you are using the starter poms for assembling
dependencies that means you have to exclude Logback and then include your chosen version
of Log4j instead. If you aren't using the starter poms then you need to provide
`commons-logging` (at least) in addition to your chosen version of Log4j.
The simplest path
to using Log4j is probably through the starter poms, even though it
requires some jiggling with excludes,
e.g. in Maven:
The simplest path
is probably through the starter poms, even though it requires some
jiggling with excludes, .
e.g. in Maven:
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
----
...
...
@@ -992,10 +993,13 @@ requires some jiggling with excludes, e.g. in Maven:
</dependency>
----
NOTE: The use of the log4j starter gathers together the dependencies for common logging
requirements (e.g. including having Tomcat use `java.util.logging` but configure the
output using Log4j). See the Actuator Log4j Sample for more detail and to see it in
action.
To use Log4j 2, simply depend on `spring-boot-starter-log4j2` rather than
`spring-boot-starter-log4j`
NOTE: The use of the one of the Log4j starters gathers together the dependencies for
common logging requirements (e.g. including having Tomcat use `java.util.logging` but
configure the output using Log4j or Log4j 2). See the Actuator Log4j or Log4j 2
samples for more detail and to see it in action.
...
...
spring-boot-samples/pom.xml
View file @
1dbc94d0
...
...
@@ -22,6 +22,7 @@
<modules>
<module>
spring-boot-sample-actuator
</module>
<module>
spring-boot-sample-actuator-log4j
</module>
<module>
spring-boot-sample-actuator-log4j2
</module>
<module>
spring-boot-sample-actuator-noweb
</module>
<module>
spring-boot-sample-actuator-ui
</module>
<module>
spring-boot-sample-amqp
</module>
...
...
spring-boot-samples/spring-boot-sample-actuator-log4j/pom.xml
View file @
1dbc94d0
...
...
@@ -8,8 +8,8 @@
<version>
1.2.0.BUILD-SNAPSHOT
</version>
</parent>
<artifactId>
spring-boot-sample-actuator-log4j
</artifactId>
<name>
Spring Boot Actuator Log4
J
Sample
</name>
<description>
Spring Boot Actuator Log4
J
Sample
</description>
<name>
Spring Boot Actuator Log4
j
Sample
</name>
<description>
Spring Boot Actuator Log4
j
Sample
</description>
<url>
http://projects.spring.io/spring-boot/
</url>
<organization>
<name>
Pivotal Software, Inc.
</name>
...
...
spring-boot-samples/spring-boot-sample-actuator-log4j2/pom.xml
0 → 100644
View file @
1dbc94d0
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-samples
</artifactId>
<version>
1.2.0.BUILD-SNAPSHOT
</version>
</parent>
<artifactId>
spring-boot-sample-actuator-log4j2
</artifactId>
<name>
Spring Boot Actuator Log4j 2 Sample
</name>
<description>
Spring Boot Actuator Log4j 2 Sample
</description>
<url>
http://projects.spring.io/spring-boot/
</url>
<organization>
<name>
Pivotal Software, Inc.
</name>
<url>
http://www.spring.io
</url>
</organization>
<properties>
<main.basedir>
${basedir}/../..
</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
<exclusions>
<exclusion>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-logging
</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-log4j2
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
<exclusions>
<exclusion>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-logging
</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
</plugins>
</build>
</project>
spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/HelloWorldService.java
0 → 100644
View file @
1dbc94d0
/*
* Copyright 2012-2013 the original author or authors.
*
* 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
sample
.
actuator
.
log4j2
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
@Component
public
class
HelloWorldService
{
@Autowired
private
ServiceProperties
configuration
;
public
String
getHelloMessage
()
{
return
"Hello "
+
this
.
configuration
.
getName
();
}
}
spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleActuatorApplication.java
0 → 100644
View file @
1dbc94d0
/*
* Copyright 2012-2013 the original author or authors.
*
* 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
sample
.
actuator
.
log4j2
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.ComponentScan
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@ComponentScan
public
class
SampleActuatorApplication
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
SpringApplication
.
run
(
SampleActuatorApplication
.
class
,
args
);
}
}
spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleController.java
0 → 100644
View file @
1dbc94d0
/*
* Copyright 2012-2013 the original author or authors.
*
* 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
sample
.
actuator
.
log4j2
;
import
java.util.Collections
;
import
java.util.Map
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
@Controller
public
class
SampleController
{
@Autowired
private
HelloWorldService
helloWorldService
;
@RequestMapping
(
"/"
)
@ResponseBody
public
Map
<
String
,
String
>
helloWorld
()
{
return
Collections
.
singletonMap
(
"message"
,
this
.
helloWorldService
.
getHelloMessage
());
}
@RequestMapping
(
"/foo"
)
@ResponseBody
public
String
foo
()
{
throw
new
IllegalArgumentException
(
"Server error"
);
}
}
spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/ServiceProperties.java
0 → 100644
View file @
1dbc94d0
/*
* Copyright 2012-2014 the original author or authors.
*
* 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
sample
.
actuator
.
log4j2
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.stereotype.Component
;
@ConfigurationProperties
(
prefix
=
"service"
,
ignoreUnknownFields
=
false
)
@Component
public
class
ServiceProperties
{
private
String
name
=
"World"
;
public
String
getName
()
{
return
this
.
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
}
spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/resources/application.properties
0 → 100644
View file @
1dbc94d0
logging.file
:
/tmp/logs/app.log
#server.port: 8080
#management.port: 8080
management.address
:
127.0.0.1
endpoints.shutdown.enabled
:
true
server.tomcat.basedir
:
target/tomcat
server.tomcat.access_log_pattern
:
%h %t "%r" %s %b
security.require_ssl
:
false
service.name
:
Daniel
shell.ssh.enabled
:
true
shell.ssh.port
:
2222
#shell.telnet.enabled: false
#shell.telnet.port: 1111
shell.auth
:
spring
#shell.auth: key
#shell.auth.key.path: ${user.home}/test/id_rsa.pub.pem
#shell.auth: simple
spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/resources/log4j2.xml
0 → 100644
View file @
1dbc94d0
<?xml version="1.0" encoding="UTF-8"?>
<Configuration
status=
"WARN"
>
<Properties>
<Property
name=
"PID"
>
????
</Property>
<Property
name=
"LOG_PATTERN"
>
[%d{yyyy-MM-dd HH:mm:ss.SSS}] log4j2%X{context} - ${sys:PID} %5p [%t] --- %c{1}: %m%n
</Property>
</Properties>
<Appenders>
<Console
name=
"Console"
target=
"SYSTEM_OUT"
follow=
"true"
>
<PatternLayout
pattern=
"${LOG_PATTERN}"
/>
</Console>
</Appenders>
<Loggers>
<Logger
name=
"org.hibernate.validator.internal.util.Version"
level=
"warn"
/>
<Logger
name=
"org.apache.coyote.http11.Http11NioProtocol"
level=
"warn"
/>
<Logger
name=
"org.apache.tomcat.util.net.NioSelectorPool"
level=
"warn"
/>
<Logger
name=
"org.apache.catalina.startup.DigesterFactory"
level=
"error"
/>
<Root
level=
"info"
>
<AppenderRef
ref=
"Console"
/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorApplicationTests.java
0 → 100644
View file @
1dbc94d0
/*
* Copyright 2012-2014 the original author or authors.
*
* 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
sample
.
actuator
.
log4j2
;
import
java.util.Map
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.test.IntegrationTest
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.boot.test.TestRestTemplate
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
/**
* Basic integration tests for service demo application.
*
* @author Dave Syer
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
SampleActuatorApplication
.
class
)
@WebAppConfiguration
@IntegrationTest
(
"server.port=0"
)
@DirtiesContext
public
class
SampleActuatorApplicationTests
{
@Value
(
"${local.server.port}"
)
private
int
port
;
@Test
public
void
testHome
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
port
,
Map
.
class
);
assertEquals
(
HttpStatus
.
OK
,
entity
.
getStatusCode
());
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
assertEquals
(
"Hello Daniel"
,
body
.
get
(
"message"
));
}
}
spring-boot-starters/pom.xml
View file @
1dbc94d0
...
...
@@ -41,6 +41,7 @@
<module>
spring-boot-starter-jta-bitronix
</module>
<module>
spring-boot-starter-logging
</module>
<module>
spring-boot-starter-log4j
</module>
<module>
spring-boot-starter-log4j2
</module>
<module>
spring-boot-starter-mobile
</module>
<module>
spring-boot-starter-actuator
</module>
<module>
spring-boot-starter-parent
</module>
...
...
spring-boot-starters/spring-boot-starter-log4j2/pom.xml
0 → 100644
View file @
1dbc94d0
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starters
</artifactId>
<version>
1.2.0.BUILD-SNAPSHOT
</version>
</parent>
<artifactId>
spring-boot-starter-log4j2
</artifactId>
<name>
Spring Boot Log4J2 Starter
</name>
<description>
Spring Boot Log4J2 Starter
</description>
<url>
http://projects.spring.io/spring-boot/
</url>
<organization>
<name>
Pivotal Software, Inc.
</name>
<url>
http://www.spring.io
</url>
</organization>
<properties>
<main.basedir>
${basedir}/../..
</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
jcl-over-slf4j
</artifactId>
</dependency>
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
jul-to-slf4j
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-slf4j-impl
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-api
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-core
</artifactId>
</dependency>
</dependencies>
</project>
spring-boot-starters/spring-boot-starter-log4j2/src/main/resources/META-INF/spring.provides
0 → 100644
View file @
1dbc94d0
provides: log4j2,log4j-slf4j-impl
spring-boot/pom.xml
View file @
1dbc94d0
...
...
@@ -74,6 +74,16 @@
<artifactId>
log4j
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-api
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-core
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
...
...
spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java
View file @
1dbc94d0
...
...
@@ -37,6 +37,8 @@ public abstract class LoggingSystem {
systems
.
put
(
"ch.qos.logback.core.Appender"
,
pkg
+
".logback.LogbackLoggingSystem"
);
systems
.
put
(
"org.apache.log4j.PropertyConfigurator"
,
pkg
+
".log4j.Log4JLoggingSystem"
);
systems
.
put
(
"org.apache.logging.log4j.LogManager"
,
pkg
+
".log4j2.Log4J2LoggingSystem"
);
systems
.
put
(
"java.util.logging.LogManager"
,
pkg
+
".java.JavaLoggingSystem"
);
SYSTEMS
=
Collections
.
unmodifiableMap
(
systems
);
}
...
...
spring-boot/src/main/java/org/springframework/boot/logging/log4j/Log4JLoggingSystem.java
View file @
1dbc94d0
...
...
@@ -33,7 +33,7 @@ import org.springframework.util.Log4jConfigurer;
import
org.springframework.util.StringUtils
;
/**
* {@link LoggingSystem} for
for <a href="http://logging.apache.org/log4j">l
og4j</a>.
* {@link LoggingSystem} for
<a href="http://logging.apache.org/log4j/1.2">L
og4j</a>.
*
* @author Phillip Webb
* @author Dave Syer
...
...
spring-boot/src/main/java/org/springframework/boot/logging/log4j/package-info.java
View file @
1dbc94d0
...
...
@@ -15,7 +15,7 @@
*/
/**
* Support for the Log4
J
logging library.
* Support for the Log4
j
logging library.
*/
package
org
.
springframework
.
boot
.
logging
.
log4j
;
spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java
0 → 100644
View file @
1dbc94d0
package
org
.
springframework
.
boot
.
logging
.
log4j2
;
import
java.net.URL
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.apache.logging.log4j.Level
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.core.LoggerContext
;
import
org.apache.logging.log4j.core.config.Configuration
;
import
org.apache.logging.log4j.core.config.ConfigurationFactory
;
import
org.apache.logging.log4j.core.config.ConfigurationSource
;
import
org.springframework.boot.logging.AbstractLoggingSystem
;
import
org.springframework.boot.logging.LogLevel
;
import
org.springframework.boot.logging.LoggingSystem
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ResourceUtils
;
import
org.springframework.util.SystemPropertyUtils
;
/**
* {@link LoggingSystem} for <a href="http://logging.apache.org/log4j/2.x/">Log4j 2</a>.
*
* @author Daniel Fullarton
* @since 1.2.0
*/
public
class
Log4J2LoggingSystem
extends
AbstractLoggingSystem
{
private
static
final
Map
<
LogLevel
,
Level
>
LEVELS
;
static
{
Map
<
LogLevel
,
Level
>
levels
=
new
HashMap
<
LogLevel
,
Level
>();
levels
.
put
(
LogLevel
.
TRACE
,
Level
.
TRACE
);
levels
.
put
(
LogLevel
.
DEBUG
,
Level
.
DEBUG
);
levels
.
put
(
LogLevel
.
INFO
,
Level
.
INFO
);
levels
.
put
(
LogLevel
.
WARN
,
Level
.
WARN
);
levels
.
put
(
LogLevel
.
ERROR
,
Level
.
ERROR
);
levels
.
put
(
LogLevel
.
FATAL
,
Level
.
ERROR
);
levels
.
put
(
LogLevel
.
OFF
,
Level
.
OFF
);
LEVELS
=
Collections
.
unmodifiableMap
(
levels
);
}
public
Log4J2LoggingSystem
(
ClassLoader
classLoader
)
{
super
(
classLoader
,
"log4j2.json"
,
"log4j2.jsn"
,
"log4j2.xml"
);
}
@Override
public
void
initialize
(
String
configLocation
)
{
Assert
.
notNull
(
configLocation
,
"ConfigLocation must not be null"
);
String
resolvedLocation
=
SystemPropertyUtils
.
resolvePlaceholders
(
configLocation
);
try
{
LoggerContext
ctx
=
(
LoggerContext
)
LogManager
.
getContext
(
false
);
URL
url
=
ResourceUtils
.
getURL
(
resolvedLocation
);
ConfigurationSource
configSource
=
new
ConfigurationSource
(
url
.
openStream
(),
url
);
Configuration
config
=
ConfigurationFactory
.
getInstance
().
getConfiguration
(
configSource
);
ctx
.
start
(
config
);
}
catch
(
Exception
ex
)
{
throw
new
IllegalStateException
(
"Could not initialize logging from "
+
configLocation
,
ex
);
}
}
@Override
public
void
setLogLevel
(
String
loggerName
,
LogLevel
level
)
{
LoggerContext
ctx
=
(
LoggerContext
)
LogManager
.
getContext
(
false
);
ctx
.
getConfiguration
().
getLoggerConfig
(
loggerName
).
setLevel
(
LEVELS
.
get
(
level
));
ctx
.
updateLoggers
();
}
}
spring-boot/src/main/java/org/springframework/boot/logging/log4j2/package-info.java
0 → 100644
View file @
1dbc94d0
/*
* Copyright 2012-2014 the original author or authors.
*
* 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.
*/
/**
* Support for the Log4j 2 logging library.
*/
package
org
.
springframework
.
boot
.
logging
.
log4j2
;
spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/basic-log4j2.xml
0 → 100644
View file @
1dbc94d0
<?xml version="1.0" encoding="UTF-8"?>
<Configuration
status=
"WARN"
>
<Properties>
<Property
name=
"PID"
>
????
</Property>
<Property
name=
"LOG_PATTERN"
>
[%d{yyyy-MM-dd HH:mm:ss.SSS}] boot%X{context} - ${sys:PID} %5p [%t] --- %c{1}: %m%n
</Property>
</Properties>
<Appenders>
<Console
name=
"Console"
target=
"SYSTEM_OUT"
follow=
"true"
>
<PatternLayout
pattern=
"${LOG_PATTERN}"
/>
</Console>
</Appenders>
<Loggers>
<Logger
name=
"org.hibernate.validator.internal.util.Version"
level=
"warn"
/>
<Logger
name=
"org.apache.coyote.http11.Http11NioProtocol"
level=
"warn"
/>
<Logger
name=
"org.crsh.plugin"
level=
"WARN"
/>
<Logger
name=
"org.apache.tomcat.util.net.NioSelectorPool"
level=
"warn"
/>
<Logger
name=
"org.apache.catalina.startup.DigesterFactory"
level=
"error"
/>
<Logger
name=
"org.apache.catalina.util.LifecycleBase"
level=
"error"
/>
<Logger
name=
"org.eclipse.jetty.util.component.AbstractLifeCycle"
level=
"error"
/>
<Root
level=
"info"
>
<AppenderRef
ref=
"Console"
/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml
0 → 100644
View file @
1dbc94d0
<?xml version="1.0" encoding="UTF-8"?>
<Configuration
status=
"WARN"
>
<Properties>
<Property
name=
"PID"
>
????
</Property>
<Property
name=
"LOG_PATH"
>
/tmp
</Property>
<Property
name=
"LOG_FILE"
>
${sys:LOG_PATH}/spring.log
</Property>
<Property
name=
"LOG_PATTERN"
>
[%d{yyyy-MM-dd HH:mm:ss.SSS}] boot%X{context} - ${sys:PID} %5p [%t] --- %c{1}: %m%n
</Property>
</Properties>
<Appenders>
<Console
name=
"Console"
target=
"SYSTEM_OUT"
follow=
"true"
>
<PatternLayout
pattern=
"${LOG_PATTERN}"
/>
</Console>
<RollingFile
name=
"File"
fileName=
"${sys:LOG_FILE}"
filePattern=
"logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz"
>
<PatternLayout>
<Pattern>
${LOG_PATTERN}
</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy
size=
"10 MB"
/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger
name=
"org.hibernate.validator.internal.util.Version"
level=
"warn"
/>
<Logger
name=
"org.apache.coyote.http11.Http11NioProtocol"
level=
"warn"
/>
<Logger
name=
"org.crsh.plugin"
level=
"warn"
/>
<Logger
name=
"org.apache.tomcat.util.net.NioSelectorPool"
level=
"warn"
/>
<Logger
name=
"org.apache.catalina.startup.DigesterFactory"
level=
"error"
/>
<Logger
name=
"org.apache.catalina.util.LifecycleBase"
level=
"error"
/>
<Logger
name=
"org.eclipse.jetty.util.component.AbstractLifeCycle"
level=
"error"
/>
<Root
level=
"info"
>
<AppenderRef
ref=
"Console"
/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java
0 → 100644
View file @
1dbc94d0
/*
* Copyright 2012-2014 the original author or authors.
*
* 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
.
springframework
.
boot
.
logging
.
log4j2
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.springframework.boot.logging.LogLevel
;
import
org.springframework.boot.test.OutputCapture
;
import
org.springframework.util.StringUtils
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
* Tests for {@link Log4J2LoggingSystem}.
*
* @author Daniel Fullarton
*/
public
class
Log4J2LoggingSystemTests
{
@Rule
public
OutputCapture
output
=
new
OutputCapture
();
private
final
Log4J2LoggingSystem
loggingSystem
=
new
Log4J2LoggingSystem
(
getClass
()
.
getClassLoader
());
private
Logger
logger
;
@Before
public
void
setup
()
{
this
.
logger
=
LogManager
.
getLogger
(
getClass
());
}
@After
public
void
clear
()
{
System
.
clearProperty
(
"LOG_FILE"
);
System
.
clearProperty
(
"LOG_PATH"
);
System
.
clearProperty
(
"PID"
);
}
@Test
public
void
testNonDefaultConfigLocation
()
throws
Exception
{
this
.
loggingSystem
.
beforeInitialize
();
this
.
loggingSystem
.
initialize
(
"classpath:log4j2-nondefault.xml"
);
this
.
logger
.
info
(
"Hello world"
);
String
output
=
this
.
output
.
toString
().
trim
();
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"Hello world"
));
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"/tmp/spring.log"
));
}
@Test
(
expected
=
IllegalStateException
.
class
)
public
void
testNonexistentConfigLocation
()
throws
Exception
{
this
.
loggingSystem
.
beforeInitialize
();
this
.
loggingSystem
.
initialize
(
"classpath:log4j2-nonexistent.xml"
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testNullConfigLocation
()
throws
Exception
{
this
.
loggingSystem
.
beforeInitialize
();
this
.
loggingSystem
.
initialize
(
null
);
}
@Test
public
void
setLevel
()
throws
Exception
{
this
.
loggingSystem
.
beforeInitialize
();
this
.
loggingSystem
.
initialize
();
this
.
logger
.
debug
(
"Hello"
);
this
.
loggingSystem
.
setLogLevel
(
"org.springframework.boot"
,
LogLevel
.
DEBUG
);
this
.
logger
.
debug
(
"Hello"
);
assertThat
(
StringUtils
.
countOccurrencesOf
(
this
.
output
.
toString
(),
"Hello"
),
equalTo
(
1
));
}
}
spring-boot/src/test/resources/log4j2-nondefault.xml
0 → 100644
View file @
1dbc94d0
<?xml version="1.0" encoding="UTF-8"?>
<Configuration
status=
"WARN"
>
<Properties>
<Property
name=
"PID"
>
????
</Property>
<Property
name=
"LOG_PATH"
>
/tmp
</Property>
<Property
name=
"LOG_FILE"
>
${sys:LOG_PATH}/spring.log
</Property>
<Property
name=
"LOG_PATTERN"
>
${sys:LOG_FILE} %d{yyyy-MM-dd HH:mm:ss.SSS}] service%X{context} - ${sys:PID} %5p [%t] --- %c{1}: %m%n
</Property>
</Properties>
<Appenders>
<Console
name=
"Console"
target=
"SYSTEM_OUT"
follow=
"true"
>
<PatternLayout
pattern=
"${LOG_PATTERN}"
/>
</Console>
</Appenders>
<Loggers>
<Root
level=
"info"
>
<AppenderRef
ref=
"Console"
/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
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