Random problem

I need to write a file when its contents are added, I will read a new line and work on the contents of a new line. The file length will never decrease (in fact, this is the tomcat log file).

I use the following codes:


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.apache.log4j.Logger;

import com.zjswkj.analyser.ddao.LogEntryDao;
import com.zjswkj.analyser.model.LogEntry;
import com.zjswkj.analyser.parser.LogParser;

public class ListenTest {
    private RandomAccessFile    raf;
    private long                lastPosition;
    private String              logEntryPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\S+) \"([^\"]+)\" \"([^\"]+)\"";
    private static Logger       log             = Logger.getLogger(ListenTest.class);

    public void startListenLogOfCurrentDay() {

        try {
            if (raf == null)
                raf = new RandomAccessFile(
                        "/tmp/logs/localhost_access_log.2010-12-20.txt",
                        "r");
            String line;
            while (true) {
                raf.seek(lastPosition);
                while ((line = raf.readLine()) != null) {
                    if (!line.matches(logEntryPattern)) {
                        // not a complete line,roll back
                        lastPosition = raf.getFilePointer() - line.getBytes().length;
                        log.debug("roll back:" + line.getBytes().length + " bytes");
                        if (line.equals(""))
                            continue;
                        log.warn("broken line:[" + line + "]");
                        Thread.sleep(2000);
                    } else {
                        // save it
                        LogEntry le = LogParser.parseLog(line);
                        LogEntryDao.saveLogEntry(le);
                        lastPosition = raf.getFilePointer();
                    }
                }
            }
        } catch (FileNotFoundException e) {
            log.error("can not find log file of today");
        } catch (IOException e) {
            log.error("IO Exception:" + e.getMessage());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new ListenTest().startListenLogOfCurrentDay();
    }
}

Now my problem is that if the line that is being written to the new line of the file is not completed, a dead loop will occur.

For example, if tomcat tries to write a new line to a file:

10.33.2.45 - - [08/Dec/2010:08:44:43 +0800] "GET /poi.txt HTTP/1.1" 200 672 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"

(: < 10.33.2.45 - - [08/Dec/2010: 08: 44: 43 +0800] "GET/poi.txt HTTP/1.1" 200 672 > ), , , tomcat , - 2 , .

( , tomcat ), , randomaccessfile , , , ,.

- ?

: "" :

10.33.2.45 - - [08/Dec/2010:08:44:43 +0800] "GET /poi.txt HTTP/1.1" 200 672 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"
+3
4

( ), - / , , 2

1: . log4j, tomcat

, - log4j. Appender

Log4j DBAppender, , , , DBAppender, . app custome appender.

log4j.rootLogger = DEBUG, S

log4j.appender.S = com.gurock.smartinspect.log4j.MyCustomAppender

log4j.appender.S.layout = org.apache.log4j.SimpleLayout

AsyncAppender DBAppender, .

2: , tomcat log4j

, , SO. , . . RandomAccessFile.

+3

, . log4j. .

Google .

0

, , - .

GrowingFileReader, readLine , . .

, lastPosition? ?

0

RAF ( ). , lines.getBytes() , readLine / .

BufferedReader RAF, fooobar.com/questions/1034374/...

0

Source: https://habr.com/ru/post/1781178/


All Articles