File: 015-handler-filter-message.t

package info (click to toggle)
liblog-handler-perl 0.45-1%2Blenny1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 392 kB
  • ctags: 145
  • sloc: perl: 2,017; makefile: 39
file content (84 lines) | stat: -rw-r--r-- 1,592 bytes parent folder | download
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
use strict;
use warnings;
use Test::More tests => 8;
use Log::Handler;

my %STRING = (
    'string 1' => 0,
    'string 2' => 0,
    'string 3' => 0,
    'string 4' => 0,
    'string 5' => 0,
);

ok(1, 'use');

my $log = Log::Handler->new();

ok(2, 'new');

$log->add(
    forward => {
        forward_to => \&check,
        maxlevel   => 6,
        filter_message => 'string 1$',
    }
);

$log->add(
    forward => {
        forward_to => \&check,
        maxlevel   => 6,
        filter_message => qr/STRING\s2$/i,
    }
);

$log->add(
    forward => {
        forward_to => \&check,
        maxlevel   => 6,
        filter_message => sub { shift->{message} =~ /string\s3$/ },
    }
);

$log->add(
    forward => {
        forward_to => \&check,
        maxlevel   => 6,
        filter_message => {
            match1    => 'foo',
            match2    => qr/bar/,
            match3    => '(?:string\s4|string\s5)',
            condition => '(!match1 && !match2) && match3',
        }
    }
);

ok(3, 'add');

sub check {
    my $m = shift;
    if ($m->{message} =~ /(string\s\d+)/) {
        if (exists $STRING{$1}) {
            $STRING{$1}++;
        } else {
            die "unexpected message $m->{message}";
        }
    }
}

$log->info('string 1');
$log->info('string 2');
$log->info('string 3');
$log->info('string 4');
$log->info('string 5');

$log->info('string 1 foo');
$log->info('string 2 foo');
$log->info('string 3 foo');
$log->info('string 4 foo');
$log->info('string 5 bar');

while ( my ($k, $v) = each %STRING ) {
    ok($v == 1, "checking if $k match (hits:$v)");
}