| 1 | # -*- mode: cperl; eval: (follow-mode) -*- | ||
| 2 | # | ||
| 3 | |||
| 4 | package App::Regather::Logg; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use diagnostics; | ||
| 9 | use Sys::Syslog qw(:standard :macros); | ||
| 10 | use Mail::Send; | ||
| 11 | use POSIX qw(strftime); | ||
| 12 | use Sys::Hostname; | ||
| 13 | use Data::Printer caller_info => 1, class => { expand => 2 }; | ||
| 14 | |||
| 15 | # https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg | ||
| 16 | use constant DPC => { info => 'green', | ||
| 17 | err => 'red', | ||
| 18 | debug => 'magenta', | ||
| 19 | warning => 'yellow', }; | ||
| 20 | |||
| 21 | =pod | ||
| 22 | |||
| 23 | =encoding UTF-8 | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | App::Regather::Logg - logging class | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use App::Regather::Logg; | ||
| 32 | my $log = new App::Regather::Logg( prognam => 'MyAppName', | ||
| 33 | foreground => $foreground_or_syslog, | ||
| 34 | colors => $wheather_to_use_term_colors ); | ||
| 35 | $log->cc( pr => 'info', fm => "App::Regather::Logg initialized ... (write to syslog)" ); | ||
| 36 | $log->cc( fg => 1, fm => "App::Regather::Logg initialized ... (write to STDOUT)" ); | ||
| 37 | ... | ||
| 38 | my $mesg = $ldap->search( filter => "(objectClass=unsearchebleThing)"); | ||
| 39 | $log->logg_ldap_err( mesg => $mesg ); | ||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | This is a class to log messages. | ||
| 44 | |||
| 45 | =head1 CONSTRUCTOR | ||
| 46 | |||
| 47 | =over 4 | ||
| 48 | |||
| 49 | =item B<new> | ||
| 50 | |||
| 51 | Creates a new B<App::Regather::Logg> object | ||
| 52 | |||
| 53 | =over 4 | ||
| 54 | |||
| 55 | =item prognam =E<gt> 'MyAppName' | ||
| 56 | |||
| 57 | program name | ||
| 58 | |||
| 59 | =item foreground =E<gt> 1 | 0 | ||
| 60 | |||
| 61 | STDOUT or syslog, default is: 0 | ||
| 62 | |||
| 63 | =item colors =E<gt> 1 | 0 | ||
| 64 | |||
| 65 | wheather to use terminal colors, default is: 0 | ||
| 66 | |||
| 67 | if set, then priorities are colored this way: | ||
| 68 | |||
| 69 | =over 4 | ||
| 70 | |||
| 71 | info => 'ansi113' | ||
| 72 | |||
| 73 | err => 'bold ansi255 on_ansi196' | ||
| 74 | debug => 'ansi195' | ||
| 75 | |||
| 76 | warning => 'bold ansi237 on_ansi214' | ||
| 77 | |||
| 78 | =back | ||
| 79 | |||
| 80 | for reference look at L<Term::ANSIColor> | ||
| 81 | |||
| 82 | =item ts_fmt =E<gt> 'strftime(3) format string' | ||
| 83 | |||
| 84 | timestamp format string, default is: "%a %F %T %Z (%z)" | ||
| 85 | |||
| 86 | =back | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub new { | ||
| 93 | my ( $self, %args ) = @_; | ||
| 94 | |||
| 95 | $args{colors} = $args{colors} // 0; | ||
| 96 | $args{foreground} = $args{foreground} // 0; | ||
| 97 | $args{prognam} = $args{prognam} // lc( (split(/:/, __PACKAGE__))[0] ); | ||
| 98 | $args{tsargsfmt} = '%a %F %T %Z (%z)'; | ||
| 99 | |||
| 100 | eval { $args{hostname} = hostname }; | ||
| 101 | $args{hostname} = 'HOSTNAME_NOT_AVAILABLE' if $@; | ||
| 102 | |||
| 103 | openlog($args{prognam}, "ndelay,pid") if ! $args{foreground}; | ||
| 104 | |||
| 105 | bless { %args }, $self; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub colors { shift->{colors} } | ||
| 109 | sub foreground { shift->{foreground} } | ||
| 110 | sub host_name { shift->{hostname} } | ||
| 111 | sub prognam { shift->{prognam} } | ||
| 112 | sub ts_fmt { shift->{ts_fmt} } | ||
| 113 | |||
| 114 | =head1 METHODS | ||
| 115 | |||
| 116 | =over 4 | ||
| 117 | |||
| 118 | =item B<conclude> | ||
| 119 | |||
| 120 | main method to do the job | ||
| 121 | |||
| 122 | =over 4 | ||
| 123 | |||
| 124 | =item fg =E<gt> 1 | 0 | ||
| 125 | |||
| 126 | foreground: stdin or syslog | ||
| 127 | |||
| 128 | =item pr =E<gt> 'level[|facility]' | ||
| 129 | |||
| 130 | priority | ||
| 131 | |||
| 132 | =item fm =E<gt> "... %s ... : %m" | ||
| 133 | |||
| 134 | sprintf format string, with the addition that %m is replaced with "$!" | ||
| 135 | |||
| 136 | =item ls =E<gt> [ $a, $b, ... ] | ||
| 137 | |||
| 138 | list of values to be passed to sprintf as arguments | ||
| 139 | |||
| 140 | =item nt =E<gt> 1 | 0 | ||
| 141 | |||
| 142 | wheather to send (notify) you this message with I<notify> method | ||
| 143 | |||
| 144 | =back | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub conclude { | ||
| 149 | my ( $self, %args ) = @_; | ||
| 150 | my %arg = ( fg => $args{fg} // $self->{foreground}, | ||
| 151 | pr => $args{pr} // 'info', | ||
| 152 | fm => $args{fm}, | ||
| 153 | nt => $args{nt} // 0 ); | ||
| 154 | $arg{pr_s} = sprintf("%s|%s", $arg{pr}, $self->{facility} // 'local4'); | ||
| 155 | $arg{pr_f} = sprintf("%s: ", uc($arg{pr}) ); | ||
| 156 | |||
| 157 | if ( exists $args{ls} ) { | ||
| 158 | @{$arg{ls}} = map { ref && ref ne 'SCALAR' ? np($_, caller_info => 0) : $_ } @{$args{ls}}; | ||
| 159 | } else { | ||
| 160 | $arg{ls} = []; | ||
| 161 | } | ||
| 162 | |||
| 163 | $arg{msg} = sprintf $arg{pr_f} . $arg{fm}, @{$arg{ls}}; | ||
| 164 | if ( $arg{fg} ) { | ||
| 165 | my $msg = sprintf("%s %s", | ||
| 166 | strftime("%b %e %H:%M:%S", localtime), | ||
| 167 | $arg{msg}); | ||
| 168 | p( $msg, | ||
| 169 | colored => $self->{colors} && $self->{foreground}, | ||
| 170 | caller_info => 0, | ||
| 171 | colors => { string => DPC->{$arg{pr}} }, | ||
| 172 | output => 'stdout' ); | ||
| 173 | } else { | ||
| 174 | syslog( $arg{pr_s}, $arg{pr_f} . $arg{fm}, @{$arg{ls}} ); | ||
| 175 | } | ||
| 176 | |||
| 177 | $self->notify( msg => $arg{msg} ) if $arg{nt}; | ||
| 178 | } | ||
| 179 | |||
| 180 | =item B<cc> | ||
| 181 | |||
| 182 | alias for I<conclude> method | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub cc { goto &conclude } | ||
| 187 | |||
| 188 | =item B<conclude_ldap_err> | ||
| 189 | |||
| 190 | method - wrapper around Net::LDAP::Message->error methods | ||
| 191 | |||
| 192 | =over 4 | ||
| 193 | |||
| 194 | =item mesg =E<gt> Net::LDAP::Message object | ||
| 195 | |||
| 196 | =back | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | sub conclude_ldap_err { | ||
| 201 | my ( $self, %args ) = @_; | ||
| 202 | my %arg = ( mesg => $args{mesg}, | ||
| 203 | nt => $args{nt} // 0 ); | ||
| 204 | $self->cc( pr => 'err', | ||
| 205 | fm => "LDAP ERROR:\n% 13s%s\n% 13s%s\n% 13s%s\n% 13s%s\n\n", | ||
| 206 | ls => [ 'ERROR: ', $arg{mesg}->error_name, | ||
| 207 | 'TEXT: ', $arg{mesg}->error_text, | ||
| 208 | 'DESCRIPTION: ', $arg{mesg}->error_desc, | ||
| 209 | 'SERVER ERROR: ', $arg{mesg}->server_error ] ); | ||
| 210 | |||
| 211 | $self->notify( msg => $arg{mesg}->error_desc ) if $arg{nt}; | ||
| 212 | } | ||
| 213 | |||
| 214 | =item B<cc_ldap_err> | ||
| 215 | |||
| 216 | alias for I<conclude_ldap_err> method | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub cc_ldap_err { goto &conclude_ldap_err } | ||
| 221 | |||
| 222 | =item B<notify> | ||
| 223 | |||
| 224 | method to be used to send log message via email | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | sub notify { | ||
| 229 | my ( $self, %args ) = @_; | ||
| 230 | my $email = Mail::Send->new; | ||
| 231 | $email->subject(sprintf("[regather @ %s] %s... (skipped)", | ||
| 232 | $self->{hostname}, | ||
| 233 | substr( $args{msg}, 0, 50))); | ||
| 234 | $email->to( @{$self->{notify_email}} ); | ||
| 235 | my $email_body = $email->open; | ||
| 236 | print $email_body sprintf("host: %s\n\n", $self->{hostname}); | ||
| 237 | print $email_body $args{msg}; | ||
| 238 | $email_body->close || | ||
| 239 | $self->cc( pr => 'err', ls => [ $! ], | ||
| 240 | fm => "email sending error: %s", ); | ||
| 241 | } | ||
| 242 | |||
| 243 | =item B<set_m> | ||
| 244 | |||
| 245 | setter to set options from config file | ||
| 246 | |||
| 247 | on input it expects App::Regather::Config object section for App::Regather::Logg | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | sub set_m { | ||
| 252 | my ( $self, $cf ) = @_; | ||
| 253 | if ( ref($cf) eq 'HASH' ) { | ||
| 254 | while ( my ( $k, $v ) = each %$cf ) { | ||
| 255 | next if exists $self->{$k}; | ||
| 256 | $self->{$k} = $v; | ||
| 257 | } | ||
| 258 | } else { | ||
| 259 | $self->cc( pr => 'err', | ||
| 260 | fm => "Logg::set_m(): argument supplied is not HASH ..." ); | ||
| 261 | return 0; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | =item B<set> | ||
| 266 | |||
| 267 | setter to set one single pair key => value | ||
| 268 | |||
| 269 | =over 4 | ||
| 270 | |||
| 271 | =item key =E<gt> value | ||
| 272 | |||
| 273 | =back | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub set { | ||
| 278 | my ( $self, $k, $v ) = @_; | ||
| 279 | $self->{$k} = $v; | ||
| 280 | } | ||
| 281 | |||
| 282 | =item B<get> | ||
| 283 | |||
| 284 | getter | ||
| 285 | |||
| 286 | =back | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub get { | ||
| 291 | my ( $self, $k ) = @_; | ||
| 292 | if ( exists $self->{$k} ) { | ||
| 293 | $self->{$k}; | ||
| 294 | } else { | ||
| 295 | $self->cc( pr => 'err', | ||
| 296 | fm => "attribute \"%s\" doesn't exist", | ||
| 297 | ls => [ $k ] ); | ||
| 298 | return; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | =head1 SEE ALSO | ||
| 303 | |||
| 304 | L<Sys::Syslog>, | ||
| 305 | L<Data::Printer>, | ||
| 306 | L<Term::ANSIColor> | ||
| 307 | |||
| 308 | =head1 AUTHOR | ||
| 309 | |||
| 310 | Zeus Panchenko E<lt>zeus@gnu.org.uaE<gt> | ||
| 311 | |||
| 312 | =head1 COPYRIGHT | ||
| 313 | |||
| 314 | Copyright 2019 Zeus Panchenko. | ||
| 315 | |||
| 316 | This program is free software; you can redistribute it and/or modify | ||
| 317 | it under the terms of the GNU General Public License as published by | ||
| 318 | the Free Software Foundation; either version 3, or (at your option) | ||
| 319 | any later version. | ||
| 320 | |||
| 321 | This program is distributed in the hope that it will be useful, | ||
| 322 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 323 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 324 | GNU General Public License for more details. | ||
| 325 | |||
| 326 | You should have received a copy of the GNU General Public License | ||
| 327 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 328 | |||
| 329 | =cut | ||
| 330 | |||
| 331 | 1; |