| 1 | =pod | ||
| 2 | |||
| 3 | =begin classdoc | ||
| 4 | |||
| 5 | |||
| 6 | Abstract base logger class for component classes. | ||
| 7 | Provides an interface to the Event and Web Logger components which | ||
| 8 | responds to the current LogLevel of the component. | ||
| 9 | If the EventLogger component is not defined, then messages | ||
| 10 | are logged to STDERR. If the WebLogger component is not defined, | ||
| 11 | the messages are silently discarded. | ||
| 12 | <p> | ||
| 13 | Copyright© 2006-2008, Dean Arnold, Presicient Corp., USA<br> | ||
| 14 | All rights reserved. | ||
| 15 | <p> | ||
| 16 | Licensed under the Academic Free License version 3.0, as specified at | ||
| 17 | <a href='http://www.opensource.org/licenses/afl-3.0.php'>OpenSource.org</a>. | ||
| 18 | |||
| 19 | @author D. Arnold | ||
| 20 | @since 2006-08-21 | ||
| 21 | @self $self | ||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | =end classdoc | ||
| 26 | |||
| 27 | =cut | ||
| 28 | package HTTP::Daemon::Threaded::Logable; | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | |||
| 33 | our $VERSION = '0.91'; | ||
| 34 | =pod | ||
| 35 | |||
| 36 | =begin classdoc | ||
| 37 | |||
| 38 | Log an error message. Prefix the message with the | ||
| 39 | error tag and the type of the object reporting the error | ||
| 40 | |||
| 41 | @param $msg error message text | ||
| 42 | |||
| 43 | @return undef | ||
| 44 | |||
| 45 | |||
| 46 | =end classdoc | ||
| 47 | |||
| 48 | =cut | ||
| 49 | sub logError { | ||
| 50 | my ($self, $msg) = @_; | ||
| 51 | |||
| 52 | $self->{EventLogger}->log(join('', '[ERROR]: [', ref $self, '] ', $msg, "\n")), | ||
| 53 | return undef | ||
| 54 | if $self->{EventLogger}; | ||
| 55 | |||
| 56 | print STDERR join('', '[ERROR]: [', ref $self, '] ', $msg), "\n"; | ||
| 57 | return undef; | ||
| 58 | } | ||
| 59 | =pod | ||
| 60 | |||
| 61 | =begin classdoc | ||
| 62 | |||
| 63 | Log a warning message. Prefix the message with the | ||
| 64 | warning tag and the type of the object reporting the error. | ||
| 65 | Warnings are only logged when the object's loglevel > 1 | ||
| 66 | |||
| 67 | @param $msg warning message text | ||
| 68 | |||
| 69 | @return this object | ||
| 70 | |||
| 71 | |||
| 72 | =end classdoc | ||
| 73 | |||
| 74 | =cut | ||
| 75 | sub logWarning { | ||
| 76 | my ($self, $msg) = @_; | ||
| 77 | |||
| 78 | return $self | ||
| 79 | unless ($self->{LogLevel} > 1); | ||
| 80 | |||
| 81 | $self->{EventLogger}->log(join('', '[WARNING]: [', ref $self, '] ', $msg, "\n")), | ||
| 82 | return $self | ||
| 83 | if $self->{EventLogger}; | ||
| 84 | |||
| 85 | print STDERR join('', '[WARNING]: [', ref $self, '] ', $msg), "\n"; | ||
| 86 | return $self; | ||
| 87 | } | ||
| 88 | =pod | ||
| 89 | |||
| 90 | =begin classdoc | ||
| 91 | |||
| 92 | Log an informational message. Prefix the message with the | ||
| 93 | information tag and the type of the object reporting the error. | ||
| 94 | Informational logs are only applied when the object's loglevel > 2, | ||
| 95 | or greater than the specified minimum loglevel | ||
| 96 | |||
| 97 | @param $msg information message text | ||
| 98 | @param $level optional minimum log level at which to log the text | ||
| 99 | |||
| 100 | @return $self | ||
| 101 | |||
| 102 | |||
| 103 | =end classdoc | ||
| 104 | |||
| 105 | =cut | ||
| 106 | sub logInfo { | ||
| 107 | my ($self, $msg, $level) = @_; | ||
| 108 | |||
| 109 | $level = 2 unless defined($level); | ||
| 110 | |||
| 111 | return $self | ||
| 112 | unless ($self->{LogLevel} > $level); | ||
| 113 | |||
| 114 | $self->{EventLogger}->log(join('', 'Info: [', ref $self, '] ', $msg, "\n")), | ||
| 115 | return $self | ||
| 116 | if $self->{EventLogger}; | ||
| 117 | |||
| 118 | print STDERR join('', 'Info: [', ref $self, '] ', $msg), "\n"; | ||
| 119 | return $self; | ||
| 120 | } | ||
| 121 | =pod | ||
| 122 | |||
| 123 | =begin classdoc | ||
| 124 | |||
| 125 | Log detail message timing traces. Only logs when the | ||
| 126 | diagnostic message timing is enabled. No prefixes | ||
| 127 | are required. | ||
| 128 | |||
| 129 | @param $msg the message timing text | ||
| 130 | |||
| 131 | @return the object | ||
| 132 | |||
| 133 | |||
| 134 | =end classdoc | ||
| 135 | |||
| 136 | =cut | ||
| 137 | sub logTiming { | ||
| 138 | print STDERR $_[1], "\n" | ||
| 139 | if $_[0]->{Stats}{Debug}{Timing} || $_[0]->{_diag_time}; | ||
| 140 | return $_[0]; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub _formatted_log_time { | ||
| 144 | # | ||
| 145 | # format: [DD/Mmm/YYYY:HH:MM:SS -UUUU] | ||
| 146 | # for now we use gmtime, since we don't have a handy | ||
| 147 | # UTC delta method | ||
| 148 | # | ||
| 149 | my $dt = gmtime(); | ||
| 150 | my @t = split(/\s+/, $dt); | ||
| 151 | return "[$t[2]/$t[1]/$t[4]:$t[3] +0000]"; | ||
| 152 | } | ||
| 153 | |||
| 154 | =pod | ||
| 155 | |||
| 156 | =begin classdoc | ||
| 157 | |||
| 158 | Collect web request log info. The HTTP request line, user authorization, referer, | ||
| 159 | and user agent are collected for later logging purposes. The collected information | ||
| 160 | is stored in this Logable object for later use when the response is eventually | ||
| 161 | sent back to the client. Note that this step is required | ||
| 162 | in order to avoid altering the standard HTTP::Daemon::ClientConn send_XXX() | ||
| 163 | interfaces overridden by HTTP::Daemon::Threaded::Socket to provide automatic web logging | ||
| 164 | capability. Only performed if a WebLogger is configured. | ||
| 165 | |||
| 166 | @param $request an HTTP::Request object | ||
| 167 | |||
| 168 | @return this Logable object | ||
| 169 | |||
| 170 | |||
| 171 | =end classdoc | ||
| 172 | |||
| 173 | =cut | ||
| 174 | sub scanForLogging { | ||
| 175 | my ($self, $request) = @_; | ||
| 176 | |||
| 177 | return undef unless $self->{WebLogger}; | ||
| 178 | |||
| 179 | my $basic = '"' . join(' ', $request->method(), $request->uri(), $request->protocol()) . '"'; | ||
| 180 | my ($userid, $pass) = $request->authorization_basic(); | ||
| 181 | $userid = '-' | ||
| 182 | unless $userid; | ||
| 183 | |||
| 184 | my $refer = $request->referer || '-'; | ||
| 185 | $refer = '"' . $refer . '"' | ||
| 186 | unless ($refer eq '-'); | ||
| 187 | |||
| 188 | my $useragent = $request->user_agent || '-'; | ||
| 189 | $useragent = '"' . $useragent . '"' | ||
| 190 | unless ($useragent eq '-'); | ||
| 191 | |||
| 192 | $self->{_log_fragments} = [ "- $userid", $basic, "$refer $useragent" ]; | ||
| 193 | return $self; | ||
| 194 | } | ||
| 195 | |||
| 196 | =pod | ||
| 197 | |||
| 198 | =begin classdoc | ||
| 199 | |||
| 200 | Log a web request. The previously stored HTTP request log fragments | ||
| 201 | are combined with the client IP address, response timestamp, response status, and | ||
| 202 | response size. The output format is the Apache Combined Log format. | ||
| 203 | |||
| 204 | @param $addr client IP address | ||
| 205 | @param $status response HTTP status code | ||
| 206 | @param $size response size in bytes | ||
| 207 | |||
| 208 | @return this object | ||
| 209 | @see <a href='http://httpd.apache.org/docs/1.3/logs.html#common'>Apache Combined Log format</a> | ||
| 210 | |||
| 211 | |||
| 212 | =end classdoc | ||
| 213 | |||
| 214 | =cut | ||
| 215 | sub logRequest { | ||
| 216 | my ($self, $addr, $status, $size) = @_; | ||
| 217 | |||
| 218 | return $self unless $self->{WebLogger}; | ||
| 219 | # | ||
| 220 | # fields are (separated by a single space): | ||
| 221 | # client IP address, | ||
| 222 | # clientid (always '-'), | ||
| 223 | # HTTP Authent userid (if any, otherwise, '-'), | ||
| 224 | # [end-of-request timestamp as (Day/Month/Year:HH:MM:SS UTC offset)] | ||
| 225 | # "basic request header (including method)" | ||
| 226 | # response status code | ||
| 227 | # resp size ("-" if no resp) | ||
| 228 | # "Referer" | ||
| 229 | # "UserAgent" | ||
| 230 | # | ||
| 231 | my $msg = join(' ', | ||
| 232 | $addr, | ||
| 233 | $self->{_log_fragments}[0], | ||
| 234 | _formatted_log_time(), | ||
| 235 | $self->{_log_fragments}[1], | ||
| 236 | $status, | ||
| 237 | $size || '-', | ||
| 238 | $self->{_log_fragments}[2]); | ||
| 239 | |||
| 240 | $self->{WebLogger}->log($msg), | ||
| 241 | return $self; | ||
| 242 | } | ||
| 243 | |||
| 244 | =pod | ||
| 245 | |||
| 246 | =begin classdoc | ||
| 247 | |||
| 248 | Updates the current log level | ||
| 249 | |||
| 250 | @param $level new log level | ||
| 251 | |||
| 252 | @return new log level | ||
| 253 | |||
| 254 | |||
| 255 | =end classdoc | ||
| 256 | |||
| 257 | =cut | ||
| 258 | sub setLogLevel { $_[0]->{LogLevel} = $_[1]; } | ||
| 259 | |||
| 260 | =pod | ||
| 261 | |||
| 262 | =begin classdoc | ||
| 263 | |||
| 264 | Returns the current log level | ||
| 265 | |||
| 266 | @return log level | ||
| 267 | |||
| 268 | |||
| 269 | =end classdoc | ||
| 270 | |||
| 271 | =cut | ||
| 272 | sub getLogLevel { return $_[0]->{LogLevel}; } | ||
| 273 | |||
| 274 | 1; |