在 Google App Engine 上使用 log4j
我需要在我的应用程序中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将 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.
以下介绍了如何使用带有 Google 插件的 Eclipse 让 log4j 正常工作。
修改 appengine-web.xml 如下:
您可以将以下代码添加到您的 servlet 中:
将 log4j.properties 文件放在 src/ 目录中,内容如下:
您可以执行一个 Project >清理然后允许它自动构建,构建将 log4j.properties 文件复制到 /war/WEB-INF/classes/ 。当您运行为>时,您将看到显示的日志Web 应用程序并请求 URL。
我知道您没有使用 Maven,但我会在下面添加说明,以防其他人需要它们。这些说明适用于 com.google.appengine.archetypes:guestbook-archetype。
在 pom.xml 中添加以下内容:
在 guestbook.jsp 中添加以下代码:
创建 src/main/webapp/WEB-INF/classes/log4j.properties,内容与上面相同。
然后运行:
调用
http://localhost:8080/
后,您将在控制台中看到日志输出。Here is how to get log4j working using Eclipse with the Google plugin.
Modify appengine-web.xml as follows:
You can add the following code to your servlet:
Put the log4j.properties file in the src/ directory with the following content:
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:
Add the following code to guestbook.jsp:
Create src/main/webapp/WEB-INF/classes/log4j.properties with the same content as above.
Then run:
You will see log output in our console after calling
http://localhost:8080/
.我必须放置 log4j.properties,然后在 web.xml 中进行配置:
这样它就可以在类路径加载之前运行并且可以工作。
虽然它不会将 JUL 内容发送到 log4j,所以您将需要一个单独的配置来执行此操作。
I have to put the log4j.properties and then configured in web.xml:
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.
忽略您在互联网上看到的所有关键字 spring + log4j + appengine。
对我有用并且没有产生歧义的解决方案是保留 JUL 并以这种方式单独使用 spring 配置 log4j:
}
然后只需将 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:
}
Then just put your log4j.properties in the root of your source folder.
有一篇关于登录 Google AppEngine 的好文章用于 Java (GAE/J) 以及 Slf4j、Log4j 和 JUL。
在
\src\main\webapp\WEB-INF\appengine-web.xml
中,您需要告诉 GAE java.util.logging (JUL) 的配置位置。
在
\src\main\webapp\WEB-INF\java-util-logging.properties
你需要或其他 JUL 级别名称,如您所愿(即“TRACE”不起作用)。
在
\src\main\resources\log4j.properties
文件中,您将拥有您需要将 log4j 添加到 CLASSPATH 中。我使用 Gradle 进行依赖管理,所以这是我的构建脚本:
如果您使用 Spring WebMVC,为了在启动应用程序时提供 Log4J 配置文件,请将以下侦听器添加到您的部署描述符中。
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 haveto tell GAE where java.util.logging (JUL) is configured.
In
\src\main\webapp\WEB-INF\java-util-logging.properties
you needor another of the JUL level names, as you like (i.e. 'TRACE' does not work).
In
\src\main\resources\log4j.properties
file you will haveYou need to add log4j to your CLASSPATH. I use Gradle for dependency management, so here is my build script:
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.