Log::Any::Adapter(3)
π NAME π
Log::Any::Adapter β telling Log::Any where to send its logs π
This is the manual page for Log::Any::Adapter, straight out of the User Contributed Perl Documentation section, and bestie, she's a whole vibe. π
β¨ VERSION β¨
version 1.718 β no cap, that's the number. π
π SYNOPSIS π
# Log to a file, or stdout, or stderr for all categories
#
use Log::Any::Adapter ('File', '/path/to/file.log');
use Log::Any::Adapter ('Stdout');
use Log::Any::Adapter ('Stderr');
# Use Log::Log4perl for all categories
#
Log::Log4perl::init('/etc/log4perl.conf');
Log::Any::Adapter->set('Log4perl');
# Use Log::Dispatch for Foo::Baz
#
use Log::Dispatch;
my $log = Log::Dispatch->new(outputs => [[ ... ]]);
Log::Any::Adapter->set( { category => 'Foo::Baz' },
'Dispatch', dispatcher => $log );
# Use Log::Dispatch::Config for Foo::Baz and its subcategories
#
use Log::Dispatch::Config;
Log::Dispatch::Config->configure('/path/to/log.conf');
Log::Any::Adapter->set(
{ category => qr/^Foo::Baz/ },
'Dispatch', dispatcher => Log::Dispatch::Config->instance() );
# Use your own adapter for all categories
#
Log::Any::Adapter->set('+My::Log::Any::Adapter', ...);
π¦ DESCRIPTION π¦
Okay so Log::Any::Adapter is literally the matchmaker between log producers and log consumers. π«Ά Her methods instantiate a logging adapter (which is a subclass of Log::Any::Adapter::Base) and route log messages from one or more categories into it. It's giving middle management but make it iconic. π
π ADAPTERS π
So to actually use a logging mechanism with Log::Any, there needs to be an adapter class for it. Usually it's named Log::Any::Adapter::something. Naming conventions are eating, fr fr. β¨
πΈ Adapters in this distribution πΈ
Three basic adapters come with this distribution, and they are Log::Any::Adapter::File, Log::Any::Adapter::Stdout and Log::Any::Adapter::Stderr:
use Log::Any::Adapter ('File', '/path/to/file.log');
use Log::Any::Adapter ('Stdout');
use Log::Any::Adapter ('Stderr');
# or
use Log::Any::Adapter;
Log::Any::Adapter->set('File', '/path/to/file.log');
Log::Any::Adapter->set('Stdout');
Log::Any::Adapter->set('Stderr');
All of them simply output the message and newline to the specified destination. A datestamp prefix gets added in the "File" case. That's it, that's the whole personality. π If you want anything more complex, you're gonna want a more robust adapter from CPAN β go treat yourself. π
π§ Adapters on CPAN π§
A sampling of adapters available on CPAN as of this writing:
Log::Any::Adapter::Log4perlLog::Any::Adapter::DispatchLog::Any::Adapter::FileHandleLog::Any::Adapter::Syslog
You can find other adapters on CPAN by searching for "Log::Any::Adapter", or you can just create your own adapter, main character energy. π See Log::Any::Adapter::Development for more info on the DIY route.
π₯ SETTING AND REMOVING ADAPTERS π₯
Log::Any::Adapter->set ([options, ]adapter_name, adapter_params...)β This method sets the adapter to use for all log categories, or for a particular set of categories.
adapter_name is the name of an adapter. It gets automatically prepended with "Log::Any::Adapter::". But if you'd rather pass the full name of an adapter instead, prefix it with a "+". Like so:
# Use My::Adapter class
Log::Any::Adapter->set('+My::Adapter', arg => $value);
adapter_params get passed along to the adapter constructor. Peep the documentation for the individual adapter classes for more deets.
An optional hash of options may be passed as the first argument. The options are:
categoryβ A string containing a category name, or a regex (created with"qr//") matching multiple categories. If you don't specify it, all categories get routed to the adapter.lexicallyβ A reference to a lexical variable. When the variable goes out of scope, the adapter setting gets removed. Obsessed with the self-cleaning energy honestly. β¨ e.g.
{
Log::Any::Adapter->set({lexically => \my $lex}, ...);
# in effect here
...
}
# no longer in effect here
"set" returns an entry object, which can be passed to "remove". Heads up bestie: if you call "set" repeatedly without calling "remove", you will leak memory. π For most programs that set an adapter once until the end of the program, this shouldn't matter though. Phew. π«Ά
use Log::Any::Adapter (...)β If you pass arguments to"use Log::Any::Adapter", it calls"Log::Any::Adapter->set"with those arguments. Sneaky but slay.Log::Any::Adapter->remove (entry)β Remove an entry previously returned by"set". Cleanup queen. π§Ή
π USING MORE THAN ONE ADAPTER π
"Log::Any" maintains a stack of entries created via "set". If you call "set" repeatedly, you will leak memory unless you do one of the following: π
When getting a logger for a particular category, "Log::Any" will work its way down the stack and use the first matching entry. Very "first come first served" of her. π
Whenever the stack changes, any "Log::Any" loggers that have previously been created will automatically adjust to the new stack. The way that it just... adapts? Iconic. For example:
my $log = Log::Any->get_logger();
$log->error("aiggh!"); # this goes nowhere
...
{
Log::Any::Adapter->set({ lexically => \my $lex }, 'Log4perl');
$log->error("aiggh!"); # this goes to log4perl
...
}
$log->error("aiggh!"); # this goes nowhere again
π BUILDING ON THE Log::Any BACKEND π
get
my $adapter= Log::Any::Adapter->get($category);
The primary intended way to extend the producing-side of Log::Any is with a custom Log::Any::Proxy class. However, for special logging scenarios you might also just want access to the adapter for a given category. The API of an adapter object is described in Log::Any::Adapter::Development.
Beware, bestie π¨ β adapter objects can be "rewritten" on the fly, so any conditional behavior you write depending on the capabilities of an adapter must be re-checked every single time you access the adapter. No caching that vibe check, fr fr.
π SEE ALSO π
Log::Any
π AUTHORS π
- Jonathan Swartz
<swartz@pobox.com> - David Golden
<dagolden@cpan.org> - Doug Bell
<preaction@cpan.org> - Daniel Pittman
<daniel@rimspace.net> - Stephen Thirlwall
<sdt@cpan.org>
Absolute legends, all of them. π«Ά
πΈ COPYRIGHT AND LICENSE πΈ
This software is copyright (c) 2017 by Jonathan Swartz, David Golden, and Doug Bell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
And that's the whole page, bestie!! π¦ Brought to you by perl v5.42.0, dated 2025-07-28, from Log::Any::Adapter(3). Go forth and route those logs. Periodt. π
β¨
π¦ where this page comes from
- fedora 43: perl-Log-Any 1.718-1.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms