package Log::Any::Adapter::YAL; use strict; use warnings; use Log::Any::Adapter::Util (); use base qw(Log::Any::Adapter::Base); # Optionally initialize object, e.g. for delegation # sub init { my ($self) = @_; $self->{attr} = ...; } # Create logging methods: debug, info, etc. # foreach my $method ( Log::Any::Adapter::Util::logging_methods() ) { no strict 'refs'; *$method = sub { ... }; } # or, support structured logging instead sub structured { my ($self, $level, $category, @args) = @_; # ... process and log all @args } # Create detection methods: is_debug, is_info, etc. # foreach my $method ( Log::Any::Adapter::Util::detection_methods() ) { no strict 'refs'; *$method = sub { ... }; }
and the application:
Log::Any->set_adapter('YAL');
The easiest way to start is to look at the source of existing adapters, such as Log::Any::Adapter::Log4perl and Log::Any::Adapter::Dispatch.
Log::Any->set_adapter(I<something>);
If it's an internal driver, you can call it whatever you like and use it like
Log::Any->set_adapter('+My::Log::Adapter');
If the logging mechanism used by your adapter supports different levels, it's your responsibility to map them appropriately when you implement the logging and detection methods described below. For example, if your mechanism only supports ``debug'', ``normal'' and ``fatal'' levels, you might map the levels like this:
At this point, overriding the default constructor is not supported. Hopefully it will not be needed.
The constructor is called whenever a log object is requested. e.g. If the application initializes Log::Any like so:
Log::Any->set_adapter('Log::YAL', yal_object => $yal, depth => 3);
and then a class requests a logger like so:
package Foo; use Log::Any qw($log);
Then $log will be populated with the return value of:
Log::Any::Adapter::Yal->new(yal_object => $yal, depth => 3, category => 'Foo');
This is memoized, so if the same category should be requested again (e.g. through a separate "get_logger" call, the same object will be returned. Therefore, you should try to avoid anything non-deterministic in your ``init'' function.
These methods must log a message at the specified level.
To help generate these methods programmatically, you can get a list of the sub names with the Log::Any::Adapter::Util::logging_methods function.
These methods must return a boolean indicating whether the specified level is active, i.e. whether the adapter is listening for messages of that level.
To help generate these methods programmatically, you can get a list of the sub names with the Log::Any::Adapter::Util::detection_methods function.
See Log::Any::Adapter::Dispatch and Log::Any::Adapter::Log4perl for examples of usage.
The following Log::Any::Adapter::Util functions give you a list of methods that you need to implement. You can get logging methods, detection methods or both:
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.