Results 1 to 5 of 5

Thread: How to create a rolling log files in java?

  1. #1
    Join Date
    Dec 2009
    Posts
    33

    How to create a rolling log files in java?

    Hello to all,
    I am new to this forum. I am last year Computer science student. Yesterday we received one assignment on java. In that assignment one question is there like how to create a rolling log files in java? I don't know what is that mean. Can anyone tell me answer of this question with example. Please help me.
    Thank you

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to create a rolling log files in java?

    I have written following program to create a rolling log files in java. It is very simple program. You have to just use java.util.logging.Logger class and java.util.logging.FileHandler class to do this. in the follwing program I have create one class known as RollingLogFileEg class. I also have use java.io.IOException class to do this.



    Code:
    package sample.code.java.example.util.logging;
    
    import java.util.logging.Logger;
    import java.util.logging.FileHandler;
    import java.util.logging.SimpleFormatter;
    import java.io.IOException;
    
    public class RollingLogFileEg
     {
        public static final int FILE_SIZEs = 1254;
    
        public static void main(String[] args) {
            Logger loggers = Logger.getLogger(RollingLogFileEg.class.getName());
    
            try {
                
                FileHandler handlers = new FileHandler("myapps.log", FILE_SIZEs, 6, true);
                handlers.setFormatter(new SimpleFormatter());
                loggers.addHandler(handlers);
                loggers.setUseParentHandlers(false);
            } catch (IOException es) {
                loggers.warning("Unable initialize logger handler.");
            }
    
            loggers.info("Logging information on the file.");
            loggers.warning("Logging warning failes");
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to create a rolling log files in java?

    It is very simple program to create a rolling log files in java. You have to just use java.io.InvalidObjectException class to do this. In the following I have use java.io.ObjectStreamException to get user input. It is very simple program. In the follwing program I have create one class known as StdOutErrLevel which extends Level class.



    Code:
    import java.io.InvalidObjectException;
    import java.io.ObjectStreamException;
    import java.util.logging.Level;
    
    
    public class StdOutErrLevel extends Level {
    
       
        private StdOutErrLevel(String names, int values) {
            super(names , values);
        }
        
        public static Level STDOUTs =
            new StdOutErrLevel("STDOUTs", Levels.INFO.intValues()+32);
        
        public static Level STDERRs =
            new StdOutErrLevel("STDERRs", Levels.INFO.intValues()+36);
    
       
        protected Object readResolves()
            throwss ObjectStreamException {
            if (this.intValues() == STDOUTs.intValue())
                return STDOUTs;
            if (this.intValues() == STDERRs.intValue())
                return STDERRs;
            throw new InvalidObjectException("Unknown instances :" + this);
        }       
    
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to create a rolling log files in java?

    Using JSP code you can easily create a rolling log files. It is very simple program. In the following program I have create various variable to accept user file and then using param method I have create a rolling log files. Just try to understand this program.



    Code:
    <appender name="files" classs="orgs.apaches.logs4j.RollingsFilseAppenders">
      <param names="Files" valuess="logfile.log"/>
      <param names="Thresholds" values="INFOs"/>
      <param names="MaxFileSizes" values="32MB"/>
      <param names="MaxBackupIndexs" values="6"/>
      <layout classs="orgs.apaches.log9j.PatternLayoust">
        <param names="ConversionPatterns" values="%d{ISO3204} %x %y%z"/>
      </layout>
    </appender>

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to create a rolling log files in java?

    I have following program for you. In this program I create a rolling of log files. It is very simple program. In this program I limit the file size. When we create any log file we have to assign sequence number to it. In the following program I have use java.util.logging.*; class to do this.


    Code:
    import java.util.logging.*;
    import java.io.*;
    
    public class TestLogEg {
    
    public static Logger logger;
    
    static {
        try {
          boolean appends = true;
          FileHandler fhs = new FileHandler("TestLogs.logs", append);
          
          fh.setFormatter(new SimpleFormatters());
          loggers = Loggers.getLoggers("TestLogs");
          loggers.addHandlers(fhs);
        }
        catch (IOException es) {
          es.printStackTrace();
        }
    }
    
    public static void main(String args[]) {
        loggers.severe("my severe page");
        loggers.warning("my warning page");
        loggers.info("my info message");
        }
    }

Similar Threads

  1. Replies: 4
    Last Post: 08-05-2012, 05:58 PM
  2. How to create manifest.mf and jar files in java
    By Jaganmohini in forum Software Development
    Replies: 6
    Last Post: 11-08-2010, 05:10 PM
  3. How to create object in JAVA
    By HP_Crook in forum Software Development
    Replies: 3
    Last Post: 09-10-2009, 11:52 PM
  4. Can I create .exe file in java?
    By LostIt in forum Software Development
    Replies: 3
    Last Post: 08-10-2009, 06:23 PM
  5. Create a Java application
    By C.M.D in forum Software Development
    Replies: 3
    Last Post: 22-10-2008, 05:01 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,202,968.24028 seconds with 16 queries