Home > Java > javaTutorial > Analysis of key elements and usage of log4j configuration file

Analysis of key elements and usage of log4j configuration file

WBOY
Release: 2024-02-19 20:56:09
Original
602 people have browsed it

Analysis of key elements and usage of log4j configuration file

Understand the key elements and usage of log4j configuration files

Log4j is a Java logging tool that is widely used in Java projects. By properly configuring the log4j configuration file, log output can be easily managed and controlled. This article will introduce the key elements and usage of log4j configuration files, and give specific code examples.

1. The basic structure of the log4j configuration file

The log4j configuration file is a file in XML or Properties format, which is used to define the type, output method, log level, etc. of logs to be recorded in the code. information. The following is the basic structure of a typical log4j configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <appender name="file" class="org.apache.log4j.FileAppender">
        <param name="File" value="logs/application.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <logger name="com.example">
        <level value="DEBUG" />
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </logger>

    <root>
        <priority value="INFO" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>
Copy after login

The configuration file mainly contains 4 elements: appender, layout, logger and root. Next, we will introduce the role and configuration of these elements in detail.

  1. appender

appender is used to define the destination of log output. Log4j provides a variety of appender implementations, such as ConsoleAppender (console output), FileAppender (file output), RollingFileAppender (rolling file output), etc. Each appender has a unique name, defined through the name attribute. The following is a sample configuration of an appender:

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n" />
    </layout>
</appender>
Copy after login
  1. layout

layout is used to define the output format of the log. log4j provides a variety of layout implementations, such as PatternLayout, HTMLLayout, SimpleLayout, etc. Each layout has a unique name, defined through the class attribute. The following is an example configuration of a layout:

<layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n" />
</layout>
Copy after login
Copy after login
  1. logger

logger is used to define the log levels of different packages or classes, and which appenders the logs are output to. Each logger has a name, defined through the name attribute. The following is an example configuration of a logger:

<logger name="com.example">
    <level value="DEBUG" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />
</logger>
Copy after login
Copy after login
  1. root

root is the parent logger of all loggers and is used to define the default log level and output appender. The following is an example configuration of root:

<root>
    <priority value="INFO" />
    <appender-ref ref="console" />
</root>
Copy after login
Copy after login

2. How to use the log4j configuration file

  1. Configuring the appender

By configuring the appender, you can specify the log The output destination. In the sample configuration file, ConsoleAppender and FileAppender are configured respectively and output to the console and file respectively.

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n" />
    </layout>
</appender>


    
    
        
    
Copy after login
  1. Configuring layout

By configuring layout, you can specify the format of log output. In the sample configuration file, PatternLayout is used and the specific log format is defined.

<layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n" />
</layout>
Copy after login
Copy after login
  1. Configuring logger

By configuring logger, you can specify the log level and output appender of different packages or classes. In the example configuration file, set the log level under the com.example package to DEBUG, and output it to the console and file appenders.

<logger name="com.example">
    <level value="DEBUG" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />
</logger>
Copy after login
Copy after login
  1. Configuring root

By configuring root, you can specify the default log level and output appender. In the example configuration file, set the default log level to INFO and output to the consoleappender.

<root>
    <priority value="INFO" />
    <appender-ref ref="console" />
</root>
Copy after login
Copy after login

The above is a detailed introduction to the key elements and usage of the log4j configuration file. By properly configuring the log4j configuration file, logs can be easily managed and controlled. I hope this article can help you understand the log4j configuration file.

The above is the detailed content of Analysis of key elements and usage of log4j configuration file. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template