Log::ger::Manual::Tutorial::500_Category - Categories
version 0.033.000
Aside from filtering log messages by level, you can also filter by category. Category normally maps to Perl packages (but they can also be something else). For example:
# in My/App/Module1.pm package My::App::Module1; use Log::ger; sub foo { log_warn("foo!"); } 1; # in My/App/Module2.pm package My::App::Module2; use Log::ger; sub bar { log_warn("bar!"); } 1;
Logger subroutines in My::App::Module1 will log messages under the category My::App::Module1, while logger subroutines in My::App::Module2 will log messages under the category My::App::Module2. In your application, you can do something like:
My::App::Module1
My::App::Module2
use Log::ger::Output Composite => ( outputs => { ... }, category_level => { 'My::App' => 'info', 'My::App::Module1' => 'debug', 'My::App::Module2' => 'error', }, );
Categories work hierarchically. The above means, other modules under My::App, like My::App::Module3 or My::App::Module4::Submodule is set to level info.
My::App
My::App::Module3
My::App::Module4::Submodule
info
package My::App::Module4; use Log::ger (); my $log = Log::ger->get_logger(); # by default will use the caller's package my $log_foo = Log::ger->get_logger(category => "Foo"); $log->warn("This message will be logged under the category 'My::App::Module4'"); $log_foo->warn("This message will be logged under the category 'Foo'");
To direct log messages under a certain category to its own output, for example category Foo to a separate file /path/foo.log while the rest go to /path/app.log, you can do something like this:
Foo
/path/foo.log
/path/app.log
use Log::ger::Output Composite => ( outputs => { File => [ { conf => {path=>'/path/app.log'}, category_level => { Foo => 'off' }, }, { conf => {path=>'/path/foo.log'}, level => 'off', category_level => { Foo => 'trace' }, }, ], }, );
So basically for /path/app.log you turn off the category Foo, while for /path/foo.log you turn off for all category except for Foo.
perlancar <perlancar@cpan.org>
This software is copyright (c) 2020, 2019, 2018, 2017 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Log::ger::Manual, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Log::ger::Manual
CPAN shell
perl -MCPAN -e shell install Log::ger::Manual
For more information on module installation, please visit the detailed CPAN module installation guide.