I have a script that "use"s various other modules.  The script will
take a command-line argument that dictates the log level, i.e., "WARN",
"ERROR", etc.  The script initializes a Log4perl instance using this level.
The script calls functions from the external modules, for which I would
like to have Log4perl log using the same log level that the calling script
has initialized.

Here is a simple example of what I have:

---[start Logging.pm]-----
#!/usr/bin/perl -w
package Logging;
use strict;
use Log::Log4perl qw(get_logger :levels);
use base 'Exporter';

our @EXPORT_OK      = qw($dbg_method_msg $logger statusMessage logMessage);
my $log_conf = q{
    log4perl.category.MODULE             = ERROR, moduleDebug, moduleRest
    # Filter to match level DEBUG
    log4perl.filter.MatchDebug = Log::Log4perl::Filter::LevelMatch
    log4perl.filter.MatchDebug.LevelToMatch  = DEBUG
    log4perl.filter.MatchDebug.AcceptOnMatch = true

    # Filter to match everything but DEBUG
    log4perl.filter.MatchRest  = Log::Log4perl::Filter::LevelMatch
    log4perl.filter.MatchRest.LevelToMatch  = DEBUG
    log4perl.filter.MatchRest.AcceptOnMatch = false

    # layout for DEBUG messages
    log4perl.appender.moduleDebug = Log::Log4perl::Appender::Screen
    log4perl.appender.moduleDebug.layout   =
Log::Log4perl::Layout::PatternLayout
    log4perl.appender.moduleDebug.layout.ConversionPattern =
[%d{yyyy-MM-dd HH:mm:ss}] %M:%L %p: %m%n
    log4perl.appender.moduleDebug.Filter   = MatchDebug
    log4perl.appender.moduleDebug.stderr   = 0


    # Normal layout for the rest
    log4perl.appender.moduleRest = Log::Log4perl::Appender::Screen
    log4perl.appender.moduleRest.layout   = Log::Log4perl::Layout::PatternLayout
    log4perl.appender.moduleRest.layout.ConversionPattern =
[%d{yyyy-MM-dd HH:mm:ss}] %C %p: %m%n
    log4perl.appender.moduleRest.Filter   = MatchRest
    log4perl.appender.moduleRest.stderr   = 0

    log4perl.category.SCRIPT             = ERROR, script
    log4perl.appender.script = Log::Log4perl::Appender::Screen
    log4perl.appender.script.stderr = 0
    log4perl.appender.script.layout = Log::Log4perl::Layout::PatternLayout
    log4perl.appender.script.layout.ConversionPattern = [%d{yyyy-MM-dd
HH:mm:ss}] %F{1} %p: %m%n
};

Log::Log4perl->init( \$log_conf );

sub scriptInit
{
  our $logger = get_logger("SCRIPT");
}
1;
---[end Logging.pm]-----

---[start Package.pm]-----
#!/usr/bin/perl -w
package Package;
use strict;
use Carp qw( carp croak );
use Logging;
use Log::Log4perl qw(get_logger );
my $logger = Log::Log4perl->get_logger("MODULE");

use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS );
use Exporter;
@ISA    = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK =
  qw(
  logTest
);

sub logTest
{
  $logger->info("info message");
  $logger->warn("warn message");
  $logger->error("error message");
}

1;
---[end Package.pm]-----

---[start script.pl]-----
#!/usr/bin/perl -w
use strict;
#use FindBin;
#use lib "$FindBin::RealBin/../../../lib";
use Logging qw( $logger );
use Package qw(logTest );

Logging->scriptInit();

$logger->level("INFO");
$logger->info("Calling logTest()");
logTest();
---[end]-----

As it stands, the output of this script is:

[2011-07-22 10:58:41] script.pl INFO: Calling logTest()
[2011-07-22 10:58:41] Package ERROR: error message

What I want is:

[2011-07-22 10:58:41] script.pl INFO: Calling logTest()
[2011-07-22 10:58:41] Package INFO: info message
[2011-07-22 10:58:41] Package WARN: warn message
[2011-07-22 10:58:41] Package ERROR: error message

How can I do that?

Thanks,
-- 
Steve Chadsey <schad...@gmail.com>

------------------------------------------------------------------------------
Magic Quadrant for Content-Aware Data Loss Prevention
Research study explores the data loss prevention market. Includes in-depth
analysis on the changes within the DLP market, and the criteria used to
evaluate the strengths and weaknesses of these DLP solutions.
http://www.accelacomm.com/jaw/sfnl/114/51385063/
_______________________________________________
log4perl-devel mailing list
log4perl-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/log4perl-devel

Reply via email to