Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
Configuration Properties File for Apache Log4j 1.x
How to write a configuration properties file for Log4j 1.x? I just want to use the basic functionalities to specify the log file name and the log level.
✍: FYIcenter.com
Below is a simple configuration file, log4j.properties, that support the Log4j API:
# Copyright (c) FYIcenter.com
log4j.rootLogger=DEBUG, MyConsole, MyFile
log4j.appender.MyConsole=org.apache.log4j.ConsoleAppender
log4j.appender.MyConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.MyConsole.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
log4j.appender.MyFile=org.apache.log4j.FileAppender
log4j.appender.MyFile.File=log4j.log
log4j.appender.MyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.MyFile.layout.ConversionPattern=%-5r %-5p [%t] %c{2} - %m%n
This sample configuration file log4j2.properties does the following:
Below is a simple Java program Log4jPropertiesConfig.java that uses the above Log4j configuration:
// Copyright (c) FYIcenter.com
import org.apache.log4j.*;
public class Log4jPropertiesConfig {
private static final Logger logger
= Logger.getLogger("Log4jPropertiesConfig");
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
logger.fatal("Hello - fatal");
logger.error("Hello - error");
logger.warn("Hello - warn");
logger.info("Hello - info");
logger.debug("Hello - debug");
logger.trace("Hello - trace");
}
}
Compile and run it with log4j-1.2.17.jar:
fyicenter> java -version java version "1.7.0_45" fyicenter> javac -cp log4j-1.2.17.jar Log4jPropertiesConfig.java fyicenter> java -cp .;log4j-1.2.17.jar Log4jPropertiesConfig 0 FATAL [main] Log4jPropertiesConfig - Hello - fatal 2 ERROR [main] Log4jPropertiesConfig - Hello - error 2 WARN [main] Log4jPropertiesConfig - Hello - warn 2 INFO [main] Log4jPropertiesConfig - Hello - info 2 DEBUG [main] Log4jPropertiesConfig - Hello - debug fyicenter> type log4j.log 0 FATAL [main] Log4jPropertiesConfig - Hello - fatal 2 ERROR [main] Log4jPropertiesConfig - Hello - error 2 WARN [main] Log4jPropertiesConfig - Hello - warn 2 INFO [main] Log4jPropertiesConfig - Hello - info 2 DEBUG [main] Log4jPropertiesConfig - Hello - debug
Everything works as expected.
⇒ Run Trivial.java Example in log4j-1.2.17.zip
⇐ Compile and Run Program with Apache Log4j 1.x
2016-06-28, ∼4484🔥, 0💬
Popular Posts:
Java Cryptography Extension 1.6 JAR File Size and Download Location: File name: jce.jar, jce-1.6.jar...
How to download and install ojdbc7.jar for Oracle 12c R1? ojdbc8.jar for Oracle 12c R1 is a Java 7 a...
JDK 17 java.sql.rowset.jmod is the JMOD file for JDK 17 SQL Rowset module. JDK 17 SQL Rowset module ...
JDK 17 java.rmi.jmod is the JMOD file for JDK 17 RMI (Remote Method Invocation) module. JDK 17 RMI m...
JBrowser Source Code Files are provided in the source package file. You can download JBrowser source...