3

I have a web application which is deployed on GlassFish 3.1.1. The current situation is that all log statements, means the ones from GlassFish and the ones from my application, are written into server.log of GlassFish.

I need a configuration which separates the logging, so that it logs to two different files, one for GlassFish (server.log) and one for my application (application.log).

I tried the following:

I added this to my domain.xml:

<jvm-options>-Dlog4j.configuration=file:///${com.sun.aas.instanceRoot}/config/_logging.properties</jvm-options>
<jvm-options>-Dlogback.configurationFile=file:///${com.sun.aas.instanceRoot}/config/logback.xml</jvm-options>

This is my logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
  <encoder>
   <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} %-5p %class:%L - %m%ex</pattern>
  </encoder>
 </appender>

 <appender name="defaultLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
   <file>application.log</file>
   <encoder>
     <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} %-5p %F - %m%n%ex</pattern>
   </encoder>
   <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
     <fileNamePattern>application.%i{yyyy-MM-dd}.log</fileNamePattern>
   </rollingPolicy>
   <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
     <MaxFileSize>5MB</MaxFileSize>
   </triggeringPolicy>
 </appender>

 <root level="INFO">
  <appender-ref ref="console" />
   <appender-ref ref="defaultLog"/>
 </root>
</configuration>

and this is the _logging.properties:

handlers = org.slf4j.bridge.SLF4JBridgeHandler
com.sun.enterprise.server.logging.GFFileHandler.flushFrequency=1
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.logtoConsole=true
com.sun.enterprise.server.logging.GFFileHandler.rotationLimitInBytes=2000000
com.sun.enterprise.server.logging.GFFileHandler.alarms=false
com.sun.enterprise.server.logging.GFFileHandler.formatter=com.sun.enterprise.server.logging.UniformLogFormatter
com.sun.enterprise.server.logging.GFFileHandler.retainErrorsStasticsForHours=0

The original logging.properties which was included in GlassFish is at its initial state.

I log messages like this:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Stub {

    private Logger logger = LoggerFactory.getLogger(Stub.class);

    public void test() {
        logger.info("hello");
    }
}

Now, with this configuration I have the following situation:

  • all log statements are printed in the NetBeans console
  • the log statements from my application are logged to application.log
  • the log statements from my application are additionally logged to server.log
  • the log statements from GlassFish are logged to server.log

Nearly what I want, but this obviously creates some redundancy and isn't a proper solution for the goal (separate the logging).

If I remove the ConsoleAppender from the logback.xml so it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <appender name="defaultLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
   <file>application.log</file>
   <encoder>
     <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} %-5p %class - %m%n%ex</pattern>
   </encoder>
   <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
     <fileNamePattern>application.%i{yyyy-MM-dd}.log</fileNamePattern>
   </rollingPolicy>
   <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
     <MaxFileSize>5KB</MaxFileSize>
   </triggeringPolicy>
 </appender>

 <root level="INFO">
  <appender-ref ref="console" />
   <appender-ref ref="defaultLog"/>
 </root>
</configuration>

then I have only GlassFish specific log messages in server.log and only application error messages in application.log, but the application log messages do not appear in the NetBeans console (because they are not appended to the console obviously).

I don't really get how this is working in general, so one question is, why does it (partially) work this way? How does logback know that only the application specific messages should be logged into application.log? Why are the GlassFish messages not logged into application.log?

I suspect that GlassFish logs his stuff in a different way? I guess I have to somehow change the configuration of the normal GlassFish logging, but I don't have a clue how. I tried setting the log level for my application packages to OFF in the normal logging.properties but this doesn't change anything, I guess this is because the log messages look like this:

[#|2014-05-23T16:43:02.948+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=10;_ThreadName=Thread-2;|23 Mai 2014 16:43:02.947 INFO  com.test.MyInitializer - [Module initialized]|#]

It looks like it can't filter the messages because they come from javax.enterprise.system.std.com.sun.enterprise.server.logging, but if I filter this package, the log messages are not logged in the console.

My main question: How to fix the issue, how to log only application specific messages to a different log file, all GlassFish log messages to server.log and really ALL log messages to the console?

unwichtich
  • 13,712
  • 4
  • 53
  • 66

0 Answers0