Improved log4j project

This commit is contained in:
oborkovskyi 2016-07-01 17:38:30 +03:00
parent d9240931cf
commit 7704b2eb01
6 changed files with 54 additions and 26 deletions

View File

@ -10,7 +10,6 @@
<dependencies> <dependencies>
<!--log4j dependencies--> <!--log4j dependencies-->
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
@ -19,26 +18,23 @@
<!--log4j2 dependencies--> <!--log4j2 dependencies-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<!--<dependency>--> <artifactId>log4j-api</artifactId>
<!--<groupId>org.apache.logging.log4j</groupId>--> <version>2.6</version>
<!--<artifactId>log4j-api</artifactId>--> </dependency>
<!--<version>2.6</version>--> <dependency>
<!--</dependency>--> <groupId>org.apache.logging.log4j</groupId>
<!--<dependency>--> <artifactId>log4j-core</artifactId>
<!--<groupId>org.apache.logging.log4j</groupId>--> <version>2.6</version>
<!--<artifactId>log4j-core</artifactId>--> </dependency>
<!--<version>2.6</version>-->
<!--</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

@ -13,7 +13,7 @@
<param name="file" value="baeldung.log" /> <param name="file" value="baeldung.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} %p %m%n" />
</layout> </layout>
</appender> </appender>

View File

@ -4,19 +4,19 @@
# Console appender # Console appender
<Console name="stdout" target="SYSTEM_OUT"> <Console name="stdout" target="SYSTEM_OUT">
# Pattern of log message for console appender # Pattern of log message for console appender
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n" /> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %p %m%n" />
</Console> </Console>
# File appender # File appender
<File name="fout" fileName="baeldung.log" immediateFlush="false" append="false"> <File name="fout" fileName="baeldung.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} %p %m%n"/>
</File> </File>
</Appenders> </Appenders>
<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="TRACE"/>
<Root level="DEBUG"> <Root level="DEBUG">
<AppenderRef ref="stdout" /> <AppenderRef ref="stdout" />

View File

@ -3,7 +3,7 @@
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout"> <layout class="ch.qos.logback.classic.PatternLayout">
# Pattern of log message for console appender # Pattern of log message for console appender
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern> <Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Pattern>
</layout> </layout>
</appender> </appender>
@ -14,12 +14,12 @@
<append>false</append> <append>false</append>
<encoder> <encoder>
# Pattern of log message for file appender # Pattern of log message for file appender
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</pattern> <pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</pattern>
</encoder> </encoder>
</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="TRACE"/>
<root level="DEBUG"> <root level="DEBUG">
<appender-ref ref="stdout" /> <appender-ref ref="stdout" />