| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Log | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # A simple base class logging module. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andy Wardley <abw@wardley.org> | ||
| 10 | # | ||
| 11 | #======================================================================== | ||
| 12 | |||
| 13 | package Badger::Log; | ||
| 14 | |||
| 15 | use Badger::Class | ||
| 16 | version => 0.01, | ||
| 17 | base => 'Badger::Prototype', | ||
| 18 | import => 'class', | ||
| 19 | utils => 'blessed Now', | ||
| 20 | config => 'system|class:SYSTEM format|class:FORMAT strftime|class:STRFTIME', | ||
| 21 | constants => 'ARRAY CODE', | ||
| 22 | constant => { | ||
| 23 | MSG => '_msg', # suffix for message methods, e.g. warn_msg() | ||
| 24 | LOG => 'log', # method a delegate must implement | ||
| 25 | }, | ||
| 26 | vars => { | ||
| 27 | SYSTEM => 'Badger', | ||
| 28 | FORMAT => '[<time>] [<system>] [<level>] <message>', | ||
| 29 | STRFTIME => '%a %b %d %T %Y', | ||
| 30 | LEVELS => { | ||
| 31 | debug => 0, | ||
| 32 | info => 0, | ||
| 33 | warn => 1, | ||
| 34 | error => 1, | ||
| 35 | fatal => 1, | ||
| 36 | } | ||
| 37 | }, | ||
| 38 | messages => { | ||
| 39 | bad_level => 'Invalid logging level: %s', | ||
| 40 | }; | ||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | class->methods( | ||
| 45 | # Our init method is called init_log() so that we can use Badger::Log as | ||
| 46 | # a mixin or base class without worrying about the init() method clashing | ||
| 47 | # with init() methods from other base classes or mixins. We create an | ||
| 48 | # alias from init() to init_log() so that it also Just Works[tm] as a | ||
| 49 | # stand-alone object | ||
| 50 | init => \&init_log, | ||
| 51 | |||
| 52 | # Now we define two methods for each logging level. The first expects | ||
| 53 | # a pre-formatted output message (e.g. debug(), info(), warn(), etc) | ||
| 54 | # the second additionally wraps around the message() method inherited | ||
| 55 | # from Badger::Base (eg. debug_msg(), info_msg(), warn_msg(), etc) | ||
| 56 | map { | ||
| 57 | my $level = $_; # lexical variable for closure | ||
| 58 | |||
| 59 | $level => sub { | ||
| 60 | my $self = shift; | ||
| 61 | return $self->{ $level } unless @_; | ||
| 62 | $self->log($level, @_) | ||
| 63 | if $self->{ $level }; | ||
| 64 | }, | ||
| 65 | |||
| 66 | ($level.MSG) => sub { | ||
| 67 | my $self = shift; | ||
| 68 | return $self->{ $level } unless @_; | ||
| 69 | $self->log($level, $self->message(@_)) | ||
| 70 | if $self->{ $level }; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | keys %$LEVELS | ||
| 74 | ); | ||
| 75 | |||
| 76 | |||
| 77 | sub init_log { | ||
| 78 | my ($self, $config) = @_; | ||
| 79 | my $class = $self->class; | ||
| 80 | my $levels = $class->hash_vars( LEVELS => $config->{ levels } ); | ||
| 81 | |||
| 82 | # populate $self for each level in $LEVEL using the | ||
| 83 | # value in $config, or the default in $LEVEL | ||
| 84 | while (my ($level, $default) = each %$levels) { | ||
| 85 | $self->{ $level } = | ||
| 86 | defined $config->{ $level } | ||
| 87 | ? $config->{ $level } | ||
| 88 | : $levels->{ $level }; | ||
| 89 | } | ||
| 90 | |||
| 91 | # call the auto-generated configure() method to update $self from $config | ||
| 92 | $self->configure($config); | ||
| 93 | |||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub log { | ||
| 98 | my $self = shift; | ||
| 99 | my $level = shift; | ||
| 100 | my $action = $self->{ $level }; | ||
| 101 | my $message = join('', @_); | ||
| 102 | my $method; | ||
| 103 | |||
| 104 | return $self->_fatal_msg( bad_level => $level ) | ||
| 105 | unless defined $action; | ||
| 106 | |||
| 107 | # depending on what the $action is set to, we add the message to | ||
| 108 | # an array, call a code reference, delegate to another log object, | ||
| 109 | # print or ignore the mesage | ||
| 110 | |||
| 111 | if (ref $action eq ARRAY) { | ||
| 112 | push(@$action, $message); | ||
| 113 | } | ||
| 114 | elsif (ref $action eq CODE) { | ||
| 115 | &$action($level, $message); | ||
| 116 | } | ||
| 117 | elsif (blessed $action && ($method = $action->can(LOG))) { | ||
| 118 | $method->($action, $level, $message); | ||
| 119 | } | ||
| 120 | elsif ($action) { | ||
| 121 | warn $self->format($level, $message), "\n"; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | sub format { | ||
| 126 | my $self = shift; | ||
| 127 | my $args = { | ||
| 128 | time => Now->format($self->{ strftime }), | ||
| 129 | system => $self->{ system }, | ||
| 130 | level => shift, | ||
| 131 | message => shift, | ||
| 132 | }; | ||
| 133 | my $format = $self->{ format }; | ||
| 134 | $format =~ | ||
| 135 | s/<(\w+)>/ | ||
| 136 | defined $args->{ $1 } | ||
| 137 | ? $args->{ $1 } | ||
| 138 | : "<$1>" | ||
| 139 | /eg; | ||
| 140 | return $format; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub level { | ||
| 144 | my $self = shift; | ||
| 145 | my $level = shift; | ||
| 146 | return $self->_fatal_msg( bad_level => $level ) | ||
| 147 | unless exists $LEVELS->{ $level }; | ||
| 148 | return @_ ? ($self->{ $level } = shift) : $self->{ $level }; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub enable { | ||
| 152 | my $self = shift; | ||
| 153 | $self->level($_ => 1) for @_; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub disable { | ||
| 157 | my $self = shift; | ||
| 158 | $self->level($_ => 0) for @_; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _error_msg { | ||
| 162 | my $self = shift; | ||
| 163 | $self->Badger::Base::error( | ||
| 164 | $self->Badger::Base::message(@_) | ||
| 165 | ); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub _fatal_msg { | ||
| 169 | my $self = shift; | ||
| 170 | $self->Badger::Base::fatal( | ||
| 171 | $self->Badger::Base::message(@_) | ||
| 172 | ); | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | 1; | ||
| 177 | |||
| 178 | __END__ |