Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1046197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:06:28+00:00 2026-05-16T16:06:28+00:00

I am using java.util logging classes to create a rolling file appender. I want

  • 0

I am using java.util logging classes to create a rolling file appender. I want to create a log reader that reads from these logs as data is written to them.

The rolling log appender code works fine on its own. But once I start the reader thread new files are not created i.e. if rolling log appender is set to use 5 files it will create 1og.0, log.1, log.2 and so on but if the reader thread starts then it will only create log.0 no other files are created. I notice this in both java logging and log4j.

I am using nio to read in the log reader. So my doubt is, is creating another FileChannel on the same file creating some problem? or have I missed some basic nio stuff here that is causing problems.

here is the code..

public class Test {

     private static final long initialTime = System.currentTimeMillis();
     private static Logger logger = Logger.getLogger(Test.class.getName());
     public static void main(String[] args) {

        logger.setLevel(Level.INFO);
        try {
            BufferedReader reader = new BufferedReader(
                                            new InputStreamReader(
                                                    new FileInputStream(
                                                            new File(System.getProperty("user.dir")+File.separator+"input.dat"))));

             FileHandler handler = new FileHandler("log/test.log", 1024, 5, true);
             handler.setFormatter(new SimpleFormatter());
             logger.addHandler(handler);
             logger.setUseParentHandlers(false);
             //If I comment the next two lines the rolling appender works as expected both in java logging
             //and when using log4j. however once I start the log reader only one file is created.
             Thread logReader = new Thread(new LogReader("log/test.log.0"));
             logReader.start();
            while(reader.ready())
            {
                String strToLog = reader.readLine();
                logger.info(strToLog);
                //Only want to run this for 10 secs.
                if(System.currentTimeMillis() - initialTime > 10000)
                    System.exit(0);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{

        }
    }

}
class LogReader implements Runnable {


    private final int BUFFER_SIZE = 1024;

    private RandomAccessFile file;

    private FileChannel chan;

    private long offset;

    private ByteBuffer buf;

    private Charset charset = Charset.forName("UTF-8");

    private String filename = "output.log";

    public LogReader(String logfile) throws IOException {
        System.out.println("Starting log reader from " + logfile);
        file = new RandomAccessFile(logfile, "r");
        offset = file.length();
        chan = file.getChannel();
        chan.position(offset);
        buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
        System.out.println("Started log reader from " + logfile);
    }

    public void run() {
        //Even if I have nothing here..the problem exists..
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:06:29+00:00Added an answer on May 16, 2026 at 4:06 pm

    I think there might be some concurrency issues with RandomAccessFile.

    I suggest you use a different class for reading the file. E.g. java.io.FileReader

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want simply to log on the console using java.util.Logging: Logger log = Logger.getLogger(my.logger);
I'm having trouble finding my physical log files. I'm using the java.util.logging classes. I
I am using java.util.logging.Logger But with storing messages in a log file, it also
I am using java.util.logging.Logger and I want to enable all log levels. I thought
When using java.util.logging, the log level for a specific class (i.e. for a child
Using the logging classes in java.util.logging, is it possible to set up two different
Using java.util.logging.Logger to output some log to the console just like this: public static
I'm trying to pass -Djava.util.logging.config.file=/path/to/my/logging.properties so that I can configure my loggers using the
I am using the default java.util.Logging API to print information to my log. Logger.getLogger(Datastore.class.getName());
I'm trying to modify a file in a zip-file using java.util.net As direct modification

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.