This article describes the preparations for enabling log output using the org.apache.log4j package in Eclipse.
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
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.
< ?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
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