| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Logger::LoggerBase; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Logger::LoggerBase - logger base class | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | package My::Logger; | ||
| 14 | |||
| 15 | use base qw(Wombat::Logger::LoggerBase); | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | Convenience base class for logger implementations. The only method | ||
| 20 | that must be implemented is C<write()>, plus any accessor methods | ||
| 21 | required for configuration, and C<start()> and C<stop()> if resources | ||
| 22 | must be initialized and cleaned up. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | use fields qw(container level started); | ||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | |||
| 30 | use constant FATAL => 0; | ||
| 31 | use constant ERROR => 1; | ||
| 32 | use constant WARNING => 2; | ||
| 33 | use constant INFO => 3; | ||
| 34 | use constant DEBUG => 4; | ||
| 35 | |||
| 36 | use constant LEVELS => { | ||
| 37 | FATAL => FATAL, | ||
| 38 | ERROR => ERROR, | ||
| 39 | WARNING => WARNING, | ||
| 40 | INFO => INFO, | ||
| 41 | DEBUG => DEBUG, | ||
| 42 | }; | ||
| 43 | |||
| 44 | use Servlet::Util::Exception (); | ||
| 45 | |||
| 46 | =pod | ||
| 47 | |||
| 48 | =head1 CONSTRUCTOR | ||
| 49 | |||
| 50 | =over | ||
| 51 | |||
| 52 | =item new() | ||
| 53 | |||
| 54 | Construct and return a B<Wombat::Logger::LoggerBase> instance, | ||
| 55 | initializing fields appropriately. If subclasses override the | ||
| 56 | constructor, they must be sure to call | ||
| 57 | |||
| 58 | $self->SUPER::new(); | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub new { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | $self = fields::new($self) unless ref $self; | ||
| 68 | |||
| 69 | $self->{container} = undef; | ||
| 70 | $self->{level} = ERROR; | ||
| 71 | $self->{started} = undef; | ||
| 72 | |||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | =pod | ||
| 77 | |||
| 78 | =head1 ACCESSOR METHODS | ||
| 79 | |||
| 80 | =over | ||
| 81 | |||
| 82 | =item getContainer() | ||
| 83 | |||
| 84 | Return the Container with which this Logger is associated. | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub getContainer { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | return $self->{container}; | ||
| 92 | } | ||
| 93 | |||
| 94 | =pod | ||
| 95 | |||
| 96 | =item setContainer($container) | ||
| 97 | |||
| 98 | Set the Container with which this Logger is associated. | ||
| 99 | |||
| 100 | B<Parameters:> | ||
| 101 | |||
| 102 | =over | ||
| 103 | |||
| 104 | =item $container | ||
| 105 | |||
| 106 | the Container with which this Logger is associated | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub setContainer { | ||
| 113 | my $self = shift; | ||
| 114 | my $container = shift; | ||
| 115 | |||
| 116 | $self->{container} = $container; | ||
| 117 | |||
| 118 | return 1; | ||
| 119 | } | ||
| 120 | |||
| 121 | =pod | ||
| 122 | |||
| 123 | =item getLevel() | ||
| 124 | |||
| 125 | Return the verbosity level of this Logger. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub getLevel { | ||
| 130 | my $self = shift; | ||
| 131 | |||
| 132 | return $self->{level}; | ||
| 133 | } | ||
| 134 | |||
| 135 | =pod | ||
| 136 | |||
| 137 | =item setLevel($level) | ||
| 138 | |||
| 139 | Set the verbosity level of this Logger. Messages logged with a higher | ||
| 140 | verbosity than this will be silently ignored. | ||
| 141 | |||
| 142 | Allowable values in increasing order are: | ||
| 143 | |||
| 144 | =over | ||
| 145 | |||
| 146 | =item 'FATAL' | ||
| 147 | |||
| 148 | =item 'ERROR' | ||
| 149 | |||
| 150 | =item 'WARNING' | ||
| 151 | |||
| 152 | =item 'INFO' | ||
| 153 | |||
| 154 | =item 'DEBUG' | ||
| 155 | |||
| 156 | =back | ||
| 157 | |||
| 158 | The default level is 'ERROR'. | ||
| 159 | |||
| 160 | B<Parameters:> | ||
| 161 | |||
| 162 | =over | ||
| 163 | |||
| 164 | =item $level | ||
| 165 | |||
| 166 | the verbosity level, as a string | ||
| 167 | |||
| 168 | =back | ||
| 169 | |||
| 170 | B<Throws:> | ||
| 171 | |||
| 172 | =over | ||
| 173 | |||
| 174 | =item B<Servlet::Util::IllegalArgumentException> | ||
| 175 | |||
| 176 | if the specified log level is not one of the allowed values | ||
| 177 | |||
| 178 | =back | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | sub setLevel { | ||
| 183 | my $self = shift; | ||
| 184 | my $level = shift; | ||
| 185 | |||
| 186 | my $match = uc $level; | ||
| 187 | if (exists LEVELS->{$level}) { | ||
| 188 | $self->{level} = LEVELS->{$level}; | ||
| 189 | } else { | ||
| 190 | my $msg = "unsupported log level [$level]"; | ||
| 191 | Servlet::Util::IllegalArgumentException->throw($msg); | ||
| 192 | } | ||
| 193 | |||
| 194 | return 1; | ||
| 195 | } | ||
| 196 | |||
| 197 | =pod | ||
| 198 | |||
| 199 | =back | ||
| 200 | |||
| 201 | =head1 PUBLIC METHODS | ||
| 202 | |||
| 203 | =over | ||
| 204 | |||
| 205 | =item log($message, $exception, $level) | ||
| 206 | |||
| 207 | Send a message and/or an exception to the log destination. If a level | ||
| 208 | is specified, the Logger must be set to a verbosity level greater than | ||
| 209 | or equal to the specified level. If a level is not specified, the | ||
| 210 | message and/or exception will be logged unconditionally. | ||
| 211 | |||
| 212 | B<Parameters:> | ||
| 213 | |||
| 214 | =over | ||
| 215 | |||
| 216 | =item $message | ||
| 217 | |||
| 218 | an optional string message to log | ||
| 219 | |||
| 220 | =item $exception | ||
| 221 | |||
| 222 | an optional exception to log in stack trace form | ||
| 223 | |||
| 224 | =item $level | ||
| 225 | |||
| 226 | an optional log level | ||
| 227 | |||
| 228 | =back | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | sub log { | ||
| 233 | my $self = shift; | ||
| 234 | my $msg = shift; | ||
| 235 | my $e = shift; | ||
| 236 | my $level = shift; | ||
| 237 | |||
| 238 | if (!defined $level || | ||
| 239 | $self->{level} >= LEVELS->{$level}) { | ||
| 240 | $self->write("$msg\n") if $msg; | ||
| 241 | |||
| 242 | if ($e) { | ||
| 243 | $self->write($e); | ||
| 244 | |||
| 245 | if ($e->isa('Servlet::ServletException')) { | ||
| 246 | my $root = $e->getRootCause(); | ||
| 247 | if ($root) { | ||
| 248 | $self->write("----- Root Cause -----\n"); | ||
| 249 | $self->write($root); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | return 1; | ||
| 256 | } | ||
| 257 | |||
| 258 | =pod | ||
| 259 | |||
| 260 | =item write($string) | ||
| 261 | |||
| 262 | Write the specified string to the log destination. The default | ||
| 263 | implementation does nothing. Subclasses must override this method. | ||
| 264 | |||
| 265 | B<Parameters:> | ||
| 266 | |||
| 267 | =over | ||
| 268 | |||
| 269 | =item $string | ||
| 270 | |||
| 271 | the string to write to the log destination | ||
| 272 | |||
| 273 | =back | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub write {} | ||
| 278 | |||
| 279 | =pod | ||
| 280 | |||
| 281 | =back | ||
| 282 | |||
| 283 | =head1 LIFECYCLE METHODS | ||
| 284 | |||
| 285 | =over | ||
| 286 | |||
| 287 | =item start() | ||
| 288 | |||
| 289 | Prepare for the beginning of active use of this Logger. This method | ||
| 290 | must be called before any of the public methods of the component are | ||
| 291 | utilized. Subclasses should initialize logging resources with this | ||
| 292 | method. | ||
| 293 | |||
| 294 | B<Throws:> | ||
| 295 | |||
| 296 | =over | ||
| 297 | |||
| 298 | =item B<Wombat::LifecycleException> | ||
| 299 | |||
| 300 | if the Logger cannot be started | ||
| 301 | |||
| 302 | =back | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | sub start { | ||
| 307 | my $self = shift; | ||
| 308 | |||
| 309 | if ($self->{started}) { | ||
| 310 | throw Wombat::LifecycleException->new("logger already started"); | ||
| 311 | } | ||
| 312 | |||
| 313 | $self->{started} = 1; | ||
| 314 | |||
| 315 | return 1; | ||
| 316 | } | ||
| 317 | |||
| 318 | =pod | ||
| 319 | |||
| 320 | =item stop() | ||
| 321 | |||
| 322 | Gracefully terminate the active use of this Logger. This method must | ||
| 323 | be the last called on a given instance of this component. Subclasses | ||
| 324 | should release logging resources with this method. | ||
| 325 | |||
| 326 | B<Throws:> | ||
| 327 | |||
| 328 | =over | ||
| 329 | |||
| 330 | =item B<Wombat::LifecycleException> | ||
| 331 | |||
| 332 | if the Logger cannot be stopped | ||
| 333 | |||
| 334 | =back | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | sub stop { | ||
| 339 | my $self = shift; | ||
| 340 | |||
| 341 | unless ($self->{started}) { | ||
| 342 | throw Wombat::LifecycleException->new("logger not started"); | ||
| 343 | } | ||
| 344 | |||
| 345 | return 1; | ||
| 346 | } | ||
| 347 | |||
| 348 | 1; | ||
| 349 | __END__ |