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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:02:41+00:00 2026-06-10T04:02:41+00:00

How can I set up log4j to use two separate patterns based on the

  • 0

How can I set up log4j to use two separate patterns based on the logging level of the message?

For example, right now I’m using the following which applies to all levels:

log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH\:mm\:ss} - %m%n

What I’d like to do is use a different pattern for levels of WARN and above. Such as:

%-5p %d{HH\:mm\:ss} - [%L:%C] %m%n

To output the line number and class where the warning or error occurred. The extra cost of pulling the caller information would be worth it in situations where a warning or error occurs.

Is this doable?

Here is my full log4j.properties file:

log4j.rootLogger=WARN, stdout1, stdout2
log4j.category.my.package=DEBUG

log4j.appender.stdout1=org.apache.log4j.ConsoleAppender
log4j.appender.stdout1.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout1.layout.ConversionPattern=%-5p %d{HH\:mm\:ss} - %m%n

log4j.appender.stdout2=org.apache.log4j.ConsoleAppender
log4j.appender.stdout2.threshold=WARN
log4j.appender.stdout2.target=System.err
log4j.appender.stdout2.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout2.layout.ConversionPattern=%-5p %d{HH\:mm\:ss} - [%t] [%L:%C] %m%n

EDIT

threshold looks to be what I need to log all levels of WARN and higher. Is there a similar property that will configure the other appender to log all levels of INFO and lower?

  • 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-10T04:02:42+00:00Added an answer on June 10, 2026 at 4:02 am

    Found the answer here: Logging error to stderr and debug, info to stdout with log4j

    I ended up having to use an xml file instead of a properties file to get the results that I want.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration>
        <!-- STDERR - WARN and above -->
        <appender name="stderr" class="org.apache.log4j.ConsoleAppender">
            <param name="threshold" value="warn" />
            <param name="target" value="System.err"/>
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%-5p %d{HH\:mm\:ss} - [%t][%L:%C] %m%n" />
            </layout>
        </appender>
        <!-- STDINFO - DEBUG & INFO -->
        <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
            <param name="threshold" value="debug" />
            <param name="target" value="System.out"/>
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%-5p %d{HH\:mm\:ss} - %m%n" />
            </layout>
            <filter class="org.apache.log4j.varia.LevelRangeFilter">
                <param name="LevelMin" value="debug" />
                <param name="LevelMax" value="info" />
            </filter>
        </appender>
        <category name="my.project.package">
            <!-- Display DEBUG and above for all log messages in My Project -->
            <param name="level" value="debug" />
        </category>
        <root>
            <!-- Global Setting - Display only WARN messages
                 This surpresses other libraries from display DEBUG & INFO messages
                 But the Category setting above for your project will override this 
                 and display DEBUG & INFO messages (using STDOUT) as well as 
                 WARN and above (using STDERR) -->
            <priority value="warn"></priority>
            <appender-ref ref="stderr" />
            <appender-ref ref="stdout" />
        </root>
    </log4j:configuration>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use log4j as logger for my web application. in log4j, I can set
Using the logging classes in java.util.logging, is it possible to set up two different
I am using Apache Commons Logging ™ . For now I wanted to use
I can set the event on the calendar on my device by using this
I understand that we can set the various style attributes from code behind using
I am used to using Tomcat/Log4J for my logging. My current project is deployed
I can set up an autofilter using pyWin32, but I wondered if it's possible
In log4j.properties I can set PatternLayout e.g. ([%p] %c - %m - %d %n)
I can set up one-to-many mapping of List objects by using annotations, however using
In weblogic I can configure in the console for the Serverlog to use log4j

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.