请正确初始化log4j系统。运行网络服务时

发布于 2024-11-19 00:45:32 字数 742 浏览 5 评论 0原文

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

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

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

我不知道为什么会这样。

我正在使用 Eclipse 生成 Web 服务并部署在 Tomcat 6.0 中。我检查 Axis 开发人员指南 并根据它们

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

我在 axis.jar 中没有找到 log4j.properties。

对此有什么帮助吗?

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

I have written a simple web service 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 Please initialize the log4j system properly.

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 Guide and according to them

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.

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

Any help on this?

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

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

发布评论

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

评论(7

纵性 2024-11-26 00:45:32

这些消息非常棘手,以至于人们创建了此错误报告让它更清楚。

它们的棘手之处在于,如果 Log4j 找不到其 log4j.properties(或 log4j.xml)文件,则会写入警告,但 如果文件很好,但从配置的角度来看其内容不完整。

以下段落摘自 log4j 的故障排除指南

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

log4j:找不到类别(some.category.name)的附加程序。
log4j:请正确初始化log4j系统。

Log4j 没有默认的日志记录目标。用户有责任确保所有类别都可以继承附加程序。通过将附加程序附加到根类别可以轻松实现这一点。

您可以在 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

这应该清除启动时收到的那些WARN消息。(确保您还没有名为 stdoutappender 还要注意您为根记录器指定的级别;debug 将非常冗长,并且应用程序中的每个库都会如此。开始向控制台写入内容。)

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

Those messages are something tricky, enough so that people created this bug report to make it clearer.

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 contents are not complete from a configuration point of view.

The following paragraph is taken from the log4j's troubleshooting guide:

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:

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.

You can find info on how to configure the root logger (log4j.rootLogger) in the log4j documentation; basically, by adding something as simple as this at the beginning of the file:

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 careful 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.)

As for the log4j.properties/log4j.xml file, 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). 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.

笑饮青盏花 2024-11-26 00:45:32

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

You have to create your own log4j.properties in the classpath folder.

屋檐 2024-11-26 00:45:32

好吧,如果您已经创建了 log4j.properties,您可以将其路径添加到类路径中,以便在执行过程中找到它。
是的,该东西将在类路径中搜索该文件。
既然您说您查看了 axis 但没有找到,我假设您没有 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

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

# 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
忆悲凉 2024-11-26 00:45:32

如果您的类中存在以下语句,那么您的 log4j.properties 应该位于 java source(src) 文件夹中,如果它是 jar 可执行文件,则应该打包在 jar 中而不是单独的文件中。

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

谢谢,

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.

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

Thanks,

旧伤还要旧人安 2024-11-26 00:45:32

警告无法找到记录器的附加程序意味着您正在使用log4j 日志系统,但您尚未在配置文件中添加任何 Appender(例如 FileAppender、ConsoleAppender、SocketAppender、SyslogAppender 等)或配置文件丢失。

配置 log4j 的方法有三种:使用属性文件 (log4j.properties)、使用 XML 文件和通过 Java 代码 (rootLogger.addAppender(new NullAppender());)。

如果您有属性文件(例如,安装 Solr 时),则需要将此文件放在 classpath 目录。

类路径

以下是一些如何确定类路径值的命令建议:

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

或者从 Java:System.getProperty("java.class.path")

Tomcat

如果您使用 Tomcat,您可以将 log4j.properties 放入:/usr/share/tomcat?/lib//var/lib/ tomcat?/webapps/*/WEB-INF/lib/ 文件夹。

Solr

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

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.

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());).

If you've property file present (e.g. when installing Solr), you need to place this file within your classpath directory.

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").

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.

Solr

For the reference, Solr log4j.properties looks 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
柒七 2024-11-26 00:45:32

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

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

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

yourproject/javaresoures/src/log4j.properties (Put inside src folder)
夜无邪 2024-11-26 00:45:32

您可以像上面的答案一样配置 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();
    }
}

因此在表下配置:

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

You can configure log4j.properties like above answers, or use 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)));
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文