The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Log::Any::For::Package - Add logging to package

VERSION

version 0.20

SYNOPSIS

 # Add log to some packages

 use Foo;
 use Bar;
 use Log::Any::For::Package qw(Foo Bar);
 ...

 # Now calls to your module functions are logged, by default at level 'trace'.
 # To see the logs, use e.g. Log::Any::App in command-line:

 % TRACE=1 perl -MLog::Any::App -MFoo -MBar -MLog::Any::For::Package=Foo,Bar \
     -e'Foo::func(1, 2, 3)'
 ---> Foo::func([1, 2, 3])
  ---> Bar::nested()
  <--- Bar::nested()
 <--- Foo::func() = 'result'

 # Using add_logging_to_package(), gives more options

 use Log::Any::For::Package qw(add_logging_to_package);
 add_logging_to_package(packages => [qw/My::Module My::Other::Module/]);

FAQ

My package Foo is not in a separate source file, Log::Any::For::Package tries to require Foo and dies.

Log::Any::For::Package detects whether package Foo already exists, and require() the module if it does not. To avoid the require(), simply declare the package before use()-ing Log::Any::For::Package, e.g.:

 BEGIN { package Foo; ... }
 package main;
 use Log::Any::For::Package qw(Foo);

ENVIRONMENT

LOG_PACKAGE_INCLUDE_SUB_RE (str)

LOG_PACKAGE_EXCLUDE_SUB_RE (str)

LOG_SUB_ARGS (bool)

LOG_SUB_RESULT (bool)

CREDITS

Some code portion taken from Devel::TraceMethods.

SEE ALSO

Log::Any::For::Class

For some modules, use the appropriate Log::Any::For::*, for example: Log::Any::For::DBI, Log::Any::For::LWP.

DESCRIPTION

This module has Rinci metadata.

FUNCTIONS

None are exported by default, but they are exportable.

add_logging_to_package(%args) -> any

Add logging to package.

Logging will be done using Log::Any.

Currently this function adds logging around function calls, e.g.:

    ---> Package::func(ARGS)
    <--- Package::func() = RESULT
    ...

Arguments ('*' denotes required arguments):

  • filter_subs => code|re

    Filter subroutines to add logging to.

    The default is to read from environment LOGPACKAGEINCLUDESUBRE and LOGPACKAGEEXCLUDESUBRE (these should contain regex that will be matched against fully-qualified subroutine/method name), or, if those environment are undefined, add logging to all non-private subroutines (private subroutines are those prefixed by _). For example.

  • logger_args => any

    Pass arguments to logger.

    This allows passing arguments to logger routine.

  • packages* => array

    Packages to add logging to.

    Each element can be the name of a package or a regex pattern (any non-valid package name will be regarded as a regex). Package will be checked for existence; if it doesn't already exist then the module will be require()'d.

    This module will also install an @INC import hook if you have regex. So if you do this:

        % perl -MData::Sah -MLog::Any::For::Package=Data::Sah::.* -e'...'

    then if Data::Sah::Compiler, Data::Sah::Lang, etc get loaded, the import hook will automatically add logging to it.

  • postcall_logger => code

    Supply custom postcall logger.

    Just like precalllogger, but code will be called after subroutine/method is called. Code will be given a hashref argument \%args containing these keys: args (arrayref, a shallow copy of the original @), orig (coderef, the original subroutine/method), name (string, the fully-qualified subroutine/method name), result (arrayref, the subroutine/method result), logger_args (arguments given when adding logging).

    You can use this mechanism to customize logging.

  • precall_logger => code

    Supply custom precall logger.

    Code will be called when logging subroutine/method call. Code will be given a hashref argument \%args containing these keys: args (arrayref, a shallow copy of the original @_), orig (coderef, the original subroutine/method), name (string, the fully-qualified subroutine/method name), logger_args (arguments given when adding logging).

    You can use this mechanism to customize logging.

    The default logger accepts these arguments (can be supplied via logger_args):

    • indent => INT (default: 0)

    Indent according to nesting level.

    • max_depth => INT (default: -1)

    Only log to this nesting level. -1 means unlimited.

    • logsubargs => BOOL (default: 1)

    Whether to display subroutine arguments when logging subroutine entry. The default can also be supplied via environment LOGSUBARGS.

    • logsubresult => BOOL (default: 1)

    Whether to display subroutine result when logging subroutine exit. The default can also be set via environment LOGSUBRESULT.

Return value:

AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.