Prepare for log output using log4j in Eclipse.

This article describes the preparations for enabling log output using the org.apache.log4j package in Eclipse.

  1. First, download the log4j package (here log4j-1.2.17.zip) from the following site. http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.17/log4j-1.2.17.zip

  2. Unzip the log4j-1.2.17.zip file and add the log4j-1.2.17.jar file to your project's external library. From the Eclipse screen

Let's test if the log is output in a 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."); } }

When I execute it, the log is not output. The following error is output. log4j:WARN No appenders could be found for logger (test.project.TestClass). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Upon examination, this error seems to be an error asking me to set the log output setting because I can't find it in test.project.TestClass. As a setting method, there is a method to set in the property file (log4j.properties) and the XML file (log4j.xml), so this time, we will use the XML file.

  1. Create a log4j.xml file. (Created in the same location as the source file) By default, the ~ </ appender> part is on the net, so it will be plagiarized as it is. The ~ </ category> part is the newly created definition. *** is the package name, not the class name (in this case, "test.project")

< ?xml version="1.0" encoding="UTF-8" ?> < !DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> < log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> < appender name="stdout" 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 SSS} %5p %5t %c{1} - %m%n" /> < /layout> < /appender>

< category name="test.project" > < appender-ref ref="stdout" /> < /category>

< /log4j:configuration >

I will try it again. That will come out again. log4j:WARN No appenders could be found for logger (test.project.TestClass). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Why is it still an error? After investigating various things, it seems that I can not see it because log4j.xml is in the place where the classpath does not pass.

public class TestClass { public static void main(String[] args) { Logger logger = Logger.getLogger(TestClass.class.getName()); // Add and confirm URL fileXmlUrl = Loader.getResource("log4j.xml"); DOMConfigurator.configure (fileXmlUrl); // <-fileXmlUrl is null // Add and confirm // Log output logger.info("This is info."); } }

  1. Set the classpath in the location of the log4j.xml file.

Now if you run the source you ran above, you'll get a value instead of null. Next, if you delete the added part and move it, it will work properly this time.

2017/XX/XX 14:56:09 781 INFO main TestClass - This is info.

Recommended Posts

Prepare for log output using log4j in Eclipse.
Enable log output to both file and console using log4j in Eclipse.
[For beginners] I tried using DBUnit in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
Using Amateurs UML in Eclipse
Log output in Json format using lograge / lograge-sql with RubyOnRails
Log output to file in Java
Output Spring Boot log in json format
Enable code completion in Eclipse for Mac
[For beginners] How to debug in Eclipse
Eclipse Pleiades All in One for Mac released
ChatWork4j for using the ChatWork API in Java
How to color code console output in Eclipse
Technology for reading Java source code in Eclipse
[Java] API creation using Jerjey (Jax-rs) in eclipse
Output request and response log in Spring Boot
[Note] Struts2 environment construction using Gradle in Eclipse
Allow development in Eclipse environment using iPLAss SDK
Replaced Tomcat 8.5 log output with Log4j2.8 or later
Compare PDF output in Java for snapshot testing
Tips for generating files for eclipse projects in Gradle
MVC in Eclipse.
Try using Log4j 2.0
Best practices for log infrastructure design in container operations
I tried using an extended for statement in Java
Sample code for log output by Java + SLF4J + Logback