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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6608775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:12:27  来源:igfitidea点击:

Please initialize the log4j system properly. While running web service

javaweb-serviceslog4jaxis

提问by java_enthu

Maybe it looks silly to ask this but I am confused. I referred to Configuring Log4j propertybut it doesn't seem to help.

也许问这个问题看起来很傻,但我很困惑。我提到了配置 Log4j 属性,但它似乎没有帮助。

I have written a simple web service HelloWorld. And while running it I am getting the error something like this :

我编写了一个简单的 Web 服务 HelloWorld。在运行它时,我收到如下错误:

log4j:WARN No appenders could be found for logger (org.apache.axis.transport.http.AxisServlet). log4j:WARN Please initialize the log4j system properly.

log4j:WARN 找不到记录器 (org.apache.axis.transport.http.AxisServlet) 的附加程序。log4j:WARN 请正确初始化 log4j 系统。

I am not sure why its happening.

我不确定为什么会发生。

I am generating the web-service using Eclipse and deployed in Tomcat 6.0. I check on Axis Developer's Guideand according to them

我正在使用 Eclipse 生成 Web 服务并部署在 Tomcat 6.0 中。我查看了Axis Developer's Guide并根据他们

log4j.configuration=log4j.properties Use this system property to specify the name of a Log4J configuration file. If not specified, the default configuration file is log4j.properties. A log4j.properties file is provided in axis.jar.

log4j.configuration=log4j.properties 使用此系统属性来指定 Log4J 配置文件的名称。如果未指定,则默认配置文件为 log4j.properties。在axis.jar 中提供了一个log4j.properties 文件。

I didn't find log4j.properties in the axis.jar.

我没有在axis.jar.

Any help on this?

这有什么帮助吗?

采纳答案by flash

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. If no appenders are attached to a category nor to any of its ancestors, you will get the following message when trying to log:

使用 appender 将日志输出写入目标。如果没有附加程序附加到一个类别或其任何祖先,您将在尝试记录时收到以下消息:

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. It is the user's responsibility to ensure that all categories can inherit an appender. This can be easily achieved by attaching an appender to the root category.

Log4j 没有默认的日志记录目标。用户有责任确保所有类别都可以继承 appender。这可以通过将 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.rootLogger在 log4j文档中找到有关如何配置根记录器 ( ) 的信息,基本上是在文件开头添加如下简单的内容:

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, debugwill be very verbose and every library in your app will start writing stuff to the console).

这应该清除那些WARN在启动时你得到的消息(请确保您还没有一个appender命名stdout;也可以到什么程度,你给根记录小心,debug会很冗长,并且在你的应用程序的每个图书馆将开始写东西控制台)。

As about the log4j.properties/log4j.xml, I suggest you place this file in /WEB-INF/classesas it is important to have it exposed for different tweaks (activating/deactivating logs, changing log levels etc). 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.

至于log4j.properties/ log4j.xml,我建议您将这个文件放在其中,/WEB-INF/classes因为将它公开以进行不同的调整(激活/停用日志、更改日志级别等)很重要。您也可以将它放在类路径中的 JAR 中(如您在评论中指定的那样),但它将包含在存档中(希望在存档内的正确位置)并且不会像它那样容易处理在/WEB-INF/classes.

回答by flash

You have to create your own log4j.propertiesin the classpath folder.

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

回答by kenorb

Warning No appenders could be found for loggermeans that you're using log4jlogging 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 (e.g. when installing Solr), you need to place this file within your classpathdirectory.

如果您有属性文件(例如在安装 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.propertiesinto: /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.propertieslooks like:

作为参考,Solrlog4j.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

回答by Kannan

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,

谢谢,

回答by AlexD

Well, if you had already created the log4j.properties you would add its path to the classpath so it would be found during execution.
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.
Create it somewhere and add to your classpath. Put it for example, in c:/proj/resources/log4j.properties

In your classpath you simple add .......;c:/proj/resources

好吧,如果您已经创建了 log4j.properties,您可以将其路径添加到类路径中,以便在执行期间找到它。
是的,东西会在类路径中搜索这个文件。
既然你说你查看了轴并没有找到,我假设你没有 log4j.properties,所以这里有一个粗略但完整的例子。
在某处创建它并添加到您的类路径中。举个例子,在 c:/proj/resources/log4j.properties

在你的类路径中,你简单地添加......;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

回答by keshava

If you are using Logger.getLogger(ClassName.class)then place your log4j.propertiesfile in your class path:

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

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

回答by Knautiluz

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)));
  }