0

I've been trying to set up logging for my application which is deployed on GlassFish 3.1.2.2. Firstly, I've read some tutorials how to configure SLF4J and apply it to my app and GF. I found that one which was very helpful because logging started to work. However the way Glassfish stores logs is very annoying for me. I found some answers (this post seemed to be the satisfying one) but unfortunately I met some problem which I can't solve. I wrote my own formatter and put it into Glassfish just like in the post above and it works but only partly. When I see the logs in NetBeans console they look the way I would like them to look:

INFO: 2014-03-30 20:48:51,768 INFO  p.e.a.w.kino.rk.rest.SessionService - User (admin) has successfully logged out

However, in the server.log file they still have the GlassFish format

[#|2014-03-30 20:48:51.769|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=93;_ThreadName=Thread-2;|2014-03-30 20:48:51,768 INFO  p.e.a.w.kino.rk.rest.SessionService - User (admin) has successfully logged out.
|#]

I created two files with logging properties as it was suggested in the first link, so I attach this two files: _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=false
com.sun.enterprise.server.logging.GFFileHandler.rotationLimitInBytes=2000000
com.sun.enterprise.server.logging.GFFileHandler.alarms=false
com.sun.enterprise.server.logging.GFFileHandler.formatter=pl.edu.amu.wmi.kino.rk.utils.ReportKeeperLogFormatter
com.sun.enterprise.server.logging.GFFileHandler.retainErrorsStasticsForHours=0
com.sun.enterprise.server.logging.GFFileHandler.level=ALL
java.util.logging.FileHandler.formatter=pl.edu.amu.wmi.kino.rk.utils.ReportKeeperLogFormatter

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
  <layout class="ch.qos.logback.classic.PatternLayout">
   <Pattern>%d %-5level %logger{36} - %msg%n</Pattern>
  </layout>
 </appender>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">        
    <file>${com.sun.aas.instanceRoot}/logs/server.log</file>        
    <encoder>
        <pattern>%date %level [%file:%line] %msg%n</pattern>
    </encoder>
    </appender>

 <logger name="com.sido">
  <level value="debug" />
 </logger>

 <logger name="org.springframework">
  <level value="debug" />
 </logger>

 <root>
  <level value="debug" />
  <appender-ref ref="console" />
 </root>
</configuration>

On some other site I found that thing with another appender in logback.xml but it didn't work.

UPDATE I changed the file in "FILE" appender in logback.xml to some other file, i.e. temp_server.log and the file was created but nothing was appended to it.

Could you tell me what am I missing or doing wrong?

Community
  • 1
  • 1

1 Answers1

0

I found solution today. I didn't read exactly how to deal with file appenders. Now everything is alright so I'll post my solution - changed logback.xml. Now it supports File Rolling Policy and saves logs the way I wanted.

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
  <layout class="ch.qos.logback.classic.PatternLayout">
   <Pattern>%d %-5level %logger{36} - %msg%n</Pattern>
  </layout>
 </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${com.sun.aas.instanceRoot}/logs/rk_log.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${com.sun.aas.instanceRoot}/logs/rk_log_%d{yyyy-MM-dd}.log</FileNamePattern>
        </rollingPolicy>

        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%date{YYYY-MM-dd HH:mm:ss} %-5level %logger{35} - %msg%n</Pattern>
        </layout>
    </appender>

 <root level="INFO">
    <appender-ref ref="FILE"/>
 </root>   

 <logger name="com.sido">
  <level value="debug" />
 </logger>

 <logger name="org.springframework">
  <level value="debug" />
 </logger>

 <root>
  <level value="debug" />
  <appender-ref ref="console" />
 </root>
</configuration>
  • Note: you are still breaking the specification by writing directly to the file system (just think what will happen in a clustered environment). That said you can just do normal logging to a file outside Glassfish and not mess with the Glassfish configuration. – Thorbjørn Ravn Andersen Mar 31 '14 at 21:31