13 Replies Latest reply on Feb 21, 2013 12:19 PM by jamezp

    JBoss7 Logging Rotating Feature

    tcwang88

      Hi,

       

      I was testing the JBoss 7 logging feature, and I found that JBoss provides two approachs to rotate log files.

       

      * size-rotating-file-handler

           Here is the configuration I set in standalone.xml

          

           <size-rotating-file-handler name="FILE">

                <formatter>

                     <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

                </formatter>

                <file relative-to="jboss.server.log.dir" path="server.log"/>

                <rotate-size value="20k"/>

                <max-backup-index value="10"/>

                <append value="true"/>

           </size-rotating-file-handler>

       

           This works fine for rotating log files.

       

      * periodic-rotating-file-handler

           For this approach, it doesn't work in my environment.

           Here is the configuration I set in standalone.xml

       

           <periodic-rotating-file-handler name="FILE">

                <formatter>

                     <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

                </formatter>

                <file relative-to="jboss.server.log.dir" path="server.log"/>

                <suffix value=".yyyy-MM-dd"/>

                <append value="true"/>

           </periodic-rotating-file-handler>

       

           I tried to modify the system time of OS, and also tried to wait for one day to start the server again, but the two tests failed; the logs were still going to server.log and did not rotate. Is there anything I missed to set? What I expect this feature should be creating a new server.log each single day, and making the old one to server.log.yyyy-MM-dd.

       

       

      Besides, I am also looking for a feature that rotating log files every time I startup JBoss. Does JBoss provide this ability?

       

       

      Thanks,

       

      Tzu-Chi

        • 1. Re: JBoss7 Logging Rotating Feature
          jamezp

          We just did change the way the rollover by date works. We now first look at the file's last modification date to determine when to rollover, but that does you know good now :-) For testing though on the periodic rotating file you could change the suffix to mm which would be minutes.

           

          There is no handler for rotating on subsequent start-ups. You could easily write one and use a custom handler though. It would be simple enough to extend either java.util.logging.FileHandler or org.jboss.logmanager.handlers.FileHandler and on the setFile() method just check if the file exists. If it exists rename it to a new file name.

           

          Something like this might work.

           

          import java.io.File;
          import java.io.FileNotFoundException;
          
          
          import org.jboss.logmanager.handlers.FileHandler;
          
          
          /**
           * @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
           */
          public class BootRotatingFileHandler extends FileHandler {
              private int maxBackupIndex = 1;
          
          
              public BootRotatingFileHandler() {
                  super();
              }
          
          
              public BootRotatingFileHandler(final File file) throws FileNotFoundException {
                  super(file);
              }
          
          
              public BootRotatingFileHandler(final String fileName) throws FileNotFoundException {
                  super(fileName);
              }
          
          
              public BootRotatingFileHandler(final File file, final int maxBackupIndex) throws FileNotFoundException {
                  this.maxBackupIndex = maxBackupIndex;
                  setFile(file);
              }
          
          
              public BootRotatingFileHandler(final String fileName, final int maxBackupIndex) throws FileNotFoundException {
                  this.maxBackupIndex = maxBackupIndex;
                  setFileName(fileName);
              }
          
          
              @Override
              public void setFile(final File file) throws FileNotFoundException {
                  if (file != null) {
                      synchronized (outputLock) {
                          rotateFile(file);
                      }
                  }
                  super.setFile(file);
              }
          
          
              /**
               * Set the maximum backup index (the number of log files to keep around).
               *
               * @param maxBackupIndex the maximum backup index
               */
              public void setMaxBackupIndex(final int maxBackupIndex) {
                  checkAccess(this);
                  synchronized (outputLock) {
                      this.maxBackupIndex = maxBackupIndex;
                  }
              }
          
          
              private void rotateFile(final File file) {
                  final int maxBackupIndex = this.maxBackupIndex;
                  if (maxBackupIndex > 0) {
                      if (file == null) {
                          return;
                      }
                      new File(file.getAbsolutePath() + "." + maxBackupIndex).delete();
                      for (int i = maxBackupIndex - 1; i >= 1; i--) {
                          new File(file.getAbsolutePath() + "." + i).renameTo(new File(file.getAbsolutePath() + "." + (i + 1)));
                      }
                      file.renameTo(new File(file.getAbsolutePath() + ".1"));
                  }
              }
          }
          
          
          • 2. Re: JBoss7 Logging Rotating Feature
            tcwang88

            Hi James,

             

            Thanks for the information you provided. The custom-handler works well and that is what I expect.

             

             

            Sincerely,

             

            Tzu-Chi

            • 3. Re: JBoss7 Logging Rotating Feature
              tcwang88

              Hi James,

               

              To extend from org.jboss.logmanager.handlers.FileHandler works fine for me right now; however, I tried to extend from java.util.logging.FileHandler, and I faced some issues.

               

              First, there is no setFile API in FileHandler, and the way to set fileName is from constructor.

               

              Second, although I passed the fileName to FileHandler constructor, I can see the logs were written into that log file. However, there was another file came with it called "server.log.lck" which was generated by FileHandler.openFiles(), and should be cleanup at close(). How can I invoke the close method when the server is shutting down?

               

               

              Thanks,

               

              Tzu-Chi

              • 4. Re: JBoss7 Logging Rotating Feature
                jamezp

                Yeah, it's definitely easier to extend JBoss Logging as it's FileHandler is much better IMO. I can understand not wanting the outside dependency, but FWIW it's got some nice built in i18n capabilities. To you use those there is an additional build-time only dependency. Weŕe working on getting some info on JBoss Logging up, but here is some information on the i18n stuff if you are interested https://community.jboss.org/wiki/JBossLoggingTooling.

                 

                Good catch on the shutdown not closing the handlers. There should probably be a shutdown handler that does that, but currently there is not. I went ahead and created a JIRA for that https://issues.jboss.org/browse/AS7-4528.

                • 5. Re: JBoss7 Logging Rotating Feature
                  guinotphil

                  Hi,

                   

                  I think there is another bug:

                   

                  Here is my handler:

                  <periodic-rotating-file-handler name="FILE_ERROR">

                      <level name="INFO"/>

                      <formatter>

                          <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

                      </formatter>

                      <file relative-to="jboss.server.log.dir" path="server.log"/>

                      <suffix value=".yyyy-MM-dd-a"/>

                      <append value="true"/>

                  </periodic-rotating-file-handler>

                   

                  And before startup, last modification time of server.log:

                   

                  Wed Sep 12 10:27:46 CEST 2012

                   

                   

                  => calculated next roll over time :

                  Wed Sep 12 22:00:00 CEST 2012

                   

                   

                  It should be Wed Sep 12 12:00:00 CEST 2012

                   

                   

                  Actually the line

                  calendar.clear(Calendar.HOUR);

                  does not affect the hour.

                  it should be

                  calendar.set(Calendar.HOUR, 0);

                   

                   

                  Thanks for reading

                  • 6. Re: JBoss7 Logging Rotating Feature
                    dmlloyd

                    Would it be possible to try out this patch:

                     

                    http://github.com/dmlloyd/jboss-logmanager/commit/46b4a26dd0e

                     

                    It should fix this issue and maybe one or two more similar ones...

                    • 7. Re: JBoss7 Logging Rotating Feature
                      guinotphil

                      Thank you very much for the quick fix.

                       

                      I'm gonna try it now.

                      • 8. Re: JBoss7 Logging Rotating Feature
                        rohanemmanuel

                        hi ,

                        i have the following logging.properties, if i dont use append then every minute the file is overwritten. but if i have append as true the file will be appended but the file never gets rotated in any of the case. im using the jboss logging in an standalone application. can you please point out whats missing or something wrong in the configuration.?

                         

                        # Root logger level

                        logger.level=DEBUG

                        # Root logger handlers

                        logger.handlers=FILE

                         

                         

                        # Console handler configuration

                        #handler.CONSOLE=org.jboss.logmanager.handlers.ConsoleHandler

                        #handler.CONSOLE.properties=autoFlush

                        #handler.CONSOLE.level=DEBUG

                        #handler.CONSOLE.autoFlush=true

                        #handler.CONSOLE.formatter=PATTERN

                        handler.FILE=org.jboss.logmanager.handlers.PeriodicRotatingFileHandler

                        handler.FILE.level=DEBUG

                        handler.FILE.properties=autoFlush,append,fileName,suffix

                        handler.FILE.autoFlush=true

                        handler.FILE.append=true

                        handler.FILE.fileName=C:/jbosslogging.log

                        handler.FILE.suffix=.yyyy-MM-dd-HH-mm

                        handler.FILE.formatter=PATTERN

                         

                         

                         

                         

                        # Formatter pattern configuration

                        formatter.PATTERN=org.jboss.logmanager.formatters.PatternFormatter

                        formatter.PATTERN.properties=pattern

                        #formatter.PATTERN.pattern=%d{HH:mm:ss,SSS} %-5p [%c] %s%E%n

                        formatter.PATTERN.pattern=%d{HH:mm:ss,SSS} %C.%M:%L (%t) %5p %c{1}:%L - %m%n

                         

                        Regards ,

                        Rohan Emmanuel

                        • 9. Re: JBoss7 Logging Rotating Feature
                          jamezp

                          I just tested and it seems to be rotating fine for me. What version of the log manager are you using? The newest is 1.4.0.Final.

                           

                          --

                          James R. Perkins

                          • 10. Re: JBoss7 Logging Rotating Feature
                            rohanemmanuel

                            Hi James ,

                            thanks for the quick response,

                             

                            im using 1.3.1 version of log manager.....i just checked , if i have one standalone app then the file is rotating fine....when i have more than one (tried with two as of now)standalone apps logging into the same file(they are using same logging.properties file)  then the file is not rotating.please advice

                             

                            --

                            Rohan Emmanuel

                            • 11. Re: JBoss7 Logging Rotating Feature
                              jamezp

                              I definitely wouldn't have two different applications writing to the same log files. There is no guarantee what will happen. You could lose log messages or experience what you're seeing. The handlers are thread-safe, but writing from two different JVM's could result in the unexplained behavior you're seeing.

                               

                              --

                              James R. Perkins

                              • 12. Re: JBoss7 Logging Rotating Feature
                                rohanemmanuel

                                ya...thats explains it...thanks...

                                any suggestions on this, if i want two or more applications to log into same file...can it be done ?

                                 

                                ---

                                Rohan Emmanuel

                                • 13. Re: JBoss7 Logging Rotating Feature
                                  jamezp

                                  Only way I can think of would be some kind of log server that would handle writing the log statements.

                                   

                                  --

                                  James R. Perkins