[JAVA] Enable log output to both file and console using log4j in Eclipse.

This article describes how to use the org.apache.log4j package in Eclipse to allow log output to both the file and the console.

Before reading this article, if you do not have an environment that can output logs using the log4j package, you need to create an environment by referring to the link below. Reference article: https://qiita.com/toolate32/items/538f3a99d23a7a1f29bd

  1. First, let's check the log output with the following simple project.

package test.project; import org.apache.log4j.Logger;

public class TestClass { public static void main(String[] args) { Logger logger = Logger.getLogger(TestClass.class.getName()); // Log output logger.info("This is info."); } }

For the time being, it is output to the console. Console output: 2017/XX/XX 14:56:09 781 INFO main TestClass - This is info.

The contents of the current log4j.xml are as follows. 1:< ?xml version="1.0" encoding="UTF-8" ?> 2:< !DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 3:< log4j:configuration xmlns:log4j="" class="autolink">http://jakarta.apache.org/log4j/" > 4:< appender name="stdout" class="org.apache.log4j.ConsoleAppender"> 5:< param name="Target" value="System.out" /> 6:< layout class="org.apache.log4j.PatternLayout"> 7:< param name="ConversionPattern" value="%d{yyyy/MM/dd HH: mm:ss SSS} %5p %5t %c{1} - %m%n" /> 8:< /layout> 9:< /appender> 10:< category name="test.project" > 11:< appender-ref ref="stdout" /> 12:< /category> 13:< /log4j:configuration>

  1. Add the log4j.xml settings so that they are output to both the console and the log file. In the above configuration file, lines 4-9 specify the console output definition, and lines 10-12 specify the log output settings in the package test.project. Since apender-ref is set to "stdout" on the 11th line, the log output of the definition on the 4th to 9th lines is performed. To define the file output and output the file to this, add the following definition.

1:< ?xml version="1.0" encoding="UTF-8" ?> 2:< !DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 3:< log4j:configuration xmlns:log4j="" class="autolink">http://jakarta.apache.org/log4j/" > 4:< appender name="stdout" class="org.apache.log4j.ConsoleAppender"> 5:< param name="Target" value="System.out" /> 6:< layout class="org.apache.log4j.PatternLayout"> 7:< param name="ConversionPattern" value="%d{yyyy/MM/dd HH: mm:ss SSS} %5p %5t %c{1} - %m%n" /> 8:< /layout> 9:< /appender> 4-1:< appender name="fileout" class="org.apache.log4j.FileAppender"> 5-1:< param name="File" value="logTest.log" /> 6-1:< layout class="org.apache.log4j.PatternLayout"> 7-1:< param name="ConversionPattern" value="%d{yyyy/MM/dd HH: mm:ss SSS} %5p %5t %c{1} - %m%n" /> 8-1:< /layout> 9-1:< /appender> 10:< category name="test.project" > 11:< appender-ref ref="stdout" /> 11-1:< appender-ref ref="fileout" /> 12:< /category> 13:< /log4j:configuration>

The added parts are 4-1 to 9-1, 11-1. Since the file output definition is set in 4-1 to 9-1 and the apender-ref is set to "fileout" in the 11-1st line, the log output of the 4-1 to 9-1 line definition is performed.

Now, when I try to run the above source, it is output to both the console and the log file this time.

Recommended Posts

Enable log output to both file and console using log4j in Eclipse.
Prepare for log output using log4j in Eclipse.
Log output to file in Java
How to color code console output in Eclipse
Gzip-compress byte array in Java and output to file
Output request and response log in Spring Boot
Build ELK stack on Mac OS X and output JAVA log to Elasticsearch (log4j2)
Output settings to debug console in Visual Studio Code
Output log to external file with slf4j + logback with Maven
How to convert A to a and a to A using AND and OR in Java
How to debug the generated jar file in Eclipse
To debug in eclipse
Log output in Json format using lograge / lograge-sql with RubyOnRails
When there is no output to stdout in docker log
How to set character code and line feed code in Eclipse
Output relative path in .classpath file generated by gradle eclipse
Output javadoc to word file
Output XML tree to file
Using Amateurs UML in Eclipse
Output JaCoCo coverage to console
Ubuntu unable to log in
I can't log in to MySQL from Django when using docker-compose
I want to hook log file generation / open with log4j # FileAppender
How to POST JSON in Java-Method using OkHttp3 and method using HttpUrlConnection-
Unable to get resources when using modules in Gradle and IntelliJ
Put the date (e.g. yyyy-MM-dd) in the log file output by logback
Sample to read and write LibreOffice Calc fods file in JRuby 2021
[Android] Convert Map to JSON using GSON in Kotlin and Java
Sample to read and write LibreOffice Calc fods file in Java 2021
I tried to display the calendar on the Eclipse console using Java.
Steps to install Maven on Mac and use it in Eclipse
[Ruby on Rails] How to log in with only your name and password using the gem devise
Conveniences I've been using since migrating from Eclipse to IntelliJ IDEA (in terms of search and Git)