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 7013329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:22:22+00:00 2026-05-27T22:22:22+00:00

I am trying to read a log file continuously where the logs are being

  • 0

I am trying to read a log file continuously where the logs are being written ,both the processes are happening simultaneously.I am using rolling file appender for generating logs but the problem is ,when the file is about to change sometimes the data written when the file is nearing end is not read.Here is my code that generates log file continuously.

import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class Slf4jSample {

    static Logger logger = LoggerFactory.getLogger(Slf4jSample.class);

    public static void main(final String[] args) {
        int delay = 0; // delay for 5 sec. 

        int period = 1000;  // repeat every sec.
        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {
            int i = 0;

            public void run() {
                // Task here ...
                for (i = 0; i < 50; i++) {
                    logger.error("testing" + i);
                    System.out.println(i);
                }
            }
        }, delay, period);
    }
}

and here is my code that is reading it continuously.

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.mobility.config.ParseConfig;
import com.mobility.mail.SendMail;
import com.mobility.vo.ConfigurationVO;
import com.mobility.vo.LogVO;
import javax.mail.*;


            TimerTask fileWatcherTask = new LogWatcher(fileName) {

                long addFileLen = fileName.length();
                FileChannel channel, oldChannel;
                FileLock lock, oldLock;

                @Override
                protected void onChange(File file) {

                    RandomAccessFile access = null;
                    RandomAccessFile oldAccess = null;
                    int level = 0;
                    try {
                        access = new RandomAccessFile(file, "rw");
                        oldAccess = new RandomAccessFile(prevFileName, "rw");
                        channel = access.getChannel();
                        lock = channel.lock();
                        oldChannel = oldAccess.getChannel();
                        oldLock = oldChannel.lock();
//                      System.out.println("addFileLen>>" + addFileLen);

                        if (file.length() <= addFileLen) {
                            System.out.println("in if>>");
                            // reading old file

                            if (prevFileName.exists()) {
                                parsingLog(oldAccess, addFileLen,level);
                                addFileLen = file.length();
                            }
                            // reading old file ends

//                          Seek to 0th position when the file changes.
                            parsingLog(access, 0,level);
                        } else {
//                          access.seek(addFileLen);
                            parsingLog(access, addFileLen,level);
                            System.out.println("else>>>");
                            addFileLen = file.length();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        lock.release();
                        oldLock.release();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    } // Close the file

                    try {
                        channel.close();
                        oldChannel.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };

            Timer timer = new Timer();
            // repeat the check every second
            long scheduleTimer = Long.parseLong(properties
                    .getProperty("scheduler.timer"));
            timer.schedule(fileWatcherTask, new Date(), scheduleTimer);
        }
    }

    public void parsingLog(RandomAccessFile access, long fileLength, int level) {

        List<String> logList = new ArrayList<String>();
        int flag = 0;
        String line = null;
        try {
            access.seek(fileLength);
            while ((line = access.readLine()) != null) {

                 System.out.println("after reading line >"+line);

                }

    }catch(Exception e){
}
}

PS:it doesn’t read the contents when it is nearing end.

  • 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-27T22:22:23+00:00Added an answer on May 27, 2026 at 10:22 pm

    I would open the file until it has been fully read and only close it when you have finished reading it. This may require your logger to create a new file each time, but you are unlikely to miss anything this way.

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

Sidebar

Related Questions

I'm Trying to Write Logs to Custom Log.txt File on Android File using this
I'm trying to read a Log4net XMLLayout output log file using Log4View. <?xml version=1.0?>
I'm trying to read a log file of log4net: FileStream fs = new FileStream(filePath,
I am trying to log a method's parameters by using reflection. I read the
In Perl I am trying to read a log file and will print only
I'm trying to find a nice way to read a log file in real
I am trying to read a log file with the content look like this:
I am trying to read a file I create that contains all the logs
I am trying to read the log files or any other file and display
I am trying to read data from a .log file and process its contents.

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.