daily pastebin goal
19%
SHARE
TWEET

saku log colorizer.pl

maya000 Sep 22nd, 2017 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env perl
  2. #
  3. # saku-log.pl - saku log colorizer.
  4. #
  5. #usage.
  6. # tail -f logfile | saku-log.pl
  7. #
  8. # grep foobar logfile | saku-log.pl | less
  9. #
  10.  
  11.  
  12. use strict;
  13. use warnings;
  14.  
  15. BEGIN { $| = 1 }  #stdout flush
  16.  
  17. my $GREEN = "\e[36m";
  18. my $RESET = "\e[0m";
  19. my $IP    = "\e[32m";
  20. my $WARN  = "\e[33;1m";
  21. my $ERRNO = "\e[31;1m";
  22. my $INFO  = "\e[35;1m";
  23. my $UA    = "\e[96;1m";
  24. my $BASE  = $GREEN;
  25. my $ip_pattern = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d{0,6})?';
  26.  
  27. while (my $line = <>) {
  28.     chomp $line;
  29.     $line = $BASE . $line . $RESET . "\n";
  30.     $line =~ s|<>|$RESET<>$BASE|g;
  31.     $line =~ s|($ip_pattern)|$RESET$IP$1$BASE|g;
  32.     $line =~ s|(<>\Q${BASE}\Etalk: http://)([^\d].+?)/|$1$RESET$IP$2$RESET$BASE/|;
  33.  
  34.     #info
  35.     $line =~ s|<>\Q${BASE}\E(shingetsu.+(started\|finished))|<>$INFO$1|;
  36.     $line =~ s|(client timeout)|$RESET$INFO$1$RESET|;
  37.     $line =~ s|<>\Q${BASE}\E(direct).+?m<>|<>\e[35m$1$RESET<>|;
  38.  
  39.     #warning
  40.     $line =~ s|Warning: |${WARN}Warning:$RESET |;
  41.     $line =~ s|(: records not found\.)|\e[33m$1$RESET|;
  42.     $line =~ s|(: too large or spam record.)|\e[33m$1$RESET|;
  43.     $line =~ s|(<urlopen error .+)|\e[31m$1|;
  44.     $line =~ s|<>\Q${BASE}\EIOError|<>IOError|;
  45.     $line =~ s|(\[Errno \d+\])|$ERRNO$1$RESET|;
  46.     $line =~ s|(: error)|\e[31m$1$RESET|;
  47.     $line =~ s|<>\Q${BASE}\E(binascii.Error:)( .+)|<>\e[31m$1$RESET$2|;
  48.     $line =~ s|: (HTTP Error \d+:.+)|: $RESET\e[31m$1|;
  49.     $line =~ s|<>\Q${BASE}\E(code \d+.+?)\(|<>\e[33m$1$RESET\(|;
  50.     $line =~ s|<>\Q${BASE}\E(-+)|<>$1|;
  51.     $line =~ s|<>\Q${BASE}\E( *File "/usr/.+/python3\..+)|<>$1|;
  52.    $line =~ s|<>\Q${BASE}\E(Exception\|Traceback\|AttributeError)(.+)|<>$1$2|;
  53.    $line =~ s|^\Q${BASE}\E([^\d]+?.*)|$1|;
  54.    
  55.    #UA
  56.    $line =~ s|<>\Q${BASE}\E(shinGETsu.+?)(\(.+?\)).+?m$|<>\e[35m$1$RESET$2|;
  57.    $line =~ s|(\(.+?\))$|\e[34m$1$RESET|;
  58.    $line =~ s|<>\Q${BASE}\E(Mozilla/5.0 \(.+\|ia_archiver\|Hatena::Fetcher/.+\|Python-urllib/.+\|CCBot/.+\|ltx71.+)|<>$UA$1|;
  59.    
  60.    print $line;
  61. }
  62.  
  63. print "$RESET";
  64.  
  65. __END__
RAW Paste Data
Top