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

Ask A Question

Stats

  • Questions 535k
  • Answers 535k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer My compiler mangles things a little different to yours (OSX… May 17, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer I don't think it's possible to connect to an app… May 17, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer ok, it is in the documentation but I feel it's… May 17, 2026 at 1:09 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am using java.util.logging.Logger But with storing messages in a log file, it also
Using the logging classes in java.util.logging, is it possible to set up two different
I would like to do this using java.util.logging if possible, any ideas? thanks.
I'm trying to figure out how to use the java.util.logging features. All of the
I'm trying to read a long type from a text file using Scanner in
I am attempting to create Domino DateTime objects in arbitrary time zones using the
I'm configuring the logging for a Java application. What I'm aiming for is two
I am used to using Tomcat/Log4J for my logging. My current project is deployed
I'm writing a java application. Currently this produces a file called trace.txt in the
Using Java, Is there a quick way to convert an alpha-2 country code (IN

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.