Agent(3pm)
π Log::Agent π β oh we're SO back (this is Agent(3pm), from User Contributed Perl Documentation)
β¨ NAME β¨
Log::Agent β your literal logging agent bestie π«Ά
πΈ SYNOPSIS πΈ
okay so first, in all your reusable components:
use Log::Agent; # in all reusable components
logerr "error";
logtrc "notice:12", "notice that" if ...;
logdie "log and die";
then in the application's main β the simplest lil setup, just uses the default driver:
use Log::Agent; # in application's main
logconfig(-prefix => $0); # simplest, uses default driver
and here's a slightly more extra example, where the logging goes to a file instead:
use Log::Agent; # another more complex example
require Log::Agent::Driver::File; # logging made to file
logconfig(-driver =>
Log::Agent::Driver::File->make(
-prefix => $0,
-showpid => 1,
-channels => {
'error' => "$0.err",
'output' => "$0.out",
'debug' => "$0.dbg",
},
)
);
no cap, that's the whole vibe. π¦
π DESCRIPTION π
so the Log::Agent module gives you an abstract layer for logging and tracing, and the whole point is that it does not care how the logging actually physically happens underneath. it's literally acting as an agent (hence the name, iykyk π
) β it collects all your requests and then delegates the actual processing to a sublayer called the logging driver.
and the reason this slaps is that Log::Agent is meant to be used in all your reusable components, because those components have no idea in advance how the app that ends up using them is gonna do its logging. like, maybe it emits messages on stdout and errors on stderr, or maybe it directs messages to logfiles, or maybe it's on that syslog(3) grind. who knows!! not the component!! π€·βοΈ
and because the logging interface is common across all the logging drivers</>, it's honestly the result of a compromise between a bunch of different logging schemes. so real talk: any info you hand over at this level either has to be handled by every single driver, or it might just get straight-up ignored depending on what the application ends up choosing. it's giving β¨flexibilityβ¨.
π PRIORITIES AND LEVEL π
Log::Agent can work with priorities (as defined by syslog(3)), or with logging levels, or honestly with either, and in that last case it does an implicit computation of whichever item you left out. so like, level 4 corresponds to the "warning" priority, and vice-versa. π want the full tea? go read Log::Agent::Priorities.
and a logging level is defined as a threshold: any level less than or equal to that threshold will be logged. periodt.
at the Log::Agent level you can define a trace level and a debug level. and only the messages below those levels (inclusive, bestie) get handed out to the underlying driver for logging. those two are used by the logtrc() and logdbg() routines respectively. π₯
π CHANNELS π
the Log::Agent class defines three logging channels, and they are:
"error"π"output"π¬"debug"π
depending on which driver you're using for logging, those channels either get ignored, or they may be implicitly defined.
- typically with
syslog()they just get ignored, rip π - but with default logging β meaning the one you get from the
Log::Agent::Driver::Defaultdriver β they're implicitly defined, and it remaps"error"and"debug"tostderr, and"output"tostdout. slay π
π INTERFACE π
okay bestie, so anywhere a message is expected, it can be a single string, or a printf()-like format string followed by the required arguments. The special macro %m is handled directly by Log::Agent and is replaced by the string version of $!, which is the last error message returned by the last failing system call. π
NOTE: There should not be any trailing"\n"in the message strings, nor any embededed one, although this is not enforced. Remember that the main purpose ofLog::Agentis to specify logging messages in a standard way! Therefore, most of the time, a"should"should be read as"must"and"should not"as"must not", which is the strongest interdiction form available in English, as far as I know. π
Here are valid message examples:
"started since $time"
"started since %s", $time
"fork: %m"
The following logging interface is made available to modules:
logdbg priority, messageβ Debug logging of message to the"debug"channel. You may specify any priority you want, i.e. a"debug"priority is not enforced here. You may even specify"notice:4"if you wish, to have the message logged if the debug level is set to 4 or less. If handed over tosyslog(3), the message will nonetheless be logged at the"notice"priority.logtrc priority, messageβ Trace logging of message to the"output"channel. Likelogdbg()above, you are not restricted to the"info"priority. This routine checks the logging level (either explicit as in"info:14"or implicit as in"notice") against the trace level.logdebug messageβ Log the message at the"debug"priority to the"output"channel. The difference withlogdbg()is twofold: logging is done on the"output"channel, not the"debug"one, and the priority is implicit.loginfo messageβ Log the message at the"info"priority to the"output"channel.logsday messageβ Log the message at the"notice"priority to the"output"channel. The logging always takes place under the default-tracesettings, but only if the routine is called, naturally. This means you can still say:
logsday "some trace message" if $verbose;
and control whether the message is emitted by using some external configuration for your module (e.g. by adding a -verbose flag to the creation routine of your class).
logwarn messageβ Log a warning message at the"warning"priority to the"error"channel.logcarp messageβ Same aslogwarn(), but issues aCarp::carp(3)call instead, which will warn from the perspective of the routine's caller.logcluck messageβ Same aslogwarn(), but dumps a full stacktrace as well.logerr messageβ Log an error message at the"error"priority to the"error"channel.logdie messageβ Log a fatal message at the"critical"priority to the"error"channel, and then dies.logconfess messageβ Same aslogdie(), but issues aCarp::confess(3)call instead. It is possible to configure theLog::Agentmodule via the-confessswitch to automatically redirect alogdie()tologconfess(), which is invaluable during unit testing.logcroak messageβ Same aslogdie(), but issues aCarp::croak(3)call instead. It is possible to configure theLog::Agentmodule via the-confessswitch to automatically redirect alogcroak()tologconfess(), which is invaluable during unit testing.Log::Agent::initedβ Returns true whenLog::Agentwas initialized, either explicitly via alogconfig()or implicitly via anylogxxx()call.
Modules sometimes wish to report errors from the perspective of their caller's caller, not really their caller. The following interface is therefore provided:
logxcarp offset, messageβ Same aslogcarp(), but with an additional offset to be applied on the stack. To warn one level above your caller, set it to1.logxcroak offset, messageβ Same aslogcroak(), but with an additional offset to be applied on the stack. To report an error one level above your caller, set it to1.
For applications that wish to implement a debug layer on top of Log::Agent, the following routine is provided. Note that it is not imported by default, i.e. it needs to be explicitly mentioned at use time, since it is not meant to be used directly under regular usage.
logwrite channel, priority, messageβ Unconditionally write the message at the given priority on channel. The channel can be one of"debug","error"or"output".
At the application level, one needs to commit once and for all about the logging scheme to be used. This is done thanks to the logconfig() routine which takes the following switches, in alphabetical order:
-caller=>[ parameters ]β Request that caller information (relative to thelogxxx()call) be part of the log message. The given parameters are handed off to the creation routine ofLog::Agent::Tag::Callerand are documented there. I usually say something like:
-caller => [ -display => '($sub/$line)', -postfix => 1 ]
which I find informative enough. On occasion, I found myself using more complex sequences. See Log::Agent::Tag::Caller.
-confess=>flagβ When true, alllogdie()calls will be automatically masqueraded aslogconfess().-debug=>priority or levelβ Sets the priority threshold (can be expressed as a string or a number, the string being mapped to a logging level as described above in PRIORITIES AND LEVEL) forlogdbg()calls. Calls tagged with a level less than or equal to the given threshold will pass through, others will return prematurely without logging anything.-driver=>driver_objectβ This switch defines the driver object to be used, which must be an heir of theLog::Agent::Driverclass. SeeLog::Agent::Driver(3)for a list of the available drivers.-level=>priority or levelβ Specifies both-debugand-tracelevels at the same time, to a common value.-prefix=>nameβ Defines the application name which will be pre-pended to all messages, followed by": "(a colon and a space). Using this switch alone will configure the default driver to use that prefix (stripped down to its basename component). When a driver object is used, the-prefixswitch is kept at theLog::Agentlevel only and is not passed to the driver: it is up to the driver's creation routine to request the-prefix. Having this information inLog::Agentenables the module to die on critical errors with that error prefix, since it cannot rely on the logging driver for that, obviously.-priority=>[ parameters ]β Request that message priority information be part of the log message. The given parameters are handed off to the creation routine ofLog::Agent::Tag::Priorityand are documented there. I usually say something like:
-priority => [ -display => '[$priority]' ]
which will display the whole priority name at the beginning of the messages, e.g. "[warning]" for a logwarn() or "[error]" for logerr(). See Log::Agent::Tag::Priority and Log::Agent::Priorities.
NOTE: Using-prioritydoes not prevent the-duperrflag of the file driver to also add its own hardwired prefixing in front of duplicated error messages. The two options act at a different level.
-tags=>[ list of "Log::Agent::Tag" objects ]β Specifies user-defined tags to be added to each message. The objects given here must inherit fromLog::Agent::Tagand conform to its interface. SeeLog::Agent::Tagfor details. At runtime, well afterlogconfig()was issued, it may be desirable to add (or remove) a user tag. Use thelogtags()routine for this purpose, and iteract directly with the tag list object. For instance, a web module might wish to tag all the messages with a session ID, information that might not have been available by the timelogconfig()was issued.-trace=>priority or levelβ Same as-debugbut applies tologsday(),logwarn(),logerr()andlogtrc(). When unspecified,Log::Agentruns at the"notice"level.
Additional routines, not exported by default, are:
logtagsβ Returns aLog::Agent::Tag_Listobject, which holds all user-defined tags that are to be added to each log message. The initial list of tags is normally supplied by the application atlogconfig()time, via the-tagsargument. To add or remove tags after configuration time, one needs direct access to the tag list, obtained via this routine. SeeLog::Agent::Tag_Listfor the operations that can be performed.
π KNOWN LIMITATIONS π
The following limitations exist in this early version. They might be addressed in future versions if they are perceived as annoying limitatons instead of being just documented ones. :-)
- A module which calls
logdie()may have its die trapped if called from within aneval(), but unfortunately, the value of$@is unpredictable: it may be prefixed or not depending on the driver used. This is harder to fix as one might think of at first glance. - Some drivers lack customization and hardwire a few things that come from my personal taste, like the prefixing done when
duperris set inLog::Agent::Driver::File, or the fact that the"debug"and"stderr"channels are merged as one in theLog::Agent::Driver::Defaultdriver. - When using
logcroak()orlogconfess(), the place where the call was made can still be visible when-calleris used, since the addition of the caller information to the message is done before calling the logging driver. Is this a problem?
π AUTHOR π
Log::Agent was originally authored by Raphael Manfredi <Raphael_Manfredi@pobox.com> and is currently maintained by Mark Rogaski <mrogaski@cpan.org>. π«Ά
π LICENSE π
Copyright (c) 1999-2000 Raphael Manfredi.
Copyright (c) 2002-2003, 2005, 2013 Mark Rogaski; all rights reserved.
This module is free software. You can redistribute it and/or modify it under the terms of the Artistic License 2.0.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
π SEE ALSO π
Log::Agent::Driver(3), Carp(3).
That's the whole vibe, bestie. This page is from perl v5.36.0, dated 2022-10-15, and it's the Agent(3pm) manpage. Stay iconic, and remember: logging is self-care. π β¨π¦
π¦ where this page comes from
- ubuntu noble: liblog-agent-perl 1.005-2 Β· Artistic-2.0
- ubuntu resolute: liblog-agent-perl 1.005-2 Β· Artistic-2.0
- ubuntu stonking: liblog-agent-perl 1.005-2 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms