Use of log4j (log for Java (Java logs ))

Source: Internet
Author: User

Log4j is an open-source project of Apache. By using log4j, we can control the log information shipping destination: console, file, GUI component, and even an interface server, NT event recorder, Unix
Syslog daemon. We can also control the output format of each log. By defining the level of each log information, we can control the log generation process in greater detail. The most interesting thing is that these can be configured flexibly through a configuration file, without the need to modify the application code.

Official station: http://logging.apache.org/log4j/

Steps for using log4j:

1. Add the JAR file of log4j

2. Create the log4j. properties file under classpath and write the corresponding content.

3. Configure and modify attributes

4. Add related statements to the class for log output

 

Simple use example: use the most basic configuration here

First, put log4j. jar in the classpath environment of the project.

import org.apache.log4j.BasicConfigurator;import org.apache.log4j.Logger;public class LogTest {static final Logger logger = Logger.getLogger(LogTest.class);public static void main(String[] args) {BasicConfigurator.configure();logger.debug("debug message");logger.info("info message");logger.warn("warn message");logger.error("error message");logger.fatal("fatal message");}}

After running, you can see that some Japanese messages are printed. This is the role of log4j.

You can also use the configuration file log4j. properties.

Use propertyconfigurator. Configure ("log4j. propertites ");

You can also write logs to log files.

Example of the log4j. properties file:

Log4j. rootlogger = debug, CA, Fa

# Leleappender

Log4j. appender. CA = org. Apache. log4j. leleappender

Log4j. appender. Ca. layout = org. Apache. log4j. patternlayout

Log4j. appender. Ca. layout. conversionpattern = %-4r [% T] %-5 p % C % x-% m % N

 

# File appender

Log4j. appender. Fa = org. Apache. log4j. fileappender

Log4j. appender. Fa. File = test. Log

Log4j. appender. Fa. layout = org. Apache. log4j. patternlayout

Log4j. appender. Fa. layout. conversionpattern = %-4r [% T] %-5 p % C % x-% m % N

 

# Set the loggerlevel of file appender to warn

Log4j. appender. Fa. Threshold = warn

 

You can also use the XML file configuration as follows:

<?xml version="1.0" encoding="UTF-8" ?>02.<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">03.<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>04.<appender name="CA" class="org.apache.log4j.ConsoleAppender">05.<layout class="org.apache.log4j.PatternLayout">06.<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />07.</layout>08.</appender>09.<appender name="FA" class="org.apache.log4j.FileAppender">10.<param name="File" value="sample.log"/>11.<param name="Threshold" value="WARN"/>12.<layout class="org.apache.log4j.PatternLayout">13.<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />14.</layout>15.</appender>16.<root>17.<level value="DEBUG" />18.<appender-ref ref="CA" />19.<appender-ref ref="FA" />20.</root>21.</log4j:configuration>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.