1########################################### 2# Test Suite for Log::Log4perl 3# Mike Schilli, 2002 (m@perlmeister.com) 4########################################### 5 6BEGIN { 7 if($ENV{INTERNAL_DEBUG}) { 8 require Log::Log4perl::InternalDebug; 9 Log::Log4perl::InternalDebug->enable(); 10 } 11} 12 13use warnings; 14use strict; 15 16######################### 17# change 'tests => 1' to 'tests => last_test_to_print'; 18######################### 19use Test::More; 20BEGIN { plan tests => 24 }; 21 22use Log::Log4perl; 23use Log::Log4perl::Layout; 24 25use Log::Log4perl::Level; 26use Log::Log4perl::Appender::TestBuffer; 27use File::Spec; 28 29my $app = Log::Log4perl::Appender->new( 30 "Log::Log4perl::Appender::TestBuffer"); 31 32ok(1); # If we made it this far, we/re ok. 33 34my $logger = Log::Log4perl->get_logger("abc.def.ghi"); 35$logger->level($DEBUG); 36$logger->add_appender($app); 37my $layout = Log::Log4perl::Layout::PatternLayout->new( 38 "bugo %% %c{2} %-17F{2} %L hugo"); 39$app->layout($layout); 40my $line = __LINE__ + 1; 41$logger->debug("That's the message"); 42 43is($app->buffer(), "bugo % def.ghi " . 44 File::Spec->catfile(qw(t 003Layout.t)) . 45 " $line hugo"); 46 47############################################################ 48# Log the message 49############################################################ 50$app->buffer(""); 51$layout = Log::Log4perl::Layout::PatternLayout->new( 52 "The message is here: %m"); 53$app->layout($layout); 54$logger->debug("That's the message"); 55 56is($app->buffer(), "The message is here: That's the message"); 57 58############################################################ 59# Log the time 60############################################################ 61$app->buffer(""); 62$layout = Log::Log4perl::Layout::PatternLayout->new("[%r] %m"); 63$app->layout($layout); 64$logger->debug("That's the message"); 65 66like($app->buffer(), qr/^\[\d+\] That's the message$/); 67 68############################################################ 69# Log the date/time 70############################################################ 71$app->buffer(""); 72$layout = Log::Log4perl::Layout::PatternLayout->new("%d> %m"); 73$app->layout($layout); 74$logger->debug("That's the message"); 75 76like($app->buffer(), 77 qr#^\d{4}/\d\d/\d\d \d\d:\d\d:\d\d> That\'s the message$#); 78 79############################################################ 80# Log the date/time with own timer function 81############################################################ 82sub mytimer1 { 83 # 2 days after 1/1/1970 to compensate for time zones 84 return 180000; 85} 86 87$app->buffer(""); 88$layout = Log::Log4perl::Layout::PatternLayout->new( 89 { time_function => \&mytimer1 }, "%d{MM/yyyy}> %m"); 90$app->layout($layout); 91$logger->debug("That's the message"); 92like($app->buffer(), qr{01/1970}); 93 94 # epoch format 95$app->buffer(""); 96$layout = Log::Log4perl::Layout::PatternLayout->new( 97 { time_function => \&mytimer1 }, "%d{e}> %m"); 98$app->layout($layout); 99$logger->debug("That's the message"); 100like($app->buffer(), qr/^180000/); 101 102############################################################ 103# Check SimpleLayout 104############################################################ 105$app->buffer(""); 106$layout = Log::Log4perl::Layout::SimpleLayout->new(); 107$app->layout($layout); 108$logger->debug("That's the message"); 109 110is($app->buffer(), "DEBUG - That\'s the message\n"); 111 112############################################################ 113# Check depth level of %M - with debug(...) 114############################################################ 115 116sub mysubroutine { 117 $app->buffer(""); 118 $layout = Log::Log4perl::Layout::PatternLayout->new("%M: %m"); 119 $app->layout($layout); 120 $logger->debug("That's the message"); 121} 122 123mysubroutine(); 124is($app->buffer(), 'main::mysubroutine: That\'s the message'); 125 126############################################################ 127# Check depth level of %M - with debug(...) 128############################################################ 129 130$app->buffer(""); 131$layout = Log::Log4perl::Layout::PatternLayout->new("%M: %m"); 132$app->layout($layout); 133$logger->debug("That's the message"); 134 135is($app->buffer(), 'main::: That\'s the message'); 136 137############################################################ 138# Check Filename and Line # 139############################################################ 140$app->buffer(""); 141$layout = Log::Log4perl::Layout::PatternLayout->new("%F-%L %m"); 142$app->layout($layout); 143$line = __LINE__ + 1; 144$logger->debug("That's the message"); 145 146like($app->buffer(), qr/003Layout.t-$line That's the message/); 147 148############################################################ 149# Don't append a newline if the message already contains one 150############################################################ 151$app->buffer(""); 152$layout = Log::Log4perl::Layout::PatternLayout->new("%m%n"); 153$app->layout($layout); 154$logger->debug("That's the message\n"); 155 156is($app->buffer(), "That\'s the message\n"); 157 158############################################################ 159# But don't suppress other %ns 160############################################################ 161$app->buffer(""); 162$layout = Log::Log4perl::Layout::PatternLayout->new("a%nb%n%m%n"); 163$app->layout($layout); 164$logger->debug("That's the message\n"); 165 166is($app->buffer(), "a\nb\nThat\'s the message\n"); 167 168############################################################ 169# Test if the process ID works 170############################################################ 171$app->buffer(""); 172$layout = Log::Log4perl::Layout::PatternLayout->new("%P:%m"); 173$app->layout($layout); 174$logger->debug("That's the message\n"); 175 176like($app->buffer(), qr/^\d+:That's the message$/); 177 178############################################################ 179# Test if the hostname placeholder %H works 180############################################################ 181$app->buffer(""); 182$layout = Log::Log4perl::Layout::PatternLayout->new("%H:%m"); 183$app->layout($layout); 184$logger->debug("That's the message\n"); 185 186like($app->buffer(), qr/^[^:]+:That's the message$/); 187 188############################################################ 189# Test max width in the format specifiers 190############################################################ 191#min width 192$app->buffer(""); 193$layout = Log::Log4perl::Layout::PatternLayout->new("%5.5m"); 194$app->layout($layout); 195$logger->debug("123"); 196is($app->buffer(), ' 123'); 197 198#max width 199$app->buffer(""); 200$logger->debug("1234567"); 201is($app->buffer(), '12345'); 202 203#left justify 204$app->buffer(""); 205$layout = Log::Log4perl::Layout::PatternLayout->new("%-5.5m"); 206$app->layout($layout); 207$logger->debug("123"); 208is($app->buffer(), '123 '); 209 210############################################################ 211# Check depth level of %M - with eval {...} 212############################################################ 213 214$app->buffer(""); 215$layout = Log::Log4perl::Layout::PatternLayout->new("%M: %m"); 216$app->layout($layout); 217sub foo { 218 eval { 219 $logger->debug("Thats the message"); 220 }; 221} 222foo(); 223is($app->buffer(), 'main::foo: Thats the message'); 224 225############################################################ 226# Check two levels of %M - with eval {...} 227############################################################ 228 229$app->buffer(""); 230$layout = Log::Log4perl::Layout::PatternLayout->new("%M: %m"); 231$app->layout($layout); 232sub foo2 { 233 eval { 234 eval { 235 $logger->debug("Thats the message"); 236 }; 237 }; 238} 239foo2(); 240is($app->buffer(), 'main::foo2: Thats the message'); 241 242############################################################ 243# Check depth level of %M - with eval {...} 244############################################################ 245 246$app->buffer(""); 247$layout = Log::Log4perl::Layout::PatternLayout->new("%M: %m"); 248$app->layout($layout); 249eval { 250 $logger->debug("Thats the message"); 251}; 252is($app->buffer(), 'main::: Thats the message'); 253 254############################################################ 255# Non-portable line breaks 256############################################################ 257 258$app->buffer(""); 259$layout = Log::Log4perl::Layout::PatternLayout->new("%m\\n"); 260$app->layout($layout); 261eval { 262 $logger->debug("Thats the message"); 263}; 264is($app->buffer(), "Thats the message\n"); 265 266$app->buffer(""); 267$layout = Log::Log4perl::Layout::PatternLayout->new("%m\\r\\n"); 268$app->layout($layout); 269eval { 270 $logger->debug("Thats the message"); 271}; 272is($app->buffer(), "Thats the message\r\n"); 273 274############################################################ 275# Render a multiline message 276############################################################ 277 278$app->buffer(""); 279$layout = Log::Log4perl::Layout::PatternLayout::Multiline->new("%M: %m%n"); 280$app->layout($layout); 281eval { 282 $logger->debug("Thats the\nmultiline\nmessage"); 283}; 284is($app->buffer(), "main::: Thats the\nmain::: multiline\nmain::: message\n"); 285 286