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

  • Home
  • SEARCH
  • 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 8155559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:42:11+00:00 2026-06-06T16:42:11+00:00

I am trying to replace a pattern in the log message before it gets

  • 0

I am trying to replace a pattern in the log message before it gets logged on to the file. The logger uses the Perl Module Log::Log4perl. I see that there is only a Filter option available in this module, which does not solve my problem.

Actually, this is what I want to do. Let us say my log messages are "Testing the logger module" and "Developing the logger module". I want to replace the string 'module' to 'package' in the message. So, whenever there is a string ‘module’ in the log message, it should be replaced to ‘package’ before logging. And I want to use Perl’s regex find/replace here, which will allow me to replace different patterns.

Is this possible? Can anyone help?

Thanks in advance,

  • 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-06-06T16:42:14+00:00Added an answer on June 6, 2026 at 4:42 pm

    You can do this by specifying a custom cspec in your conf file.

    # basic config to write the log to a file
    log4perl.logger.mylog = DEBUG, MyLog
    log4perl.appender.MyLog = Log::Log4perl::Appender::File
    log4perl.appender.MyLog.filename = my.log
    
    # create a custom cspec called 'A' with an anonymous sub.
    # The sub would be called with the following args: $layout, $message, $category, $priority, $caller_level
    # Then apply the regex as you like.
    log4j.PatternLayout.cspec.A = sub { $_[1] =~ s/module/package/; $_[1] }
    
    # then use the cspec here with '%A'
    log4perl.appender.MyLog.layout = Log::Log4perl::Layout::PatternLayout
    log4perl.appender.MyLog.layout.ConversionPattern = %A%n
    

    And your code shouldn’t need to change:

    use Log::Log4perl;
    log::Log4perl::init('log.conf');
    my $logger = Log::Log4perl->get_logger('mylog');
    $logger->info("Testing the logger module"); # prints 'Testing the logger package'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replace one regex pattern with another regex pattern. st_srt =
I'm trying to replace a friendly url pattern with a html url notation but
Trying to replace any non alpha-numeric characters with a hyphen. Can't see why it
I have been trying to replace a word in a text file with a
I'm trying to get re.sub to replace a pattern specified with a value for
Im trying to make a preg-replace pattern to convert the text orderId into order-id.
I am trying to replace all occurrences of of a pattern in JavaScript. My
Am trying to replace content with empty using this regular expression. objRegExp.Pattern = <(?>/?)(?!(p|strong)(>|\s))[^<]+?>
I'm working with jQuery and am trying to write a pattern replace, but it
I am trying to replace a sentence in .config file using powershell. ${c:Web.config} =

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.