]> granicus.if.org Git - pgbadger/commitdiff
Allow log parsing without any log_line_prefix and extend it to be defined by the...
authorDarold <gilles@darold.net>
Sun, 15 Apr 2012 20:16:55 +0000 (22:16 +0200)
committerDarold <gilles@darold.net>
Sun, 15 Apr 2012 20:16:55 +0000 (22:16 +0200)
--regex-db and --regex-user. For exemple the default regex of pgbadger to parse user and db name from log_line_prefix can be written like this: pgbadger -l mylogfile.log  --regex-user="user=([^,]*)," --regex-db="db=([^,]*)"

pgbadger

index b4946ef63f37621b89fff5fe73ef1cbc0fc7d852..e930c97465428fee0920605df68765dabbc23617 100755 (executable)
--- a/pgbadger
+++ b/pgbadger
@@ -50,6 +50,8 @@ my $debug = 1;
 my $nohighlight = 0;
 my $from = '';
 my $to = '';
+my $regex_prefix_dbname = '';
+my $regex_prefix_dbuser = '';
 
 my $t0 = Benchmark->new;
 
@@ -71,6 +73,8 @@ my $result = GetOptions (
        "n|nohighlight!"=> \$nohighlight,
        "b|begin=s"     => \$from,
        "e|end=s"       => \$to,
+       "regex-db=s"    => \$regex_prefix_dbname,
+       "regex-user=s"  => \$regex_prefix_dbuser,
 );
 
 if ($ver) {
@@ -1481,26 +1485,41 @@ sub parse_query
        # Extract user and database information from the logprefix part
        my $t_dbname = '';
        my $t_dbuser = '';
-       if ($t_logprefix =~ /^(user|usr)=([^,]*)/) {
-               $t_dbuser = $1;
-       }
-       if ($t_logprefix =~ /^(dbname|db|database)=([^,]*)/) {
-               $t_dbname = $1;
-       }
+       $t_logprefix =~ s/\s+$//;
+       if ($t_logprefix) {
+               # User defined regex
+               if ($regex_prefix_dbuser) {
+                       if ($t_logprefix =~ /$regex_prefix_dbuser/) {
+                               $t_dbuser = $1;
+                       }
+               # Default regex
+               } elsif ($t_logprefix =~ /^user=([^,]*)/) {
+                       $t_dbuser = $1;
+               }
+               # User defined regex
+               if ($regex_prefix_dbname) {
+                       if ($t_logprefix =~ /$regex_prefix_dbname/) {
+                               $t_dbname = $1;
+                       }
+               # Default regex
+               } elsif ($t_logprefix =~ /^db=([^,]*)/) {
+                       $t_dbname = $1;
+               }
 
-       # Check user and/or database if require
-       if ($dbname) {
-               # Log line do not match the required dbname
-               if (!$cur_info{dbname} || ($dbname ne $cur_info{dbname})) {
-                       %cur_info = ();
-                       return;
+               # Check user and/or database if require
+               if ($dbname) {
+                       # Log line do not match the required dbname
+                       if (!$cur_info{dbname} || ($dbname ne $cur_info{dbname})) {
+                               %cur_info = ();
+                               return;
+                       }
                }
-       }
-       if ($dbuser) {
-               # Log line do not match the required dbuser
-               if (!$cur_info{dbuser} || ($dbuser ne $cur_info{dbuser})) {
-                       %cur_info = ();
-                       return;
+               if ($dbuser) {
+                       # Log line do not match the required dbuser
+                       if (!$cur_info{dbuser} || ($dbuser ne $cur_info{dbuser})) {
+                               %cur_info = ();
+                               return;
+                       }
                }
        }