Thread tail -f fuer Apache-Errorlog mit Tk (2 answers)
Opened by Strat at 2004-02-21 12:12

Strat
 2004-02-21 12:12
#41622 #41622
User since
2003-08-04
5246 Artikel
ModeratorIn
[Homepage] [default_avatar]
Habe mal, weil mich das Standard tail-f etwas genervt hat, schnell mal was fuer ein Apache-Errorlog was auf Tk-Basis geschrieben (vielleicht interessiert es ja jemanden)
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! /usr/bin/perl
use warnings;
use strict;

use Tk;
use Tk::ROText;

my $logfile = 'E:\apacheweb\8084_develop.perl-community.de\log\error.log';

my $mw = MainWindow->new();
my $buttonFrame = $mw->Frame()->pack(-side => 'top', -fill => 'x', -expand => 1);
$buttonFrame->Button(-text => 'Quit', -command => sub { $mw->destroy })
->pack(-side => 'right');

my $text = $mw->Scrolled('ROText', -scrollbars => 'e',
-width => 100, -height => 50, -wrap => 'word')
->pack(-fill => 'both', -expand => 1);

$buttonFrame->Button(-text => 'Ans Ende scrollen',
-command => sub { $text->see('end') })
->pack(-side => 'left');

$text->tagConfigure('bold', -font => 'Courier 10 bold');
$text->tagConfigure('red', -background => 'red');
$text->tagConfigure('yellow', -background => 'yellow');

unless (open (LOG, $logfile)) {
die "Error: couldn't read '$logfile': $!\n";
} # unless
&UpdateFromLog();

MainLoop;

close(LOG);

# ------------------------------------------------------------
sub UpdateFromLog {
seek(LOG, 0, 1);
my $found = 0;
while (<LOG>) {
chomp($_);
$found = 1;

# try to parse line
if (my ($date, $mode, $client, $message) =
/^
\[ ([^\]]+) \]
\s+
\[ ([^\]]+) \]
\s+
\[ ([^\]]+) \]
\s+
(.+)
$/x) {
$text->insert('end', "$date\t$mode\t$client\n", 'bold');
$text->insert('end', "$message\n", $mode eq 'error' ? 'red': 'yellow');
} # if
elsif (my ($date3, $mode3, $message3) =
/^
\[ ([^\]]+) \]
\s+
\[ ([^\]]+) \]
\s+
(.+)
$/x) {
$text->insert('end', "$date3\t$mode3\n", 'bold');
$text->insert('end', "$message3\n", $mode3 eq 'error' ? 'red': 'yellow');
} # elsif

elsif (my ($date2, $file, $message2) =
/^
\[ ([^\]]+) \]
\s+
(\S+)
\s+
(.+)
$/x ) {
$text->insert('end', "$date2\t$file\n", 'bold');
$text->insert('end', "$message2\n", 'yellow');
} # elsif

# if not possible, output line
else {
$text->insert('end', "$_\n");
} # else
}
$found and $text->see('end');

$mw->after(1000, \&UpdateFromLog);
} # UpdateFromLog
# ------------------------------------------------------------

ist zwar sehr primitiv (gerade das spalten-scannen), und es gibt da auch bessere Wege (war da nicht was mit FileEvent?), es tut es aber fuer meine Zwecke recht gut...
ich habe es ShowErrorLog.ptk genannt und fuer Windows die Endung .ptk mit wperl.exe (activestate) verknuepft, damit ich nicht dauernd das laestige dos-fenster in der taskleiste habe...
perl -le "s::*erlco'unaty.'.dk':e,y;*kn:ai;penmic;;print"
http://www.fabiani.net/

View full thread tail -f fuer Apache-Errorlog mit Tk