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 555231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:47:36+00:00 2026-05-13T11:47:36+00:00

Log4perl is a great tool for logging. The warnings pragma is also an essential

  • 0

Log4perl is a great tool for logging.

The warnings pragma is also an essential tool.

However, when Perl scripts are running as daemons, the Perl warnings, are printed into STDERR where nobody can see them, and not into the Log4perl log file of the relevant program.

Is there a way to catch Perl warnings into the Log4perl log?

For example, this code will log just fine to the log file, but in case this is run as a daemon, the Perl warnings will be not be included in the log:

#!/usr/bin/env perl
use strict;
use warnings;

use Log::Log4perl qw(get_logger);

# Define configuration
my $conf = q(
                log4perl.logger                    = DEBUG, FileApp
                log4perl.appender.FileApp          = Log::Log4perl::Appender::File
                log4perl.appender.FileApp.filename = test.log
                log4perl.appender.FileApp.layout   = PatternLayout
);

# Initialize logging behaviour
Log::Log4perl->init( \$conf );

# Obtain a logger instance
my $logger = get_logger("Foo::Bar");
$logger->error("Oh my, an error!");

$SIG{__WARN__} = sub {
    #local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
    $logger->warn("WARN @_");
};

my $foo = 100;
my $foo = 44;

This still prints out to STDERR:

"my" variable $foo masks earlier declaration in same scope at log.pl line 27.

And the log file does not catch this warning.

  • 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-13T11:47:37+00:00Added an answer on May 13, 2026 at 11:47 am

    It’s in the Log4perl FAQ as Some module prints messages to STDERR. How can I funnel them to Log::Log4perl? and My program already uses warn() and die(). How can I switch to Log4perl?.

    Your specific problem has the added wrinkle that you are seeing a compile-time warning, so you need to adjust the FAQ advice to setup the logging at compile time as early as possible. Do that in a BEGIN block as close to the top of the source as you can stand:

     BEGIN {
         ... all of your logging setup
         }
    

    You can tell if it’s a compile-time warning by running a syntax check with warnings enabled:

     % perl -cw program
    

    If you see the warning during the syntax check, it’s a compile-time warning.

    I’d rather catch the compile-time warnings in development though. They shouldn’t make it through to a production system. 🙂

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

Sidebar

Related Questions

I'm using logging vai Log::Log4perl in my perl script; I'm wondering if making multiple
Is there a way to use Log::Log4perl to make a smart self-logging module that
Here's the code I have: use strict; use warnings; use Log::Log4perl qw(:easy); Log::Log4perl->init({ level
I'm using log4perl with a set of scripts and objects. I'd like to be
I have a Mason template running under mod_perl, which is using Log::Log4perl. I want
My (existing) perl files creates a Log file using Log4Perl in the following format
I'm planning to use Log4Perl in my modules for logging. My code structure goes
When using log4perl , the debug log layout that I'm using is : log4perl.appender.D10.layout=PatternLayout
I'm maintaining some website code that will soon dump all its errors and warnings
log4perl has a threshold option in the configuration file that will log all calls

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.