How to parse this postgresql log file?

  • Thread starter Joost Kraaijeveld
  • Start date
J

Joost Kraaijeveld

Hi,

i want to parse a PostgreSQL log file in Java to analyse it. It contains
entries that may be 1 to N lines per entry but every entry is preceded
by a timestamp that looks like this "2006-12-08 06:36:28".

I have read some docs about StreamTokenizer but that did not help me much.

Any pointer to docs or code is appreciated..

TIA
 
J

Joost Kraaijeveld

To partially answer my own question, but with an additional question:

How do I get the string that is actually matched?

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Application {

public static void main(String[] args) {
String string = "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]";
try {
Scanner sc = new Scanner(new BufferedReader(new
FileReader("postgresql.log")));
sc.useDelimiter(string);

int i = 0;
while (sc.hasNext() == true && ++i < 20) {
String logLine = sc.next();
System.out.println(logLine.replaceAll("\n", " "));
}
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
 
E

EricF

Hi,

i want to parse a PostgreSQL log file in Java to analyse it. It contains
entries that may be 1 to N lines per entry but every entry is preceded
by a timestamp that looks like this "2006-12-08 06:36:28".

I have read some docs about StreamTokenizer but that did not help me much.

Any pointer to docs or code is appreciated..

TIA

You could read the log as lines of Strings and use a regular expression to
detect the timestamp. Take a look at String.matches().

FWIW I'm looking at a Postgres log in the pg_log dir. Each entry is on 1 line
but the line starts with the timestamp like yours. This would be real easy to
parse. But your log may be different.

Eric
 
M

Martin Gregorie

Joost said:
Hi,

i want to parse a PostgreSQL log file in Java to analyse it. It contains
entries that may be 1 to N lines per entry but every entry is preceded
by a timestamp that looks like this "2006-12-08 06:36:28".

I have read some docs about StreamTokenizer but that did not help me much.

Any pointer to docs or code is appreciated..
You could easily generate a Java parser with the Coco/R parser generator
which would extract date, time and the multi-line text as separate items.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top