0

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/confgets 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-INFand 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
moondaisy
  • 4,303
  • 6
  • 41
  • 70
  • Try this https://stackoverflow.com/questions/17673670/logging-using-log4j-xml-in-glassfish or this https://stackoverflow.com/questions/12532339/no-appenders-could-be-found-for-loggerlog4j – Carlos Laspina Jun 09 '17 at 20:25
  • @CarlosLaspina Thanks, I have read those and tried but I keep getting the error. I have updated the question explaining where it is now. – moondaisy Jun 10 '17 at 14:39
  • What is your folder structure for your project? What is the version of you server? – Carlos Laspina Jun 10 '17 at 17:29
  • @CarlosLaspina just included a partial folder structure – moondaisy Jun 10 '17 at 17:34
  • try this https://stackoverflow.com/questions/1485987/where-should-i-put-the-log4j-properties-file https://sites.google.com/site/bugsarewelcome/home/configure-log4j-for-use-in-glassfish-3-1 or http://javaworkbench.blogspot.com/2011/07/configure-log4j-with-glassfish.html – Carlos Laspina Jun 10 '17 at 18:35

1 Answers1

0

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.

Carlos Laspina
  • 2,013
  • 4
  • 27
  • 44
  • In this case every ear that I deploy to the server will be using the same properties, right? I'm deploying more than one and I want them to be independent. – moondaisy Jun 10 '17 at 18:53
  • I updated my answer, please try this and give me your feedback. – Carlos Laspina Jun 10 '17 at 21:15
  • I tried it and have updated the question, by placing it in the `src` folder it doesn't get copy to the `build` folder on build – moondaisy Jun 11 '17 at 14:33