log4j
Log4j is a fast and flexible framework for logging application debugging messages.
Example log4j 1.2.17
Project structure:
. |-- log4j.properties |-- log4jexample.log |-- pom.xml |-- src | `-- main | `-- java | `-- org | `-- allowed | `-- bitarus | `-- log4jexample | `-- Main.java `-- target |-- classes | `-- org | `-- allowed | `-- bitarus | `-- log4jexample | `-- Main.class |-- log4jexample-1.0.0.0-SNAPSHOT.jar |-- maven-archiver | `-- pom.properties `-- surefire
pom.xml
1 <project
2 xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6 <groupId>org.allowed.bitarus</groupId>
7 <artifactId>log4jexample</artifactId>
8 <version>1.0.0.0-SNAPSHOT</version>
9 <packaging>jar</packaging>
10 <name>log4jexample</name>
11 <dependencies>
12 <dependency>
13 <groupId>log4j</groupId>
14 <artifactId>log4j</artifactId>
15 <version>1.2.17</version>
16 </dependency>
17 </dependencies>
18 </project>
Main.java
1 package org.allowed.bitarus.log4jexample;
2 import java.util.concurrent.ExecutorService;
3 import java.util.concurrent.Executors;
4 import org.apache.log4j.Logger;
5 /*
6 java -cp target/log4jexample-1.0.0.0-SNAPSHOT.jar:/home/vitor/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar -Dlog4j.configuration="file:///home/vitor/Documents/log4jexample/log4j.properties" org.allowed.bitarus.log4jexample.Main
7 */
8 public class Main{
9 public static void main(String[] args){
10 ExecutorService executorService = Executors.newFixedThreadPool(10);
11 //org.allowed.bitarus.log4jexample.Main loggerName
12 Logger logger = Logger.getLogger(Main.class);
13 logger.info("test log4j info");
14 logger.debug("test log4j debug");
15 }
16 }
log4j.properties
log4j.logger.org.allowed.bitarus.log4jexample.Main=ALL,log4jexampleAppender,log4jexampleAppenderConsole log4j.appender.log4jexampleAppender=org.apache.log4j.DailyRollingFileAppender log4j.appender.log4jexampleAppender.File=log4jexample.log log4j.appender.log4jexampleAppender.layout=org.apache.log4j.PatternLayout log4j.appender.log4jexampleAppender.layout.ConversionPattern=%d{ISO8601}|%p|%c|%t|%m%n log4j.appender.log4jexampleAppender.Threshold=DEBUG log4j.appender.log4jexampleAppender.DatePattern='.'yyyyMMdd log4j.appender.log4jexampleAppenderConsole=org.apache.log4j.ConsoleAppender log4j.appender.log4jexampleAppenderConsole.layout=org.apache.log4j.PatternLayout log4j.appender.log4jexampleAppenderConsole.layout.ConversionPattern=%d{ISO8601}|%p|%c|%t|%m%n log4j.appender.log4jexampleAppenderConsole.Threshold=INFO
# Set root logger level to ALL and its only appender to appenderx. log4j.rootLogger=ALL, appenderx #log4j.appender.appenderx=org.apache.log4j.ConsoleAppender log4j.appender.appenderx=org.apache.log4j.RollingFileAppender log4j.appender.appenderx.File=/tmp/appXYZ.log # appenderx uses PatternLayout. log4j.appender.appenderx.layout=org.apache.log4j.PatternLayout log4j.appender.appenderx.layout.ConversionPattern=%d [%t] %-5p %c - %m%n