在 Google App Engine 上使用 log4j

发布于 2024-09-26 03:50:33 字数 480 浏览 0 评论 0原文

我需要在我的应用程序中使用 log4j,但我不知道如何加载属性。默认属性文件说我应该将 log4j.properties 放入 /WEB-INF/classes/ 文件夹,但在 Eclipse 中我看不到该文件夹​​,也无法创建它,因为它已经存在。我也无法将任何文件添加到该文件夹​​。

这是我收到的错误:

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

So how can I get web application to load log4j properties?

I need to use log4j in my application, but I don't know how I can get properties loaded. Deafult properties files says that I should put log4j.properties to /WEB-INF/classes/ folder, but in eclipse I cannot see that folder and I cannot create it, because it already exists. And I cannot add any files to that folder either.

Here is error that I get:

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

So how can I get web application to load log4j properties?

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

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

发布评论

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

评论(5

你爱我像她 2024-10-03 03:50:33

将 log4j.properties 文件放入项目的源目录中,例如 /src。 Eclipse 会将其复制到您的目标构建目录中。

我建议在您的项目中使用 SLF4J 和 Log4J,以及 SpringSource 工具套件 (STS)。

Put the log4j.properties file into a source directory of your project, e.g. /src. Eclipse will copy it into your target build directory.

I recomend using SLF4J with Log4J, and the SpringSource Tool Suite (STS) for your project.

初见你 2024-10-03 03:50:33

以下介绍了如何使用带有 Google 插件的 Eclipse 让 log4j 正常工作。

修改 appengine-web.xml 如下:

<system-properties> 
  <property name="java.util.logging.config.file" value="WEB-INF/classes/log4j.properties"/> 
</system-properties> 

您可以将以下代码添加到您的 servlet 中:

import org.apache.log4j.Logger; 
... 
Logger logger = Logger.getLogger("com.foo"); 
logger.debug("Yay2!"); 

将 log4j.properties 文件放在 src/ 目录中,内容如下:

log4j.rootLogger=DEBUG, 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 %t %c{1}:%M:%L - %m%n 

您可以执行一个 Project >清理然后允许它自动构建,构建将 log4j.properties 文件复制到 /war/WEB-INF/classes/ 。当您运行为>时,您将看到显示的日志Web 应用程序并请求 URL。

我知道您没有使用 Maven,但我会在下面添加说明,以防其他人需要它们。这些说明适用于 com.google.appengine.archetypes:guestbook-archetype

在 pom.xml 中添加以下内容:

<dependency> 
<groupId>log4j</groupId> 
<artifactId>log4j</artifactId> 
<version>1.2.16</version> 
</dependency> 

在 guestbook.jsp 中添加以下代码:

<%@ page import="org.apache.log4j.Logger" %> 
... 
<% 
Logger logger = Logger.getLogger("com.foo"); 
logger.debug("Yay2!"); 
%> 

创建 src/main/webapp/WEB-INF/classes/log4j.properties,内容与上面相同。

然后运行:

mvn clean 
mvn verify 
mvn appengine:devserver 

调用 http://localhost:8080/ 后,您将在控制台中看到日志输出。

Here is how to get log4j working using Eclipse with the Google plugin.

Modify appengine-web.xml as follows:

<system-properties> 
  <property name="java.util.logging.config.file" value="WEB-INF/classes/log4j.properties"/> 
</system-properties> 

You can add the following code to your servlet:

import org.apache.log4j.Logger; 
... 
Logger logger = Logger.getLogger("com.foo"); 
logger.debug("Yay2!"); 

Put the log4j.properties file in the src/ directory with the following content:

log4j.rootLogger=DEBUG, 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 %t %c{1}:%M:%L - %m%n 

You can do a Project > Clean then allow it to automatically build, The build copies the log4j.properties file to /war/WEB-INF/classes/. You'll see the log displayed when you Run As > Web Application and request a URL.

I know that you're not using Maven but I will add instructions below in case anyone else needs them. These instructions will work with com.google.appengine.archetypes:guestbook-archetype.

Add the following to pom.xml:

<dependency> 
<groupId>log4j</groupId> 
<artifactId>log4j</artifactId> 
<version>1.2.16</version> 
</dependency> 

Add the following code to guestbook.jsp:

<%@ page import="org.apache.log4j.Logger" %> 
... 
<% 
Logger logger = Logger.getLogger("com.foo"); 
logger.debug("Yay2!"); 
%> 

Create src/main/webapp/WEB-INF/classes/log4j.properties with the same content as above.

Then run:

mvn clean 
mvn verify 
mvn appengine:devserver 

You will see log output in our console after calling http://localhost:8080/.

朮生 2024-10-03 03:50:33

我必须放置 log4j.properties,然后在 web.xml 中进行配置:

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> 
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

这样它就可以在类路径加载之前运行并且可以工作。

虽然它不会将 JUL 内容发送到 log4j,所以您将需要一个单独的配置来执行此操作。

I have to put the log4j.properties and then configured in web.xml:

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> 
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

That way it runs before the classpath loading and it works.

Although it doesn't send the JUL stuff to log4j, so you will need a separated configuration to do that.

筑梦 2024-10-03 03:50:33

忽略您在互联网上看到的所有关键字 spring + log4j + appengine。

对我有用并且没有产生歧义的解决方案是保留 JUL 并以这种方式单独使用 spring 配置 log4j:

public class CustomXmlWebApplicationContext extends XmlWebApplicationContext {
@Override
protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
    super.initBeanDefinitionReader(beanDefinitionReader);

    try {
        Resource res = beanDefinitionReader.getResourceLoader().getResource("classpath:log4j.properties");
        Properties props = new Properties();
        props.load(res.getInputStream());
        PropertyConfigurator.configure(props);
    }
    catch(Throwable e) {
        e.printStackTrace();
    }
}

}

然后只需将 log4j.properties 放在源文件夹的根目录中。

Ignore everything you see in the internet for the keywords spring + log4j + appengine.

The solution that worked for me and didn't created ambiguity was to leave JUL be and configure log4j with spring separately in this way:

public class CustomXmlWebApplicationContext extends XmlWebApplicationContext {
@Override
protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
    super.initBeanDefinitionReader(beanDefinitionReader);

    try {
        Resource res = beanDefinitionReader.getResourceLoader().getResource("classpath:log4j.properties");
        Properties props = new Properties();
        props.load(res.getInputStream());
        PropertyConfigurator.configure(props);
    }
    catch(Throwable e) {
        e.printStackTrace();
    }
}

}

Then just put your log4j.properties in the root of your source folder.

旧时光的容颜 2024-10-03 03:50:33

有一篇关于登录 Google AppEngine 的好文章用于 Java (GAE/J) 以及 Slf4j、Log4j 和 JUL

\src\main\webapp\WEB-INF\appengine-web.xml 中,您需要

appengine-web.xml

<!-- Configure java.util.logging -->
<system-properties>
    <property name="java.util.logging.config.file" value="WEB-INF/java-util-logging.properties"/>
</system-properties>

告诉 GAE java.util.logging (JUL) 的配置位置。

\src\main\webapp\WEB-INF\java-util-logging.properties 你需要

java-util-logging.properties

.level = ALL

或其他 JUL 级别名称,如您所愿(即“TRACE”不起作用)。

\src\main\resources\log4j.properties 文件中,您将拥有

log4j.properties

log4j.rootLogger=ALL, stdout
# or a lower log level such as DEBUG, INFO or WARN. 

# Define the destination and format of our logging
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=%-5p: %m  at %C.(%F:%L) on %d{ISO8601}%n

您需要将 log4j 添加到 CLASSPATH 中。我使用 Gradle 进行依赖管理,所以这是我的构建脚本:

构建.gradle

configurations {
    all*.exclude group: "commons-logging", module: "commons-logging"
}

dependencies {
    // Logging
    compile 'org.slf4j:slf4j-api:1.7.+'
    runtime 'org.slf4j:slf4j-jdk14:1.7.+'
    runtime ('log4j:log4j:1.2.17') {
        exclude group: "com.sun.jdmk", module: "jmxtools"
        exclude group: "com.sun.jmx", module: "jmxri"
        exclude group: "javax.mail", module: "mail"
        exclude group: "javax.jms", module: "jms"
    }    
}

如果您使用 Spring WebMVC,为了在启动应用程序时提供 Log4J 配置文件,请将以下侦听器添加到您的部署描述符中。

web.xml

<!-- The definition of the Log4j Configuration -->
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

There is a good article on Logging in the Google AppEngine for Java (GAE/J) with Slf4j, Log4j and JUL.

In \src\main\webapp\WEB-INF\appengine-web.xml you need to have

appengine-web.xml

<!-- Configure java.util.logging -->
<system-properties>
    <property name="java.util.logging.config.file" value="WEB-INF/java-util-logging.properties"/>
</system-properties>

to tell GAE where java.util.logging (JUL) is configured.

In \src\main\webapp\WEB-INF\java-util-logging.properties you need

java-util-logging.properties

.level = ALL

or another of the JUL level names, as you like (i.e. 'TRACE' does not work).

In \src\main\resources\log4j.properties file you will have

log4j.properties

log4j.rootLogger=ALL, stdout
# or a lower log level such as DEBUG, INFO or WARN. 

# Define the destination and format of our logging
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=%-5p: %m  at %C.(%F:%L) on %d{ISO8601}%n

You need to add log4j to your CLASSPATH. I use Gradle for dependency management, so here is my build script:

build.gradle

configurations {
    all*.exclude group: "commons-logging", module: "commons-logging"
}

dependencies {
    // Logging
    compile 'org.slf4j:slf4j-api:1.7.+'
    runtime 'org.slf4j:slf4j-jdk14:1.7.+'
    runtime ('log4j:log4j:1.2.17') {
        exclude group: "com.sun.jdmk", module: "jmxtools"
        exclude group: "com.sun.jmx", module: "jmxri"
        exclude group: "javax.mail", module: "mail"
        exclude group: "javax.jms", module: "jms"
    }    
}

If you are using Spring WebMVC, in order to provide a Log4J configuration file when starting up your application add the following listener to your Deployment descriptor.

web.xml

<!-- The definition of the Log4j Configuration -->
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文