Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 838709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:20:57+00:00 2026-05-15T05:20:57+00:00

Is there a way to use Log::Log4perl to make a smart self-logging module that

  • 0

Is there a way to use Log::Log4perl to make a smart self-logging module that logs its operations to a file even in the absence of the calling script not initializing Log4perl? As far as I can tell from the documentation, the only way to use Log4perl is to initialize it in the running script from a configuration, then modules implementing Log4perl calls log themselves based on the caller’s Log4perl config.

Instead, I’d like the modules to provide a default initialization config for Log4perl. This would provide the default file appender for the module’s category. Then, I could override this behavior by initing Log4perl in the caller with a different config if needed, and everything would hopefully just work.

Is this sort of defensive logging behavior possible or am I going to need to rely on initing Log4perl in every .pl script that calls the module I want logged?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:20:58+00:00Added an answer on May 15, 2026 at 5:20 am

    I do this in a custom Log role in Moose (irrelevant complicated code removed):

    package MyApp::Role::Log;
    
    use Moose::Role;
    use Log::Log4perl;
    
    my @methods = qw(
        log trace debug info warn error fatal
        is_trace is_debug is_info is_warn is_error is_fatal
        logexit logwarn error_warn logdie error_die
        logcarp logcluck logcroak logconfess
    );
    
    has _logger => (
        is => 'ro',
        isa => 'Log::Log4perl::Logger',
        lazy_build => 1,
        handles => \@methods,
    );
    
    around $_ => sub {
        my $orig = shift;
        my $this = shift;
    
        # one level for this method itself
        # two levels for Class:;MOP::Method::Wrapped (the "around" wrapper)
        # one level for Moose::Meta::Method::Delegation (the "handles" wrapper)
        local $Log::Log4perl::caller_depth;
        $Log::Log4perl::caller_depth += 4;
    
        my $return = $this->$orig(@_);
    
        $Log::Log4perl::caller_depth -= 4;
        return $return;
    
    } foreach @methods;
    
    method _build__logger => sub {
        my $this = shift;
    
        my $loggerName = ref($this);
        Log::Log4perl->easy_init() if not Log::Log4perl::initialized();
        return Log::Log4perl->get_logger($loggerName)
    };
    

    As you can see, the log object is self-initializing — if Log::Log4perl->init has not been called, then easy_init is called. You could easily modify this to allow each module to customize its logger — I do so with optional role parameters, with ref($this) as the default fallback.

    PS. You may also want to look at MooseX::Log::Log4perl, which is where I started before I used the logger role above. Someday when I get around to it I will submit some much-needed patches to that MX module to incorporate some features I have added.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to use the win32clipboard module to store a reference to
Is there a way to use lapply() in a manner that forces it to
Is there any way to use global int constants in Objective C that work
When we use the console.log(someObject.someFunction) firebug shows function() string only. Is there a way
Is there any way to use Google's API to retrieve a user's current zipcode
Is there a way to use a different css for Android than is used
Is there some way to use Tuning Advisor for SQL Express? Is there some
Is there a way to use numpy from a local directory without adding the
Is there any way to use Zend_Filter_Encrypt with large files, without rising memory limit
Is there a way to use abstract or annotated class as MXBean descriptor? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.