[英]EJB SLF4J with log4j - No appenders could be found
I'm trying to use SLF4J with log4j in a Bean inside a EJB, I have already tried to place the log4j.properties file in quite a few places but I keep getting this error in the Glassfish Server console: 我正在尝试将SLF4J与log4j一起使用在EJB内的Bean中,我已经尝试将log4j.properties文件放置在很多地方,但是我在Glassfish服务器控制台中始终遇到此错误:
Grave: log4j:WARN No appenders could be found for logger (uy.ort.enviosya.cadets.services.CadetsBean).
Grave: log4j:WARN Please initialize the log4j system properly.
Grave: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info
This is my properties file: 这是我的属性文件:
# LOG4J configuration
log4j.rootLogger=DEBUG, Appender1,Appender2
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=C:/Users/Log4jWebDemo.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
The questions related to this problem say I should place it in the classpath but honestly I don't know what they mean by that (totally new to EJB). 与这个问题相关的问题说我应该将其放在类路径中,但是老实说我不知道它们的含义(对EJB来说是全新的)。
This is what I'm doing in the bean: 这就是我在bean中所做的事情:
@Stateless
public class SomeBean implements SomeBeanRemote {
private static final Logger LOGGER = LoggerFactory.getLogger(SomeBean.class);
@Override
public someMethod() {
LOGGER.info("Prueba");
...
}
...
}
Plus, should I be placing the file inside the EJB? 另外,我应该将文件放在EJB内吗? What if I want to modify the destination of the
Appender2
then? 如果我想修改
Appender2
的目的地怎么办?
I'm using Netbeans 8.2. 我正在使用Netbeans 8.2。
I have placed it inside the META-INF
folder of my ejb, and from what I can see it is present in that same folder in the Cadets-ejb.jar
that I'm deploying inside .ear
. 我将其放置在我的ejb的
META-INF
文件夹中,从中可以看到它存在于我在.ear
内部部署的Cadets-ejb.jar
中的同一文件夹中。
But I'm still getting the same error. 但是我仍然遇到同样的错误。
Partial folder structure: 部分文件夹结构:
Some
build
Some-ejb
build
dist
nbproject
src
conf
META-INF
java
test
dist
lib
nbproject
src
I managed to get it working but by placing the properties file by hand in the correct place in the build folder, which means I need to do this every time I do a clean-build. 我设法使其正常运行,但是通过将属性文件手动放置在build文件夹中的正确位置,这意味着每次执行干净构建时都需要这样做。
I tried placing it in the src folder but when I build it doesn't get copied, only what is inside src/conf
gets copied and that is the wrong location for this file. 我尝试将其放置在src文件夹中,但是在构建时不会复制它,只会复制
src/conf
,而这是该文件的错误位置。
Why is the file in src
being ignored on build? 为什么在构建时会忽略
src
中的文件?
This is my structure now: 现在是我的结构:
Some
/build
/Some-ejb
/build
/dist
/nbproject
/src
/conf
/META-INF
MANIFEST.MF
/java
/log4j.properties
/test
/dist
/lib
/log4j.jar
/nbproject
/src
I now that the file, once the jar is created needs no be at the same level that META-INF
and my class packages. 现在我知道,一旦创建了jar,该文件就不必与
META-INF
和我的类包处于同一级别。
I solved it by placing it here 我把它放在这里解决了
Some
/build
/Some-ejb
/build
/dist
/nbproject
/src
/conf
/META-INF
MANIFEST.MF
/java
log4j.properties
/test
/dist
/lib
/log4j.jar
/nbproject
/src
After I googled a lot, i think i found a solution for this: 在我搜索了很多之后,我想我找到了一个解决方案:
FOR A GLOBAL CONFIGURATION 进行全球配置
If your log4j library is included within your EAR file, then check your app server's JVM properties to ensure the log4j.configuration property is set: 如果您的EAR文件中包含log4j库,请检查应用服务器的JVM属性,以确保已设置log4j.configuration属性:
-Dlog4j.configuration
exists, verify that it contains the location of your log4j.properties file -Dlog4j.configuration
的条目,请验证它是否包含log4j.properties文件的位置。 -Dlog4j.configuration
, create one. -Dlog4j.configuration
的条目不存在 ,请创建一个。 It must follow the following template: -Dlog4j.configuration=file:///path/to/your/log4j.properties
-Dlog4j.configuration=file:///path/to/your/log4j.properties
In case your project doesn't contain log4j library 如果您的项目不包含log4j库
FOR A PROJECT 对于一个项目
For instance, you can have the following structure: 例如,您可以具有以下结构:
Some
/build
/Some-ejb
/build
/dist
/nbproject
/src
/conf
/META-INF
MANIFEST.MF
/java
/test
/dist
/lib
/log4j.jar
/nbproject
/src
/log4j.properties
In order to include the log4j-files into your classpath, you have to put the following into the META-INF/MANIFEST.MF
on your EJB Project: 为了将log4j文件包括到您的类路径中,您必须将以下内容放入EJB Project的
META-INF/MANIFEST.MF
:
Class-Path: src lib/log4j.jar
Yes: it's relative to the EAR, not to the EJB Project. 是的:它与EAR有关,而不与EJB项目有关。
Do not put the log4j.properties
itself in the classpath, only the directory that contains that file. 不要将
log4j.properties
本身放在类路径中,而只能放在包含该文件的目录中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.