15

I have configured glassfish 3 to use SLF4J in the past by using the SLF4J JUL bridge and it worked fine. The problem I'm having now is that if I use my same setup, SLF4J complains about there being duplicate SLF4J bindings on the classpath and after searching, glassfish itself contains those reference.

How do I get SLF4J working? I tried making my dependency upon the SLF4J-API as provided, and then removed LogBack Classic since glassfish already contains some logback code. Doing those things doesn't yield any successful results.

Walter

2
  • Can you please add some details about the error messages you get?
    – foch
    Dec 12, 2013 at 18:41
  • When I configure glassfish to use SLF4J, all my messages are gone essentially, no error.
    – Walter
    Dec 12, 2013 at 19:28

2 Answers 2

7

This may helps you

Download Glassfish 4, SLF4J and Logback

Stop gf4

$GF_INSTALL\bin>asadmin stop-domain

and then

Copy

  • jul-to-slf4j-1.7.5
  • slf4j-api-1.7.5
  • logback-core-1.0.13
  • logback-classic-1.0.13

to

$GF_INSTALL/glassfish/lib/endorsed

Create logback.xml in

$GF_INSTALL/glassfish/domains/domain1/config

containing

<configuration debug="true" scan="true">
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>/tmp/gf_server.log</file>
        <append>true</append>
        <encoder>
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{52} - %msg%n</Pattern>
        </encoder>
    </appender>
    <root>
        <level value="INFO"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>

Modify

$GF_INSTALL/glassfish/domains/domain1/config/logging.properties

and replace

handlers=java.util.logging.ConsoleHandler
handlerServices=com.sun.enterprise.server.logging.GFFileHandler
java.util.logging.ConsoleHandler.formatter=com.sun.enterprise.server.logging.UniformLogFormatter
com.sun.enterprise.server.logging.GFFileHandler.formatter=com.sun.enterprise.server.logging.ODLLogFormatter
com.sun.enterprise.server.logging.GFFileHandler.file=${com.sun.aas.instanceRoot}/logs/server.log
com.sun.enterprise.server.logging.GFFileHandler.rotationTimelimitInMinutes=0
com.sun.enterprise.server.logging.GFFileHandler.flushFrequency=1
java.util.logging.FileHandler.limit=50000
com.sun.enterprise.server.logging.GFFileHandler.logtoConsole=false
com.sun.enterprise.server.logging.GFFileHandler.rotationLimitInBytes=2000000
com.sun.enterprise.server.logging.GFFileHandler.excludeFields=
com.sun.enterprise.server.logging.GFFileHandler.multiLineMode=true
com.sun.enterprise.server.logging.SyslogHandler.useSystemLogging=false
java.util.logging.FileHandler.count=1
com.sun.enterprise.server.logging.GFFileHandler.retainErrorsStasticsForHours=0
log4j.logger.org.hibernate.validator.util.Version=warn
com.sun.enterprise.server.logging.GFFileHandler.maxHistoryFiles=0
com.sun.enterprise.server.logging.GFFileHandler.rotationOnDateChange=false
java.util.logging.FileHandler.pattern=%h/java%u.log
java.util.logging.FileHandler.formatter=java.util.logging.XMLFormatter

with

handlers=org.slf4j.bridge.SLF4JBridgeHandler
handlerServices=com.sun.enterprise.server.logging.GFFileHandler
java.util.logging.ConsoleHandler.formatter=com.sun.enterprise.server.logging.UniformLogFormatter
com.sun.enterprise.server.logging.GFFileHandler.formatter=com.sun.enterprise.server.logging.ODLLogFormatter
com.sun.enterprise.server.logging.GFFileHandler.file=/tmp/server.log
com.sun.enterprise.server.logging.GFFileHandler.rotationTimelimitInMinutes=0
com.sun.enterprise.server.logging.GFFileHandler.flushFrequency=1
java.util.logging.FileHandler.limit=50000
com.sun.enterprise.server.logging.GFFileHandler.logtoConsole=false
com.sun.enterprise.server.logging.GFFileHandler.rotationLimitInBytes=2000000
com.sun.enterprise.server.logging.GFFileHandler.excludeFields=
com.sun.enterprise.server.logging.GFFileHandler.multiLineMode=true
com.sun.enterprise.server.logging.SyslogHandler.useSystemLogging=false
java.util.logging.FileHandler.count=1
com.sun.enterprise.server.logging.GFFileHandler.retainErrorsStasticsForHours=0
log4j.logger.org.hibernate.validator.util.Version=warn
com.sun.enterprise.server.logging.GFFileHandler.maxHistoryFiles=0
com.sun.enterprise.server.logging.GFFileHandler.rotationOnDateChange=false
java.util.logging.FileHandler.pattern=%h/java%u.log
com.sun.enterprise.server.logging.GFFileHandler.formatter=com.sun.enterprise.server.logging.UniformLogFormatter
com.sun.enterprise.server.logging.GFFileHandler.alarms=false

Add this two new JVM options to

domain->configs->config->java-config

in

$GF_INSTALL/glassfish/domains/domain1/config/domain.xml

<jvm-options>-Djava.util.logging.config.file=${com.sun.aas.instanceRoot}/config/logging.properties</jvm-options>
<jvm-options>-Dlogback.configurationFile=file:///${com.sun.aas.instanceRoot}/config/logback.xml</jvm-options>

then start gf4 again

$GF_INSTALL\bin>asadmin start-domain

7
  • Part of that looks similar to what I already did, I hope to try this within the next 2 days and report back. Thanks,
    – Walter
    Dec 19, 2013 at 1:49
  • You are welcome, Walter, it works as expected for me in a clean gf4.
    – vzamanillo
    Dec 19, 2013 at 7:33
  • At first glance, it appears to be working; however, I only tried a subset of those things: the important one I already had (SLF4JBridgeHandler), I updated java.util.logging.config.file and logback.configurationFile to a locally provided one. I noticed even though my configuration says DEBUG for the root level, it only logs INFO or higher.
    – Walter
    Dec 19, 2013 at 15:20
  • Also, I am using the maven glassfish plugin, so it is slightly different.
    – Walter
    Dec 19, 2013 at 15:21
  • @WalterWhite did you manage to fix the problem with logging only being of the level INFO or higher? I am experiencing the same problem with embedded glassfish.
    – nickebbitt
    Jan 16, 2014 at 15:54
1

If someone else having trouble with configured ch.qos.logback.core.rolling.RollingFileAppender, i might have a solution for you.

The problem is, that following the instructions of @vzamanillo, logback itself is initialized twice. This is caused by classloader isolation of GlassFish (one time by main ClassLoader, one time by EAR ClassLoader). It becomes visible, if u configure logback with debug=true. If a RollingFileAppender is used now, two OutputStreams were opened on the same configured logfile. This prevents the logfile to be removed during rollover (visible in debug mode).

To fix this issue, i moved the libraries from /modules/endorsed to /domains/domain1/lib/ext. Now GlassFish 4 (concrete Payara) initializes logback only once and everything works as expected.

By the way: to replace JUL logging, specifying <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator" /> is a MUST, otherwise you are facing big performance issues.

To dispatch also the loggings of potential 3rd party libs to SLF4j, don't forget to install the dispatcher jars as well, which are i.e.:

  • jcl-over-slf4j.jar
  • jul-to-slf4j.jar
  • log4j-over-slf4j.jar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.