App-Regather

 view release on metacpan or  search on metacpan

lib/App/Regather/Logg.pm  view on Meta::CPAN

use Data::Printer caller_info => 1, class => { expand => 2 };

# https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg
use constant DPC => { info    => 'green',
		      err     => 'red',
		      debug   => 'magenta',
		      warning => 'yellow', };

=pod

=encoding UTF-8

=head1 NAME

App::Regather::Logg - logging class

=head1 SYNOPSIS

    use App::Regather::Logg;
    my $log = new App::Regather::Logg( prognam    => 'MyAppName',
				  foreground => $foreground_or_syslog,
				  colors     => $wheather_to_use_term_colors );
    $log->cc( pr => 'info', fm => "App::Regather::Logg initialized ... (write to syslog)" );
    $log->cc( fg => 1, fm => "App::Regather::Logg initialized ... (write to STDOUT)" );
    ...
    my $mesg = $ldap->search( filter => "(objectClass=unsearchebleThing)");
    $log->logg_ldap_err( mesg => $mesg );

=head1 DESCRIPTION

This is a class to log messages.

=head1 CONSTRUCTOR

=over 4

=item B<new>

Creates a new B<App::Regather::Logg> object

=over 4

=item prognam =E<gt> 'MyAppName'

program name

=item foreground =E<gt> 1 | 0

STDOUT or syslog, default is: 0

=item colors =E<gt> 1 | 0

wheather to use terminal colors, default is: 0

if set, then priorities are colored this way:

=over 4

info    => 'ansi113'

err     => 'bold ansi255 on_ansi196'
debug   => 'ansi195'

warning => 'bold ansi237 on_ansi214'

=back

for reference look at L<Term::ANSIColor>

=item ts_fmt =E<gt> 'strftime(3) format string'

timestamp format string, default is: "%a %F %T %Z (%z)"

=back

=back

=cut

sub new {
  my ( $self, %args ) = @_;

  $args{colors}     = $args{colors}     // 0;
  $args{foreground} = $args{foreground} // 0;
  $args{prognam}    = $args{prognam}    // lc( (split(/:/, __PACKAGE__))[0] );
  $args{tsargsfmt}  = '%a %F %T %Z (%z)';

  eval { $args{hostname} = hostname };
  $args{hostname} = 'HOSTNAME_NOT_AVAILABLE' if $@;

  openlog($args{prognam}, "ndelay,pid") if ! $args{foreground};

  bless { %args }, $self;
}

sub colors     { shift->{colors} }
sub foreground { shift->{foreground} }
sub host_name  { shift->{hostname} }
sub prognam    { shift->{prognam} }
sub ts_fmt     { shift->{ts_fmt} }

=head1 METHODS

=over 4

=item B<conclude>

main method to do the job

=over 4

=item fg =E<gt> 1 | 0

foreground: stdin or syslog

=item pr =E<gt> 'level[|facility]'

priority

=item fm =E<gt> "... %s ... : %m"

sprintf format string, with the addition that %m is replaced with "$!"

=item ls =E<gt> [ $a, $b, ... ]



( run in 2.902 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )