简体   繁体   中英

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:

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

This is what I'm doing in the 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? What if I want to modify the destination of the Appender2 then?

I'm using Netbeans 8.2.

Update 1

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 .

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

Update 2

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.

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.

Why is the file in src being ignored on build?

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.

Solution

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:

  1. Login to the Glassfish Admin Console ( http://[hostname]:4848/ )
  2. Click on 'Server(Admin Server)'-> Click on 'server-config' Configuration -> JVM Settings -> JVM Options. (depends of your server version, but the important is JVM Settings > JVM Options)
  3. If an entry for -Dlog4j.configuration exists, verify that it contains the location of your log4j.properties file
  4. If an entry do not exist for -Dlog4j.configuration , create one. It must follow the following template: -Dlog4j.configuration=file:///path/to/your/log4j.properties
  5. Restart the GlassFish.
  6. Deploy sample app and check if the Log4J statements are now available.

In case your project doesn't contain log4j library

  1. Copy log4j.jar inside the GLASSFISH_HOME/lib.

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:

Class-Path: src lib/log4j.jar

Yes: it's relative to the EAR, not to the EJB Project.

Do not put the log4j.properties itself in the classpath, only the directory that contains that file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM