The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Name

Class::Usul::Log - A simple flexible logging class

Synopsis

   use Class::Usul::Log;

   my $file = [ 't', 'test.log' ];
   my $log  = Class::Usul::Log->new( encoding => 'UTF-8', logfile => $file );
   my $text = 'Your error message goes here';

   # Can now call the following. The text will be encoded UTF-8
   $log->debug( $text ); # Does not log as debug was not true in the constructor
   $log->info ( $text );
   $log->warn ( $text );
   $log->error( $text );
   $log->alert( $text );
   $log->fatal( $text );

Description

A simple flexible logging class that supports both OO and functional interfaces

Creates methods for each logging level that encode their output. The logging levels are defined by the log levels constant

Configuration and Environment

Defines the following attributes;

appclass

Not an actual attribute. This value, if passed to the constructor, will be used as a key to class attribute that caches instances of this class for use by the functional interface

debug

Debug flag defaults to false. If set to true calls to log at the debug level will succeed rather than being ignored

encoding

Optional output encoding. If present output to the logfile is encoded

log

Optional log object. Will instantiate an instance of Log::Handler if this is not provided and the "logfile" attribute is provided

log_attributes

Attributes used to create the log object. Defaults to an empty hash reference

log_class

The classname of the log object. This is loaded on demand and defaults to Log::Handler if the "logfile" attribute is provided. If the "logfile" attribute is not provided Class::Null is used instead

logfile

Path to the logfile

Subroutines/Methods

BUILDARGS

   $usul_object_ref = Class::Usul->new;
   $log_object_ref  = Class::Usul::Log->new( builder => $usul_object_ref );

Passing an instance of Class::Usul to the constructor allows it leech attribute values from the Usul configuration object

BUILD

Store the new object reference in a class attribute for later importation. The class attribute is a hash reference keyed by the appclass attribute passed to the constructor. The first logger instance created is also stored keyed by default

import

   use Class::Usul::Log { as => ... }, 'get_logger';

Imports the get_logger function which is called as

   my $log_object_ref = get_logger $instance_name;

where the $instance_name is either the appclass attribute value passed to the OO constructor or the string default. The function returns an instance of this class. The optional parameters allow the function to imported as a different name

   use Class::Usul::Log { as => ... }, 'default';

Imports the "log" method from the default log instance as a function. Specify the appclass value instead of default to import from that instance instead. The optional parameters allow the function to imported as a different name

filehandle

Return the loggers file handle. This was added for IO::Async, so that we can tell it not to close the log file handle when it forks a child process and only works if the log_class is Log::Handler

log

   $self->log( $message );
   $self->log( $level, $message );
   $self->log( $level, $message, { ... } );
   $self->log( level => $level, message => $message, options => { ... } );
   $self->log( { level => $level, message => $message, options => { ... } } );

Logs the message at the given level. Accepts multiple signatures

Diagnostics

None

Dependencies

Class::Null
Moo
Encode
File::DataClass
Log::Handler

Incompatibilities

There are no known incompatibilities in this module

Bugs and Limitations

There are no known bugs in this module. Please report problems to the address below. Patches are welcome

Acknowledgements

Larry Wall - For the Perl programming language

Author

Peter Flanigan, <pjfl@cpan.org>

License and Copyright

Copyright (c) 2016 Peter Flanigan. All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic

This program is distributed in the hope that it will be useful, but WITHOUT WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE