简体   繁体   English

请正确初始化log4j系统警告

[英]Please initialize the log4j system properly warning

Here is the error message -这是错误消息 -

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

Here is the invocation in the SerialPortUtil class -这是SerialPortUtil类中的调用 -

private static final Logger log = Logger.getLogger(SerialPortUtil.class.getSimpleName());
.
.
.
log.info("Serial port " + port.getName() + " is available");

Here is the content of my log4j.properties file -这是我的 log4j.properties 文件的内容 -

log4j.rootLogger=DebugAppender

#Debug logging
log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=activityLog.log
log4j.appender.DebugAppender.MaxFileSize=200KB
log4j.appender.DebugAppender.MaxBackupIndex=5
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d{DATE} %t - %m%n

And the property file is on the classpath.并且属性文件在类路径上。

Everything looks right to me, so what's going on?一切对我来说都是正确的,那么发生了什么? Shouldn't the DEBUG threshold also capture INFO logging? DEBUG阈值不应该也捕获INFO日志记录吗?

Add the code添加代码

BasicConfigurator.configure();

in your static main class as below..在您的静态主类中,如下所示..

Note: add " \\hadoop-2.7.1\\share\\hadoop\\common\\lib\\commons-logging-1.1.3.jar & \\hadoop-2.7.1\\share\\hadoop\\common\\lib\\log4j-1.2.17.jar " as the external references注意:添加“\\hadoop-2.7.1\\share\\hadoop\\common\\lib\\commons-logging-1.1.3.jar & \\hadoop-2.7.1\\share\\hadoop\\common\\lib\\log4j-1.2.17.jar” jar " 作为外部引用

import org.apache.log4j.BasicConfigurator;

public class ViewCountDriver extends Configured implements Tool{

    public static void main(String[]args) throws Exception{

        BasicConfigurator.configure(); 

        int exitcode = ToolRunner.run(new ViewCountDriver(), args);
        System.exit(exitcode); 
    }
}

From the link in the error message:从错误消息中的链接:

This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration.当无法找到默认配置文件 log4j.properties 和 log4j.xml 并且应用程序未执行显式配置时,就会发生这种情况。 log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. log4j 使用 Thread.getContextClassLoader().getResource() 定位默认配置文件,不直接检查文件系统。 Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use.知道放置 log4j.properties 或 log4j.xml 的适当位置需要了解正在使用的类加载器的搜索策略。 log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments. log4j 不提供默认配置,因为在某些环境中可能禁止输出到控制台或文件系统。 Also see FAQ: Why can't log4j find my properties in a J2EE or WAR application?.另请参阅常见问题解答:为什么 log4j 无法在 J2EE 或 WAR 应用程序中找到我的属性?。

The configuration file cannot be found.找不到配置文件。 Are you using xml or a property file??您使用的是 xml 还是属性文件?

Also, use logback !另外,使用logback

Alright, so I got it working by changing this好的,所以我通过改变它来让它工作

log4j.rootLogger=DebugAppender

to this对此

log4j.rootLogger=DEBUG, DebugAppender

Apparently you have to specify the logging level to the rootLogger first?显然你必须先指定rootLogger的日志级别? I apologize if I wasted anyone's time.如果我浪费了任何人的时间,我深表歉意。

Also, I decided to answer my own question because this wasn't a classpath issue.另外,我决定回答我自己的问题,因为这不是类路径问题。

If you are encountering the error/warning when you're running a program that uses log4j, the solution is to add a log4j.properties that can be seen by the class loader.如果在运行使用 log4j 的程序时遇到错误/警告,解决方案是添加类加载器可以看到的 log4j.properties。 Normally it's in the "src" folder of your Java project: Add the following contents to the file通常它在您的 Java 项目的“src”文件夹中:将以下内容添加到文件中

log4j.rootLogger=INFO, stdout

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{ABSOLUTE} %5p %c{1}:%L - %m%n

The fix for me was to put "log4j.properties" into the "src" folder.我的解决方法是将“log4j.properties”放入“src”文件夹中。 It did not work in my package folder it has to be one up at the very root of source.它在我的包文件夹中不起作用,它必须是源代码的根目录。 Adding it to the build path which then moves it to the "Reference Libraries" also did not work.将它添加到构建路径,然后将其移动到“参考库”也不起作用。

To repeat: put log4j.properties at the root of your src folder.重复:将 log4j.properties 放在 src 文件夹的根目录下。

my log4j.properties files has the following content which works on the latest version of Spring Tool Suite which is based on Eclipse.我的 log4j.properties 文件具有以下内容,适用于基于 Eclipse 的最新版本的 Spring Tool Suite。

# Configure logging for testing: optionally with log file
#log4j.rootLogger=INFO, stdout
log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%

This work for me:这对我有用:

public static void main(String[] args) {

        Properties props = new Properties();
        props.load(new FileInputStream("src/log4j.properties"));
        PropertyConfigurator.configure(props);

None of answered method solve the problem which log4j.properties file is not found for non-maven jsf web project in NetBeans.没有回答的方法解决了在 NetBeans 中找不到非 Maven jsf web 项目的log4j.properties文件的问题。 So the answer is:所以答案是:

  1. Create a folder named resources in project root folder (outermost folder).在项目根文件夹(最外层文件夹)中创建一个名为resources文件夹。
  2. Right click project in projects explorer in left menu, select properties.在左侧菜单的项目资源管理器中右键单击项目,选择属性。
  3. Open Sources in Categories, add that folder in Source Package Folders.在类别中打开源,在源包文件夹中添加该文件夹。
  4. Open Run in Categories and add this to VM options: Dlog4j.configuration=resources/log4j.properties打开在类别中运行并将其添加到 VM 选项: Dlog4j.configuration=resources/log4j.properties
  5. Click Ok, build and deploy project as you like, that's it.单击确定,随心所欲地构建和部署项目,就是这样。

I wrote special pattern in log4j file to check whether log4j is used my file:我在 log4j 文件中写了特殊的模式来检查 log4j 是否用于我的文件:

# Root Logger Option
log4j.rootLogger=INFO, console

# Redirect Log Messages To Console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} xxxx (%F:%L) - %m%n

I checked it because if you use BasicConfigurator.configure();我检查了它,因为如果你使用BasicConfigurator.configure(); in your code in log4j use predefined pattern.在 log4j 中的代码中使用预定义的模式。

What worked for me is to create a log4j properties file (you can find many examples over the net) and place it in properties folder in your project directory (create this folder if not exicts).对我有用的是创建一个 log4j 属性文件(您可以在网上找到许多示例)并将其放在项目目录中的属性文件夹中(如果不存在,则创建此文件夹)。 The right click on the folder and Build Path->Use as Source Folder .右键单击文件夹和Build Path->Use as Source Folder

Hope it helps!希望能帮助到你!

补充一下@Gevorg,如果你在本地运行Spark也有类似情况,把log4j.properties文件放在主文件夹下的resources文件夹下。

Try doing something like the following in main:尝试在 main 中执行以下操作:

public static void main(String[] args) {
     PropertyConfigurator.configure(args[0]);
    //... your code

you need to tell log4j what its configuration should be.你需要告诉 log4j 它的配置应该是什么。

只需在 main 方法的开头配置您的 log4j 属性文件路径:例如:PropertyConfigurator.configure("D:\\files\\log4j.properties");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM