summaryrefslogtreecommitdiff
path: root/t/012Deeper.t
blob: a3a95573b81147df978daaad063cc0fe2ffa8cc9 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'


BEGIN { 
    if($ENV{INTERNAL_DEBUG}) {
        require Log::Log4perl::InternalDebug;
        Log::Log4perl::InternalDebug->enable();
    }
}

use Log::Log4perl;
use Test::More;
use File::Spec;

our $LOG_DISPATCH_PRESENT = 0;

BEGIN { 
    eval { require Log::Dispatch; };
    if($@) {
       plan skip_all => "only with Log::Dispatch";
    } else {
       $LOG_DISPATCH_PRESENT = 1;
       plan tests => 3;
    }
};

my $WORK_DIR = "tmp";
if(-d "t") {
    $WORK_DIR = File::Spec->catfile(qw(t tmp));
}
unless (-e "$WORK_DIR"){
    mkdir("$WORK_DIR", 0755) || die "can't create $WORK_DIR ($!)";
}

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $today = sprintf("%4.4d%2.2d%2.2d",$year+1900, $mon+1, $mday);
use vars qw($logfile1 $logfile6 $logfile7);
$logfile1 = File::Spec->catfile(qw(t tmp deeper1.log));
$logfile6 = File::Spec->catfile(qw(t tmp deeper6.log));
$logfile7 = File::Spec->catfile(qw(t tmp deeper7.log)); 
our @outfiles = ($logfile1, $logfile6, $logfile7);

foreach my $f (@outfiles){
    unlink $f if (-e $f);
}


my $config = <<EOL;
#specify LOGLEVEL, appender1, appender2, ...
log4j.category.plant     = INFO,  FileAppndr1
log4j.category.animal        = INFO,  FileAppndr1
log4j.category.animal.dog = DEBUG, FileAppndr1

log4j.oneMessagePerAppender = 1


# ---------------------------------------------
# FileAppndr1
log4j.appender.FileAppndr1        = org.apache.log4j.FileAppender
log4j.appender.FileAppndr1.File   = $logfile1

log4j.appender.FileAppndr1.layout = org.apache.log4j.PatternLayout
log4j.appender.FileAppndr1.layout.ConversionPattern=%d %4r [%t] %-5p %c %t - %m%n


# ---------------------------------------------------
#2nd set of tests,inheritance
log4j.category.a       = INFO, l2
log4j.category.a.b.c.d = WARN, l2

log4j.appender.l2        = org.apache.log4j.FileAppender
log4j.appender.l2.File   = $logfile6
log4j.appender.l2.layout = org.apache.log4j.PatternLayout
log4j.appender.l2.layout.ConversionPattern=%d %4r [%t] %-5p %c - %m%n


# --------------------------------------
#inheritance the other way
log4j.category.xa       = WARN, l3
log4j.category.xa.b.c.d = INFO, l3

log4j.appender.l3       = org.apache.log4j.FileAppender
log4j.appender.l3.File  = $logfile7
log4j.appender.l3.layout= org.apache.log4j.PatternLayout
log4j.appender.l3.layout.ConversionPattern=%d %4r 666  [%t] %-5p  %c - %m%n

EOL


Log::Log4perl->init(\$config);


# -----------------------------------------------------
# (1) shotgun test
#set to INFO

my $logger = Log::Log4perl->get_logger('plant');

#set to INFO
$logger->debug("debugging message 1 ");
$logger->info("info message 1 ");      
$logger->warn("warning message 1 ");   
$logger->fatal("fatal message 1 ");   

#set to DEBUG
my $doglogger = Log::Log4perl->get_logger('animal.dog');
$doglogger->debug("debugging message 2 ");
$doglogger->info("info message 2 ");
$doglogger->warn("warning message 2 ");
$doglogger->fatal("fatal message 2 ");

#set to INFO
my $animallogger = Log::Log4perl->get_logger('animal');
$animallogger->debug("debugging message 3 ");
$animallogger->info("info message 3 ");
$animallogger->warn("warning message 3 ");
$animallogger->fatal("fatal message 3 ");

#should default to animal::dog
my $deeptreelogger = Log::Log4perl->get_logger('animal.dog.leg.toenail');
$deeptreelogger->debug("debug message");
$animallogger->info("info message");
$deeptreelogger->warn("warning message");
$animallogger->fatal("fatal message");

my ($result, $expected);

{local $/ = undef;
 open (F, File::Spec->catfile(qw(t deeper1.expected))) || die $!;
 $expected = <F>;
 open (F, $logfile1) || die $!;
 $result = <F>;
 close F;
 $result =~ s/.+?] //g;
}

is ($result, $expected);


# ------------------------------------
# (6)   test inheritance
#a=INFO, a.b.c.d=WARN, a.b and a.b.c are undefined
my $la = Log::Log4perl->get_logger('a');
my $lab = Log::Log4perl->get_logger('a.b');
my $labc = Log::Log4perl->get_logger('a.b.c');
my $labcd = Log::Log4perl->get_logger('a.b.c.d');
my $labcde = Log::Log4perl->get_logger('a.b.c.d.e');

foreach my $l ($la, $lab, $labc, $labcd, $labcde){
   $l->debug("should not print");
}
foreach my $l ($la, $lab, $labc, $labcd, $labcde){
   $l->info("should print for a, a.b, a.b.c");
}
foreach my $l ($la, $lab, $labc, $labcd, $labcde){
   $l->warn("should print for a, a.b, a.b.c, a.b.c.d, a.b.c.d.e");
}
foreach my $l ($la, $lab, $labc, $labcd, $labcde){
   $l->fatal("should print for a, a.b, a.b.c, a.b.c.d, a.b.c.d.e");
}
{local $/ = undef;
 open (F, File::Spec->catfile(qw(t deeper6.expected)));
 $expected = <F>;
 open (F, $logfile6);
 $result = <F>;
 close F;
 $result =~ s/.+?] //g;
}

is($result, $expected);


# ------------------------------------
# (7)   test inheritance the other way
#xa=WARN, xa.b.c.d=INFO, xa.b and xa.b.c are undefined
my $xla = Log::Log4perl->get_logger('xa');
my $xlab = Log::Log4perl->get_logger('xa.b');
my $xlabc = Log::Log4perl->get_logger('xa.b.c');
my $xlabcd = Log::Log4perl->get_logger('xa.b.c.d');
my $xlabcde = Log::Log4perl->get_logger('xa.b.c.d.e');

foreach my $l ($xla, $xlab, $xlabc, $xlabcd, $xlabcde){
   $l->debug("should not print");
}
foreach my $l ($xla, $xlab, $xlabc, $xlabcd, $xlabcde){
   $l->info("should print for xa.b.c.d, xa.b.c.d.e");
}
foreach my $l ($xla, $xlab, $xlabc, $xlabcd, $xlabcde){
   $l->warn("should print for xa, xa.b, xa.b.c, xa.b.c.d, xa.b.c.d.e");
}
foreach my $l ($xla, $xlab, $xlabc, $xlabcd, $xlabcde){
   $l->fatal("should print for xa, xa.b, xa.b.c, xa.b.c.d, xa.b.c.d.e");
}
{local $/ = undef;
 open (F, File::Spec->catfile(qw(t deeper7.expected)));
 $expected = <F>;
 open (F, $logfile7);
 $result = <F>;
 close F;
 $result =~ s/.+?] //g;
}

is($result, $expected);


   
END{   
    foreach my $f (@outfiles){
        unlink $f if (-e $f);
    }
}