The log4j console does not print the fault solution of the log

foreword

I took over a code from another project team. In the process of IDAE debugging the program, I found that the log4j log was not printed on the console, and there was no problem with the log-related code.

I searched around the Internet and summarized my personal process to solve this problem.

process

1. Determine what configuration file is used

I don't know what the purpose is, but there are two configuration files of log4j properties and xml in the project.

At the beginning, I didn't see the xml file, but I kept paying attention to the properties file, and found that there was no problem. Later, **-Dlog4j.debug** was added to Vm options, and it was found in the log4j information printed at startup that log4j uses xml files.

2. additivity

The second is the additivity attribute of <logger> .

<!-- 打印到控制台 -->
<appender name="myConsole" class="org.apache.log4j.ConsoleAppender">
   <param name="Target" value="System.out" />
   <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern"  value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />
   </layout>
</appender>

<!-- 打印到文件中 -->
<appender name="fileAppender" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="${logfileName}.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
               value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" />
    </layout>
</appender>

<!-- logger -->
<logger name="com.test" additivity="false">
   <level value="INFO" />
   <appender-ref ref="fileAppender" />
</logger>

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

In the xml configuration file, <appender> defines the format in which the log is output to the console or a file. <logger> determines which appender is bound to each class in the project. <root> is the root logger, which will bind multiple appenders.

The additivity of the logger defaults to true, which means that the log to be printed by the logger should also be sent to the root, so that the appender of the root can print.

3. Solutions

Option One

Modify additivity to true, or remove it directly, because the default value is true

Option II

Add the console appender in the logger of com.test.

<!-- logger -->
<logger name="com.test" additivity="false">
   <level value="INFO" />
   <appender-ref ref="fileAppender" />
   <appender-ref ref="myConsole" />
</logger>

In this way, the problem of not printing logs on the console is solved.


Post-95 young programmers, write about personal practice in daily work, from the perspective of beginners, write from 0 to 1, detailed and serious. The article will be published on the official account [ Getting Started to Give Up Road ], looking forward to your attention.

Thank you for every encounter

Guess you like

Origin blog.csdn.net/CatchLight/article/details/129236667