MooseX::Role::Loggable - Extensive, yet simple, logging role using Log::Dispatchouli
version 0.010
package My::Object; use Moose; # or Moo with 'MooseX::Role::Loggable'; sub do_this { my $self = shift; $self->set_prefix('[do_this] '); $self->log_debug('starting...'); ... $self->log_debug('more stuff'); $self->clear_prefix; }
This is a role to provide logging ability to whoever consumes it using Log::Dispatchouli. Once you consume this role, you have the attributes and methods documented below.
You can propagate your logging definitions to another object that uses MooseX::Role::Loggable using the log_fields attribute as such:
log_fields
package Parent; use Moo; # replaces Any::Moose and Mouse (and Moose) use MooseX::Role::Loggable; # picking Moo or Moose has child => ( is => 'ro', isa => 'Child', lazy => 1, builder => '_build_child', ); sub _build_child { my $self = shift; return Child->new( $self->log_fields ); }
This module uses Moo so it takes as little resources as it can by default, and can seamlessly work if you're using either Moo or Moose.
A boolean for whether you're in debugging mode or not.
Default: no.
Read-only.
The facility the logger would use. This is useful for syslog.
Default: local6.
The ident the logger would use. This is useful for syslog.
Default: MooseX::Role::Loggable.
A boolean that determines if the logger would log to a file.
Default location of the file is in /tmp.
A boolean that determines if the logger would log to STDOUT.
A boolean that determines if the logger would log to STDERR.
The leaf name for the log file.
Default: undef
The path for the log file.
Whether to append the PID to the log filename.
Default: yes
Whether failure to log is fatal.
Whether only fatals are logged.
Default: no
From Log::Dispatchouli: 'stderr' or 'stdout' or an arrayref of zero, one, or both fatal log messages will not be logged to these.
Default: stderr
A hash of the fields definining how logging is being done.
This is very useful when you want to propagate your logging onwards to another object which uses MooseX::Role::Loggable.
It will return the following attributes and their values in a hash: debug, debug, logger_facility, logger_ident, log_to_file, log_to_stdout, log_to_stderr, log_file, log_path, log_pid, log_fail_fatal, log_muted, log_quiet_fatal.
debug
logger_facility
logger_ident
log_to_file
log_to_stdout
log_to_stderr
log_file
log_path
log_pid
log_fail_fatal
log_muted
log_quiet_fatal
A Log::Dispatchouli object.
All methods here are imported from Log::Dispatchouli. You can read its documentation to understand them better.
Log a message.
Log a message only if in debug mode.
Log a message and die.
Set the debug flag.
Clear the debug flag.
Set a prefix for all next messages.
Clears the prefix for all next messages.
Sets the mute property, which makes only fatal messages logged.
Clears the mute property.
Occassionally you might encounter the following error:
no ident specified when using Log::Dispatchouli at Loggable.pm line 117.
The problem does not stem from MooseX::Role::Loggable, but from a builder calling a logging method before the logger is built. Since Moo and Moose do not assure order of building attributes, which means that some attributes might not exist by the time you need them.
This specific error happens when the ident attribute isn't built by the time a builder runs. In order to avoid it, the attribute which uses the builder should be made lazy, and then called in the BUILD method. Here is an example:
ident
BUILD
package Stuff; use Moo; # or Moose with 'MooseX::Role::Logger'; has db => ( is => 'ro', lazy => 1, builder => '_build_db', } sub _build_db { my $self = shift; $self->log_debug('Building DB'); ... } sub BUILD { my $self = shift; $self->db; }
This makes the db attribute non-lazy, but during run-time. This will assure that all the logging attributes are created before you build the db attribute and call log_debug.
db
log_debug
Sawyer X <xsawyerx@cpan.org>
This software is copyright (c) 2012 by Sawyer X.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install MooseX::Role::Loggable, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Role::Loggable
CPAN shell
perl -MCPAN -e shell install MooseX::Role::Loggable
For more information on module installation, please visit the detailed CPAN module installation guide.