BAEL-65 - logging to files

This commit is contained in:
Slavisa Baeldung 2016-07-01 15:22:07 +02:00
parent d67f19d9e5
commit acaa70c66d
7 changed files with 60 additions and 27 deletions

View File

@ -21,24 +21,24 @@
<!--log4j2 dependencies--> <!--log4j2 dependencies-->
<!--<dependency>--> <dependency>
<!--<groupId>org.apache.logging.log4j</groupId>--> <groupId>org.apache.logging.log4j</groupId>
<!--<artifactId>log4j-api</artifactId>--> <artifactId>log4j-api</artifactId>
<!--<version>2.6</version>--> <version>2.6</version>
<!--</dependency>--> </dependency>
<!--<dependency>--> <dependency>
<!--<groupId>org.apache.logging.log4j</groupId>--> <groupId>org.apache.logging.log4j</groupId>
<!--<artifactId>log4j-core</artifactId>--> <artifactId>log4j-core</artifactId>
<!--<version>2.6</version>--> <version>2.6</version>
<!--</dependency>--> </dependency>
<!--logback dependencies--> <!--logback dependencies-->
<!--<dependency>--> <dependency>
<!--<groupId>ch.qos.logback</groupId>--> <groupId>ch.qos.logback</groupId>
<!--<artifactId>logback-classic</artifactId>--> <artifactId>logback-classic</artifactId>
<!--<version>1.1.7</version>--> <version>1.1.7</version>
<!--</dependency>--> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

View File

@ -0,0 +1,16 @@
package com.baeldung.log4j2;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public class Log4j2Example {
private static final Logger logger = LogManager.getLogger(Log4j2Example.class);
public static void main(String[] args) {
logger.debug("Debug log message");
logger.info("Info log message");
logger.error("Error log message");
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.logback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogbackExample {
private static final Logger logger = LoggerFactory.getLogger(LogbackExample.class);
public static void main(String[] args) {
logger.debug("Debug log message");
logger.info("Info log message");
logger.error("Error log message");
}
}

View File

@ -4,28 +4,28 @@
<!--Console appender--> <!--Console appender-->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender"> <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout"> <layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %m%n" /> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %m%n"/>
</layout> </layout>
</appender> </appender>
<!-- File appender--> <!-- File appender-->
<appender name="fout" class="org.apache.log4j.FileAppender"> <appender name="fout" class="org.apache.log4j.FileAppender">
<param name="file" value="baeldung.log" /> <param name="file" value="log4j/target/baeldung-log4j.log"/>
<param name="append" value="false" /> <param name="append" value="false"/>
<layout class="org.apache.log4j.PatternLayout"> <layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n" /> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
</layout> </layout>
</appender> </appender>
<!--Override log level for specified package--> <!--Override log level for specified package-->
<category name="com.baeldung.log4j"> <category name="com.baeldung.log4j">
<priority value="TRACE" /> <priority value="TRACE"/>
</category> </category>
<root> <root>
<level value="DEBUG" /> <level value="DEBUG"/>
<appender-ref ref="stdout" /> <appender-ref ref="stdout"/>
<appender-ref ref="fout" /> <appender-ref ref="fout"/>
</root> </root>
</log4j:configuration> </log4j:configuration>

View File

@ -8,7 +8,7 @@
</Console> </Console>
# File appender # File appender
<File name="fout" fileName="baeldung.log" immediateFlush="false" append="false"> <File name="fout" fileName="log4j/target/baeldung-log4j2.log" immediateFlush="false" append="false">
# Pattern of log message for file appender # Pattern of log message for file appender
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
</File> </File>
@ -16,7 +16,7 @@
<Loggers> <Loggers>
# Override log level for specified package # Override log level for specified package
<Logger name="com.baeldung.log4j" level="TRACE"/> <Logger name="com.baeldung.log4j2" level="ERROR"/>
<Root level="DEBUG"> <Root level="DEBUG">
<AppenderRef ref="stdout" /> <AppenderRef ref="stdout" />

View File

@ -10,7 +10,7 @@
# File appender # File appender
<appender name="fout" class="ch.qos.logback.core.FileAppender"> <appender name="fout" class="ch.qos.logback.core.FileAppender">
# Name of a log file # Name of a log file
<file>baeldung.log</file> <file>log4j/target/baeldung-logback.log</file>
<append>false</append> <append>false</append>
<encoder> <encoder>
# Pattern of log message for file appender # Pattern of log message for file appender
@ -19,7 +19,7 @@
</appender> </appender>
# Override log level for specified package # Override log level for specified package
<logger name="com.baeldung.log4j" level="TRACE"/> <logger name="com.baeldung.logback" level="ERROR"/>
<root level="DEBUG"> <root level="DEBUG">
<appender-ref ref="stdout" /> <appender-ref ref="stdout" />

View File

@ -32,6 +32,7 @@
<!-- <module>raml</module> --> <!-- <module>raml</module> -->
<module>rest-testing</module> <module>rest-testing</module>
<module>resteasy</module> <module>resteasy</module>
<module>log4j</module>
<module>spring-all</module> <module>spring-all</module>
<module>spring-apache-camel</module> <module>spring-apache-camel</module>