简体   繁体   English

请正确初始化 log4j 系统。 在运行 Web 服务时

[英]Please initialize the log4j system properly. While running web service

Maybe it looks silly to ask this but I am confused.也许问这个问题看起来很傻,但我很困惑。 I referred to Configuring Log4j property but it doesn't seem to help.我提到了配置 Log4j 属性,但它似乎没有帮助。

I have written a simple web service HelloWorld.我编写了一个简单的 Web 服务 HelloWorld。 And while running it I am getting the error something like this :在运行它时,我收到如下错误:

log4j:WARN No appenders could be found for logger (org.apache.axis.transport.http.AxisServlet). log4j:WARN 找不到记录器 (org.apache.axis.transport.http.AxisServlet) 的附加程序。 log4j:WARN Please initialize the log4j system properly. log4j:WARN 请正确初始化 log4j 系统。

I am not sure why its happening.我不确定为什么会发生。

I am generating the web-service using Eclipse and deployed in Tomcat 6.0.我正在使用 Eclipse 生成 Web 服务并部署在 Tomcat 6.0 中。 I check on Axis Developer's Guide and according to them我查看了Axis Developer's Guide并根据他们

log4j.configuration=log4j.properties Use this system property to specify the name of a Log4J configuration file. log4j.configuration=log4j.properties 使用此系统属性来指定 Log4J 配置文件的名称。 If not specified, the default configuration file is log4j.properties.如果未指定,则默认配置文件为 log4j.properties。 A log4j.properties file is provided in axis.jar.在axis.jar 中提供了一个log4j.properties 文件。

I didn't find log4j.properties in the axis.jar .我没有在axis.jar找到 log4j.properties 。

Any help on this?这有什么帮助吗?

Those messages are something tricky, enough so that people created this to make it clearer: https://issues.apache.org/bugzilla/show_bug.cgi?id=25747这些消息有些棘手,足以让人们创建它以使其更清晰: https : //issues.apache.org/bugzilla/show_bug.cgi? id =25747

What's tricky about them is that the warnings are written if Log4j can't find its log4j.properties (or log4j.xml ) file, but also if the file is fine and dandy but its content is not complete from a configuration point of view.它们的棘手之处在于,如果 Log4j 找不到它的log4j.properties (或log4j.xml )文件,并且文件很好,但从配置的角度来看它的内容不完整,则会写入警告。

The following paragraph is taken from here: http://svn.apache.org/repos/asf/logging/log4j/tags/v1_2_9/docs/TROUBLESHOOT.html以下段落摘自此处: http : //svn.apache.org/repos/asf/logging/log4j/tags/v1_2_9/docs/TROUBLESHOOT.html

Logging output is written to a target by using an appender.使用 appender 将日志输出写入目标。 If no appenders are attached to a category nor to any of its ancestors, you will get the following message when trying to log:如果没有附加程序附加到一个类别或其任何祖先,您将在尝试记录时收到以下消息:

log4j: No appenders could be found for category (some.category.name).
log4j: Please initialize the log4j system properly.

Log4j does not have a default logging target. Log4j 没有默认的日志记录目标。 It is the user's responsibility to ensure that all categories can inherit an appender.用户有责任确保所有类别都可以继承 appender。 This can be easily achieved by attaching an appender to the root category.这可以通过将 appender 附加到根类别来轻松实现。

You can find info on how to configure the root logger ( log4j.rootLogger ) in the log4j documentation , basically adding something as simple as this at the beginning of the file:您可以在 log4j文档中找到有关如何配置根记录器 ( log4j.rootLogger ) 的信息,基本上是在文件开头添加如下简单内容:

log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

This should clear those WARN messages you get on startup (make sure you don't already have an appender named stdout ; also be carefull of what level you give the root logger, debug will be very verbose and every library in your app will start writing stuff to the console).这应该会清除你在启动时收到的那些 WARN 消息(确保你还没有一个名为stdout的 appender ;还要小心你给根记录器的级别, debug将非常冗长,你的应用程序中的每个库都将开始编写东西到控制台)。

As about the log4j.properties / log4j.xml , I suggest you place this file in /WEB-INF/classes as it is important to have it exposed for different tweaks (activating/deactivating logs, changing log levels etc).至于log4j.properties / log4j.xml ,我建议您将此文件放在/WEB-INF/classes因为将其公开以进行不同的调整(激活/停用日志、更改日志级别等)很重要。 You can have it inside a JAR in the classpath also (as you specified in your comment), but it will be enclosed in the archive (hopefully in the right place inside the archive) and won't be as easy to handle as if it were in /WEB-INF/classes .您也可以将它放在类路径中的 JAR 中(如您在评论中指定的那样),但它将包含在存档中(希望在存档内的正确位置)并且不会像它那样容易处理在/WEB-INF/classes

您必须在类路径文件夹中创建自己的log4j.properties

Well, if you had already created the log4j.properties you would add its path to the classpath so it would be found during execution.好吧,如果您已经创建了 log4j.properties,您可以将其路径添加到类路径中,以便在执行期间找到它。
Yes, the thingy will search for this file in the classpath.是的,东西会在类路径中搜索这个文件。
Since you said you looked into axis and didnt find one, I am assuming you dont have a log4j.properties, so here's a crude but complete example.既然你说你查看了轴并没有找到,我假设你没有 log4j.properties,所以这里有一个粗略但完整的例子。
Create it somewhere and add to your classpath.在某处创建它并添加到您的类路径中。 Put it for example, in c:/proj/resources/log4j.properties举个例子,在c:/proj/resources/log4j.properties

In your classpath you simple add .......;c:/proj/resources在您的类路径中,您只需添加 .......;c:/proj/resources

# 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, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=c:/project/resources/t-output/log4j-application.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

Warning No appenders could be found for logger means that you're using log4j logging system, but you haven't added any Appenders (such as FileAppender, ConsoleAppender, SocketAppender, SyslogAppender, etc.) into your configuration file or the configuration file is missing.警告No appender could be found for logger表示您使用的是log4j日志系统,但您尚未在配置文件中添加任何 Appender(例如 FileAppender、ConsoleAppender、SocketAppender、SyslogAppender 等)或配置文件丢失.

There are three ways to configure log4j: with a properties file ( log4j.properties ), with an XML file and through Java code ( rootLogger.addAppender(new NullAppender()); ).有三种配置 log4j 的方法:使用属性文件 ( log4j.properties )、使用 XML 文件和通过 Java 代码 ( rootLogger.addAppender(new NullAppender()); )。

If you've property file present (eg when installing Solr), you need to place this file within your classpath directory.如果您有属性文件(例如在安装 Solr 时),您需要将此文件放在您的类路径目录中。

classpath类路径

Here are some command suggestions how to determine your classpath value:以下是一些如何确定类路径值的命令建议:

$ echo $CLASSPATH
$ ps wuax | grep -i classpath
$ grep -Ri classpath /etc/tomcat? /var/lib/tomcat?/conf

or from Java: System.getProperty("java.class.path") .或来自 Java: System.getProperty("java.class.path")

Tomcat雄猫

If you're using Tomcat, you may place your log4j.properties into: /usr/share/tomcat?/lib/ or /var/lib/tomcat?/webapps/*/WEB-INF/lib/ folder.如果您使用 Tomcat,您可以将log4j.properties放入: /usr/share/tomcat?/lib//var/lib/tomcat?/webapps/*/WEB-INF/lib/文件夹。

Solr索尔

For the reference, Solr log4j.properties looks like:作为参考,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

If the below statment is present in your class then your log4j.properties should be in java source(src) folder , if it is jar executable it should be packed in jar not a seperate file.如果您的类中存在以下语句,则您的 log4j.properties 应位于 java source(src) 文件夹中,如果它是 jar 可执行文件,则应将其打包在 jar 中而不是单独的文件中。

static Logger log = Logger.getLogger(MyClass.class);

Thanks,谢谢,

如果您正在使用Logger.getLogger(ClassName.class)则将您的log4j.properties文件放在您的类路径中:

yourproject/javaresoures/src/log4j.properties (Put inside src folder)

You can configure log4j.properties like above answers, or use org.apache.log4j.BasicConfigurator您可以像上面的答案一样配置 log4j.properties,或使用org.apache.log4j.BasicConfigurator

public class FooImpl implements Foo {

    private static final Logger LOGGER = Logger.getLogger(FooBar.class);

    public Object createObject() {
        BasicConfigurator.configure();
        LOGGER.info("something");
        return new Object();
    }
}

So under the table, configure do:所以在表下,配置做:

  configure() {
    Logger root = Logger.getRootLogger();
    root.addAppender(new ConsoleAppender(
           new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
  }

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

相关问题 当以jar运行Java应用程序时,出现log4j警告(请正确初始化log4j系统。) - log4j warning (Please initialize the log4j system properly.) when running java application as jar 运行 Apache.Spark - log4j:WARN 请正确初始化 log4j 系统 - Running Apache.Spark - log4j:WARN Please initialize the log4j system properly Log4j配置问题-log4j:WARN请正确初始化log4j系统 - Log4j Configuration issues - log4j:WARN Please initialize the log4j system properly 请正确初始化log4j系统警告 - Please initialize the log4j system properly warning log4j:WARN请正确初始化log4j系统 - log4j:WARN Please initialize the log4j system properly log4j:WARN请正确初始化log4j系统 - log4j:WARN Please initialize the log4j system properly “ log4j:WARN请正确初始化log4j系统”错误 - “log4j:WARN Please initialize the log4j system properly” error log4j:WARN找不到记录器的附加程序log4j:WARN请正确初始化log4j系统 - log4j:WARN No appenders could be found for logger log4j:WARN Please initialize the log4j system properly log4j:WARN找不到记录器的附加程序/请在Eclipse中通过Selenium执行测试时正确初始化log4j系统 - log4j:WARN No appenders could be found for logger / Please initialize the log4j system properly while executing tests through Selenium in Eclipse 正确初始化log4j系统 - Properly initialize log4j system
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM