GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
MooseX::Role::Loggable(3) User Contributed Perl Documentation MooseX::Role::Loggable(3)

MooseX::Role::Loggable - Extensive, yet simple, logging role using Log::Dispatchouli

version 0.114

    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 attributes and methods for logging defined automatically.

    package MyObject;
    use Moose # Moo works too
    with 'MooseX::Role::Loggable';

    sub run {
        my $self = shift;

        $self->log('Trying to do something');

        # this only gets written if debug flag is on
        $self->log_debug('Some debugging output');

        $self->log(
            { level => 'critical' },
            'Critical log message',
        );

        $self->log_fatal('Log and die');
    }

This module uses Moo so it takes as little resources as it can by default, and can seamlessly work with both Moo or Moose.

Sometimes your objects create additional object which might want to log using the same settings. You can simply give them the same logger object.

    package Parent;
    use Moose;
    with 'MooseX::Role::Loggable';

    has child => (
        is      => 'ro',
        isa     => 'Child',
        lazy    => 1,
        builder => '_build_child',
    );

    sub _build_child {
        my $self = shift;
        return Child->new( logger => $self->logger );
    }

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: calling object's class name.

Read-only.

A boolean that determines if the logger would log to a file.

Default location of the file is in /tmp.

Default: no.

Read-only.

A boolean that determines if the logger would log to STDOUT.

Default: no.

A boolean that determines if the logger would log to STDERR.

Default: no.

The leaf name for the log file.

Default: undef

The path for the log file.

Default: undef

Whether to append the PID to the log filename.

Default: yes

Whether failure to log is fatal.

Default: yes

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 Log::Dispatchouli object.

All methods here are imported from Log::Dispatchouli. You can read its documentation to understand them better.

Determines if "log_file" was specified.

Determines if "log_path" was specified.

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.

You shouldn't care about this. It takes care of propagating attributes from a given logger (if you provided one) to the attributes this role provides.

DEPRECATED.

Please pass the logger attribute instead:

    SomeObject->new( logger => $parent->logger );

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, some attributes might not yet 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:

    package Stuff;

    use 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".

Sawyer X <xsawyerx@cpan.org>

This software is copyright (c) 2017 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.

2017-02-12 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.