Log4j cannot find properties file in a simple application

不问归期 提交于 2019-12-11 03:36:41

问题


I have a very simple application that illustrates the function of logging. I use log4j. But I have some trouble with the settings. I downloaded the file log4j-1.2.16.jar. And connect it to my project by Properties -> Java Build Path -> Libraries -> Add External JARs.... Here are my class:

package ru.log4j.log4jhelloworld;

import org.apache.log4j.Logger;

public class MyClass {
    private static final Logger logger = Logger.getLogger(MyClass.class);
    public static void main(String[] args) {
        logger.info("Hello World!");        
    }
}  

My log4j.xml:

<?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="aa %p %c: %m%n"/>
    </layout>
  </appender>    
  <!--Root logger-->
  <root>
    <priority value ="debug" />
    <appender-ref ref="console" />
  </root>

</log4j:configuration>  

And my log4j.properties:

log4j.rootCategory=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern= %p %c: %m%n

In the console displays these exceptions:

log4j:WARN No appenders could be found for logger (ru.log4j.log4jhelloworld.MyClass).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

I do not know how I was supposed to create and connect other supporting files, such as log4j.xml and log4j.properties. And be sure whether they need? In what directory they should be located? What should I still register? Here is my project structure

.

I'm guessing that the error in this. What is my problem? Help me please!


回答1:


To solve your issue place your your log4j configuration file(i.e. log4j.xml or log4j.properties) in the src folder and it should work fine.

Moreover you don't need both files(log4j.xml and log4j.properties), just use one of them.




回答2:


The problem is that the logger system cannot find your config file. To fix it you have several options:

  1. If you need to keep logger config outside of your packaged classes (which you usually do in real production system) then

    • put your logger config file somewhere on file system outside of your classpath (e.g. /etc/my_great_app/log4j.xml )
    • Include path to your config in your Java application command line, like this -Dlog4j.configuration=<path log4j config> e.g. -Dlog4j.configuration=file:///etc/my_great_app/log4j.xml Note that path must include prefix file://.
  2. If you'd like to keep your log4j configuration along with your code (which is typical for unit tests), then all you need to do - just make sure that log4 config location is part of the classpath. For example if you're using maven, just store your log4j.properties in src/main/resources directory

Regarding difference between log4j.properties vs log4j.xml. They are just 2 different ways to define same configuration for log4j logger. You don't need both files, just decide what format you like more and use it :)




回答3:


Run the application with the -Dlog4j.debug VM argument and you will be prompted with the place where log4j want it to be.




回答4:


log4j.properties file put in src folder file like bellow

com.org.slk.LogExample.java
com.org.slk.log4j.properties

and then set PropertyConfigurator in LogExample.java

PropertyConfigurator.configure(LogExample.class.getResourceAsStream("log4j.properties"));

and property file set configuration like bellow log4j.properties

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File= fullpath /myLog.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n



回答5:


I had the similar problem. And solved it by putting the following lines in Run AS--> Run Configurations --> Arguments tab

-Dcvp.property.path=/home/local/Project/property/ --> location of your property file


Thanks!




回答6:


You can keep your log4j.properties file any location in your project. And then load your file

private static Logger logger = null;

static { System.setProperty("log4j.configurationFile", "C:\Users\jhaas\Workspace_3.0.30\RestClient\conf\log4j.properties"); }

It is similar to set the VM argument in eclipse like

-Dlog4j.configurationFile= C:\Users\jhaas\Workspace_3.0.30\RestClient\src\main\resources\log4j.properties

Better approach is to have it in your code rather than setting into the eclipse VM argument.



来源:https://stackoverflow.com/questions/9305535/log4j-cannot-find-properties-file-in-a-simple-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!