Log4j 初始化时出现警告?

发布于 2024-12-08 18:45:25 字数 360 浏览 0 评论 0原文

我正在尝试了解 log4j 所以我只是尝试做一些非常简单的事情;

Logger logger = Logger.getLogger("ClientApplicationLog");
logger.info("Logger Test");

但做了这个之后我得到了;

log4j:WARN No appenders could be found for logger (ClientApplicationLog).
log4j:WARN Please initialize the log4j system properly.

你知道我错在哪里吗?

谢谢大家

I am trying to learn about log4j so I just tried to do something which is very simple;

Logger logger = Logger.getLogger("ClientApplicationLog");
logger.info("Logger Test");

But after making this I got;

log4j:WARN No appenders could be found for logger (ClientApplicationLog).
log4j:WARN Please initialize the log4j system properly.

Do you know where I am wrong ?

Thank you all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

Hello爱情风 2024-12-15 18:45:25

您的类路径中缺少 log4j.propertieslog4j.xml

您可以使用以下方法绕过此问题,

BasicConfigurator.configure();

但请注意,这只会登录到 System.out,因此不建议这样做。您确实应该使用上面的文件之一并写入日志文件。

log4j.properties 的一个非常简单的示例是

#Log to Console as 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{yyyy-MM-dd HH:mm:ss} %-5p %c %3x - %m%n
#Log to file FILE
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logfile.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c %3x - %m%n

#Root Logger
log4j.rootLogger=INFO, stdout, file

You're missing the log4j.properties or log4j.xml in your classpath.

You can bypass this by using

BasicConfigurator.configure();

But beware this will ONLY log to System.out and is not recommended. You should really use one of the files above and write to a log file.

A very simple example of log4j.properties would be

#Log to Console as 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{yyyy-MM-dd HH:mm:ss} %-5p %c %3x - %m%n
#Log to file FILE
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logfile.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c %3x - %m%n

#Root Logger
log4j.rootLogger=INFO, stdout, file
你是年少的欢喜 2024-12-15 18:45:25

您需要在类路径中的某个位置有一个 log4j.xml,其中包含告诉它在哪里记录、如何记录等信息。或者,您可以在代码中以编程方式设置所有这些,但在实现中具有灵活性会更好。

我的 log4j.xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- ============================== -->
   <!-- Append messages to the console -->
   <!-- ============================== -->

   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
      <param name="Target" value="System.out"/>
      <param name="Threshold" value="DEBUG"/>

      <layout class="org.apache.log4j.PatternLayout">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

<!-- ======================= -->
   <!-- Setup the Root category -->
   <!-- ======================= -->

   <root>
      <!-- 
         Set the root logger priority via a system property. Note this is parsed by log4j         
       -->
      <appender-ref ref="CONSOLE"/>
   </root>

</log4j:configuration>

You need to have a log4j.xml somewhere in your class path with information telling it where to log, how to log etc. Alternatively you can set all this programmatically in your code, but it's much nicer to have the flexibility in your implementation.

My log4j.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- ============================== -->
   <!-- Append messages to the console -->
   <!-- ============================== -->

   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
      <param name="Target" value="System.out"/>
      <param name="Threshold" value="DEBUG"/>

      <layout class="org.apache.log4j.PatternLayout">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

<!-- ======================= -->
   <!-- Setup the Root category -->
   <!-- ======================= -->

   <root>
      <!-- 
         Set the root logger priority via a system property. Note this is parsed by log4j         
       -->
      <appender-ref ref="CONSOLE"/>
   </root>

</log4j:configuration>
沩ん囻菔务 2024-12-15 18:45:25

这只是警告。

修复

当找不到默认配置文件 log4j.propertieslog4j.xml 并且应用程序未执行显式配置时,会出现此问题。

要解决此问题,只需创建/复制 log4j.propertieslog4j.xml 到类路径上的某个位置(通常与 jar 文件相同)。

(可选)设置 java 选项:-Dlog4j.configuration=file:///path/to/log4j.properties

log4j 使用 Thread.getContextClassLoader().getResource() 来定位默认配置文件,并不直接检查文件系统。了解放置 log4j.propertieslog4j.xml 的适当位置需要了解所使用的类加载器的搜索策略。 log4j 不提供默认配置,因为在某些环境中可能禁止输出到控制台或文件系统。

调试

为了调试,您可以尝试使用-Dlog4j.debug=true参数。

log4j.properties 的配置

log4j.properties 的示例配置:

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

这是另一个使用多个附加程序的配置文件:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Apache Solr

如果使用 Solr,则复制 ; /example/resources/log4j.properties 到类路径上的某个位置。

Solr 的 log4j.properties 示例配置如下:

#  Logging level
solr.log=logs/
log4j.rootLogger=INFO, file, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n

#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9

#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n

log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN

# set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF

另请参阅:

This is just the warning.

Fixing

This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration.

To fix that, simply create/copy log4j.properties or log4j.xml into your a location on the classpath (usually the same as the jar files).

Optionally set java option: -Dlog4j.configuration=file:///path/to/log4j.properties.

log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use. log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments.

Debugging

For debugging, you may try to use -Dlog4j.debug=true parameter.

Configuration of log4j.properties

Sample configuration of log4j.properties:

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

Here is another configuration file that uses multiple appenders:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Apache Solr

If using Solr, copy <solr>/example/resources/log4j.properties into a location on the classpath.

Sample configuration of log4j.properties from Solr goes like:

#  Logging level
solr.log=logs/
log4j.rootLogger=INFO, file, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n

#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9

#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n

log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN

# set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF

See also:

可是我不能没有你 2024-12-15 18:45:25

您需要为您的记录器(例如日志文件)定义一个附加程序,例如在类路径上的 log4j.properties 文件中。

本教程应该包含您需要了解的所有内容。

You need to define an appender for your logger (e.g. a log file), e.g. in a log4j.properties files on the classpath.

This tutorial should contain everything you need to know.

痴情换悲伤 2024-12-15 18:45:25

似乎您的 log4j.properties 不在类路径中。确保它是,并且在您的配置文件中定义了一个名为“ClientApplicationLog”的记录器。

Seems like your log4j.properties is not in the classpath. Make sure it is and that within your config-file you have a logger with the name "ClientApplicationLog" defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文