This is a brief guide to how you can use the Log::Trace module in your scripts and modules. The \*(C`Log::Trace\*(C' documentation has a comprehensive list of options.

The basics

You can enable tracing by specifying the tracing target via the '\*(C`use\*(C'' statement or at runtime via the \*(C`import()\*(C' method. In most cases, you'll want to keep the code that enables tracing in a single point, usually the main script of your application. In general, modules should avoid directly setting tracing options.

using Log::Trace in your scripts

Here's a slightly contrived example which demonstrates the \*(C`TRACE\*(C', \*(C`TRACEF\*(C', \*(C`DUMP\*(C' and \*(C`TRACE_HERE\*(C' functions:

        #!/usr/bin/perl -w
        use strict;
        use Another::Module;
        use Log::Trace log => '/var/log/myapp.log';

        TRACE("-------- Starting archiver ---------");
        TRACEF("We are going to try to archive %d items", scalar @ARGV);
        DUMP("List of things to archive", \@ARGV);
        archive_em($_) foreach(@ARGV);

        sub archive_em {
                my $thing = shift;
                unless (Another::Module::check_safe($thing)) {
                        warn "bad chars in: $thing";
                rename $thing, $thing.".archive" or warn "Couldn't archive $thing: $!";
                TRACE("Tried to archive $thing");

Note the way \*(C`Log::Trace\*(C' is imported. The import list controls where the output of the four tracing functions goes. Instead we could have done:

use Log::Trace qw(warn);

and the trace output would have gone to \s-1STDERR\s0.

Using Log::Trace with modules

In the previous example, tracing was enabled only in the main script. Now we'll see how to enable tracing in \*(C`Another::Module\*(C' at the same time.

First, \*(C`Another::Module\*(C' needs to define a \*(C`TRACE\*(C' subroutine. It may also define \*(C`TRACEF\*(C', \*(C`TRACE_HERE\*(C' and \*(C`DUMP\*(C' stubs. It can do that simply by using \*(C`Log::Trace\*(C'. However, if \*(C`Another::Module\*(C' defines its own stub tracing functions, we can remove the dependency on \*(C`Log::Trace\*(C'.

package Another::Module;

sub check_safe {my_routine { my $filename = shift; TRACE("Checking that '$filename' has safe characters"); return $filename =~ /^([\w.\-/]+)$/ }

sub my_other_routine { TRACE_HERE(); }

# tracing stubs sub TRACE {} sub TRACE_HERE {}

Now, in the main script, we can change the '\*(C`use\*(C'' statement so tracing will be enabled in \*(C`Another::Module\*(C':

use Log::Trace log => '/var/log/myapp.log', {Deep => 1};

By default, the \*(C`Deep\*(C' option will force \*(C`Log::Trace\*(C' to export tracing functions to any modules that define a \*(C`TRACE\*(C' subroutine. That includes modules that are not directly used by the main script. But this behaviour can be relaxed or tightened with other options. See \*(L"Deep import\*(R" for examples.

Adding \*(C`TRACE\*(C' and other stub functions to your module is an Interface Contract between your module and Log::Trace (in some software circles this might be given a name such as ISupportsTracing). Of course you can write other code that takes advantage of this interface completely independent of Log::Trace, e.g.

use Another::Module; if($ENV{DEBUG}) { *Another::Module::TRACE = sub {print "TRACE: ".join("\t",@_)."\n"}; }

Error handling

Since \*(C`Log::Trace\*(C' is designed with debugging in mind, all tracing failures are non-fatal, so allowing normal execution to continue. However, Log::Trace will report to \s-1STDERR\s0 that a problem has occurred.

For example, this code:

use Log::Trace file => '/myapp.log'; TRACE('Running'); print "Hello World!\n";

Will produce this output:

Log::Trace: Cannot open /myapp.log : Permission denied at lib/Log/ line <nnn>. Hello World!


Enabling tracing on the command line

You can invoke tracing on the command line:

perl -MLog::Trace=print -e "TRACE('hello')" perl -MLog::Trace=warn -e "TRACE('hello')" perl -MLog::Trace=log,test.log -e "TRACE('hello')"

However you can't apply this approach to scripts that use \*(C`Log::Trace\*(C' or define a \*(C`TRACE\*(C' stub as these will clobber *main::TRACE set up by -M when they are compiled. Fortunately it is straightforward to write your command-line scripts so you can, for example, get trace output with -t and deep trace output with -T:

use Log::Trace; use Getopt::Std; use MyModule;

use vars qw($opt_t $opt_T); getopts("tT");

# tracing import Log::Trace 'print' if $opt_t; import Log::Trace 'print' => {Deep => 1} if $opt_T;


Sending \s-1TRACE\s0 output to browser in \s-1CGI\s0

Whilst tracing to a log file or \*(C`STDERR\*(C' is tolerable for CGIs, it's often far more convenient to return the tracing information back to the browser of the client-side developer.

use CGI;

use constant DEV_SERVER => 1;

my $trace_buffer; if(DEV_SERVER && CGI::param('Tracing')) { require Log::Trace; import Log::Trace buffer => \$trace_buffer, {Deep => 1}; }

my $output = do_everything();

print CGI::header(); print $output; if (DEV_SERVER && $trace_buffer) { print "\n\n", "<pre>", CGI::escapeHTML($trace_buffer), "</pre>"; }

You should remember to change the \*(C`DEV_SERVER\*(C' constant when releasing the \s-1CGI\s0 to a production environment.

Log levels

\*(C`Log::Trace\*(C' can filter the tracing output by referring to the logging level. The logging level is defined when you enable tracing. \*(C`Log::Trace\*(C' doesn't impose any conventions on the levels. The default levels implementation requires that the levels be numeric, but that can be overriden.

In the simplest case, you can specify the level as a threshold value:

use Log::Trace print => {Level => 3};

In this example, all trace messages at level 3 or below will be output.

You can also specify a list of valid levels:

use Log::Trace print => {Level => [0 .. 3, 7]};

All the tracing functions accept a hash as an optional first parameter where you can specify the level for that trace message. E.g.:

TRACE({Level => 4}, "This is a warning"); TRACEF({Level => 6}, "%d items found", scalar @items); TRACE_HERE({Level => 10}); DUMP({Level => 8}, 'Retrieved data', \%data);

\*(C`DUMP\*(C' is designed to accept a hash as its first parameter, but there may be cases where you wish to dump a hash that contains a \*(C`Level\*(C' key. In those cases, you can take advantage of the return value of \*(C`DUMP()\*(C':

my $dumped = DUMP({Level => 1, Health => '0.68'}); TRACE({Level => 8}, 'Game stats', $dumped);

If you specify a tracing level when you enable \*(C`Log::Trace\*(C', then tracing messages that do not specify a level will not be output, unless you include \*(C`undef\*(C' in the trace levels:

use Log::Trace print => {Level => [3, undef]}; TRACE("This is level undef, and will be output"); TRACE({Level => 3}, "This will also be output"); TRACE({Level => 8}, "... but this won't");

Here are some sample tracing levels (borrowed from Log::Agent) which you can use as a guide:

0 emergency 1 alert 2 critical 3 error 4 warning 6 notice 8 info 10 debug

Fine-tuning deep import

Occasionally you won't want to see the trace output from \s-1ALL\s0 your modules in your application. For example your application may give a module a huge data structure or call it in a long loop. The \*(C`Exclude\*(C' option allows you to mask out one or more modules.

use Log::Trace warn => {'Deep' => 1, 'Exclude' => 'MyVerboseModule'};


use Log::Trace warn => {'Deep' => 1, 'Exclude' => ['MyVerboseModule', 'Another::Module']};

Conversely you can use an opt-in approach rather than opt-out. The \*(C`Match\*(C' option allows a regular expression to be used to select which packages are initialised by Log::Trace. For example:

use Log::Trace print => {'Deep' => 1, 'Match' => qr/^MySubSystem::/};

Advanced features

Issues with the order of importing

When the Deep or Everywhere options are used, Log::Trace is imported into all the packages which have been compiled so far.

use Package::Foo; use Log::Trace ('print' => {Deep => 1}); use Package::Bar; #Compiled after Log::Trace is imported

In this example, the \s-1TRACE\s0 function in Package::Bar won't be overridden. It's trivial to swap the order in the example above so that Log::Trace is the last module used, but suppose you have a module (such as a factory) that loads others on demand:

package MyApp::Reader; sub new { my $package = shift; my $type = shift; die unless($type =~ /^MyApp::Reader::\w+$/); eval "require $type"; die($@) if($@); return $type->new(@_); }

How do you ensure Log::Trace gets imported into the backend MyApp::Reader::* modules (without polluting all your modules with Log::Trace::import calls)?

Using the (experimental) AutoImport feature

The AutoImport feature will override \*(C`CORE::require\*(C' so that from now on any modules that are loaded will have the Log::Trace import run against them: use Log::Trace('log' => '/var/log/myapp.log', {'Deep' => 1, 'AutoImport' => 1}); This only works with recent versions of perl (see the \s-1ENVIRONMENT\s0 \s-1NOTES\s0 in Log::Trace).

Getting the factory to wire the components it produces

A more \*(L"low-tech\*(R" approach that works with all versions of perl is to get the factory to attach the stub functions of the modules it loads to whatever its own stub functions have been wired to by the caller. package MyApp::Reader; sub new { my $package = shift; my $type = shift; die unless($type =~ /^MyApp::Reader::\w+$/); eval "require $type"; die($@) if($@);

# Wire the component we've created into whatever # our TRACE etc function has been wired to *{"$type\::TRACE"} = \&MyApp::Reader::TRACE; *{"$type\::DUMP"} = \&MyApp::Reader::DUMP;

return $type->new(@_); }

Custom \s-1TRACE\s0 functions

If \*(C`STDOUT\*(C', \*(C`STDERR\*(C', \*(C`syslog\*(C', a file, a file handle, or a buffer is not to your liking then the custom method is for you.

Suppose you want to send your Log::Trace output into a database:

our $sth; $sth = setup_logging_statement();

use Log::Trace custom => \&log_to_database;

sub log_to_database {

#TRACE can get any number of arguments my $message = join(",", @_);



Controlling \s-1DUMP\s0 output

By default, Data::Dumper is used with a fixed set of options for \s-1DUMP\s0 output. You can choose a different serialiser using the \*(C`Dumper\*(C' option:

import Log::Trace('print' => {Dumper => "YAML"}});

Where the string refers to a Data::Serializer::* backend. You can also control the options passed to the Data::Serializer backend (and thus customise the \s-1DUMP\s0 output) by passing a hashref of Data::Serializer contructor options:

import Log::Trace('print' => {Dumper => { serializer => 'XML::Dumper', options => { dtd => 'path/to/my.dtd' } }});

At the time of writing, not all the configuration options of the underlying serialisation modules are exposed via their Data::Serializer wrappers. If you find this a limitation, please contribute patches to extend these modules as this will benefit a number of other modules that make use of the Data::Serializer \s-1API\s0.

Execution path vs. profiling

You can use the \*(C`AllSubs\*(C' tracing option to trace the execution path through each subroutine. By default \*(C`Log::Trace\*(C' only wraps each subroutine in packages with \*(C`TRACE\*(C' defined. You can force it to do it to all modules using the \*(C`Everywhere\*(C' option. The following:

use Data::Dumper; use Log::Trace print => {AllSubs => 1, Verbose => 1, Everywhere => 1, Exclude => 'Config'}; Data::Dumper->Dumpperl([[4]]);

generates the output:

main::_\|_ANON_\|_ (3) :: Data::Dumper::Dumpperl( ) Data::Dumper::Dumpperl (3) :: Data::Dumper::new( ) Data::Dumper::Dumpperl (3) :: Data::Dumper::_dump( Data::Dumper, ... ) Data::Dumper::_dump (205) :: overload::StrVal( ARRAY, ... ) overload::StrVal (239) :: overload::OverloadedStringify( ARRAY, ... ) overload::OverloadedStringify (92) :: overload::mycan( ) overload::OverloadedStringify (92) :: overload::ov_method( ) overload::OverloadedStringify (92) :: overload::mycan( ) overload::OverloadedStringify (92) :: overload::ov_method( ) overload::OverloadedStringify (92) :: overload::mycan( ) overload::OverloadedStringify (92) :: overload::ov_method( ) overload::OverloadedStringify (92) :: overload::mycan( ) overload::OverloadedStringify (92) :: overload::ov_method( ) Data::Dumper::_dump (205) :: Data::Dumper::_dump( Data::Dumper, ... ) (eval) (0) :: Data::Dumper::DESTROY( Data::Dumper, ... )

Targeting one module

You may wonder \*(L"How do I trace what's going on in module Acme::Foo I downloaded from \s-1CPAN\s0 that isn't Log::Trace enabled?\*(R". Assuming the module doesn't have any other kind of tracing that you can hook into, all you can do is use the \*(C`AllSubs\*(C' approach. Assuming that's \s-1OK\s0, you can restrict this to just the offending module with:

use Log::Trace print => {AllSubs => 1, Everywhere => 1, Match => qr/^Acme:Foo$/};

Avoiding performance penalty

Although the trace stubs don't do anything, they do incur a small function call overhead. If this performance hit is unacceptable, you can use a constant to enable/disable all the \*(C`Log::Trace\*(C' statements in your code. The test for the constant value will be optimised out at compile time so no runtime overhead is incurred if the constant has a false value:

package ThrashMe;

use constant TRACING_ENABLED => 1; #Set to zero to optimise

sub performance_critical { TRACE("this may slow things down") if(TRACING_ENABLED); do_stuff(); }

sub TRACE{}



$Revision: 1.9 $