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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:56:24+00:00 2026-05-16T03:56:24+00:00

I have a project that uses Apache Commons Logging & log4j with a large

  • 0

I have a project that uses Apache Commons Logging & log4j with a large number of classes doing logging. 95% of my logs show up with the same prefix

log4j.appender.MyCompany.layout.ConversionPattern=[%d][%-5p][%c] %m%n

[2010-08-05 11:44:18,940][DEBUG][com.mycompany.projectname.config.XMLConfigSource] Loading config from [filepath]
[2010-08-05 12:05:52,715][INFO ][com.mycompany.projectname.management.ManagerCore] Log entry 1
[2010-08-05 12:05:52,717][INFO ][com.mycompany.projectname.management.ManagerCore] Log entry 2

I know with %c{1}, I can just show the last part of the category (i.e. the class name), but is there a way to trim off the common portion ‘com.mycompany.projectname’ from each log under that package, considering how much room it takes up on each line?

  • 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-16T03:56:24+00:00Added an answer on May 16, 2026 at 3:56 am

    If you are using Log4j 1.2.16, you can change your layout to EnhancedPatternLayout, which lets you specify a negative value for the category parameter. From the docs:

    For example, for the category name “alpha.beta.gamma” … %c{-2} will remove two elements [from the front] leaving “gamma”

    Here is a more complete example:

    log4j.appender.C.layout=org.apache.log4j.EnhancedPatternLayout
    log4j.appender.C.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%t] [%c{-3}] %m%n
    

    which in your case should chop off com.mycompany.projectname.

    However, this will apply to every message that is logged, even if it didn’t come from your code. In other words, the category org.hibernate.engine.query.HQLQueryPlan would be trimmed to query.HQLQueryPlan, which may not be what you want.

    If you need absolute control over this (i.e. you want to specifically strip out the text “com.mycompany.projectname” from every message), then you will need to implement your own Layout class. Something like this should do it:

    import org.apache.log4j.PatternLayout;
    import org.apache.log4j.spi.LoggingEvent;
    
    public class MyPatternLayout extends PatternLayout
    {
      @Override
      public String format(LoggingEvent event)
      {
        String msg = super.format(event);
        msg = msg.replace("com.mycompany.projectname", "");
    
        return msg;
      }
    }
    

    Good luck!

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

Sidebar

Related Questions

I have a project that uses an Apache server that demands NTLM auth. If
I have a project that uses two third party libraries, both of which make
We have a project that uses JPA/Hibernate on the server side, the mapped entity
I have a project that uses a System.Timers.Timer to update the position of motors.
I have a project that uses the Enterprise Library 4.1. When I target .net
We have a project that uses HP Quality Center and one of the regular
I have a project that uses two independent Android libraries. Each of them contains
I have a project that uses the .net FileSystemWatcher to watch a Samba network
I have a project that uses JQuery-UI. I recently found formee, which is a
I have a Silverlight project that uses MVVM with Prism. I followed http://www.telerik.com/help/silverlight/patterns-and-practices-eventtocommand-prism.html 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.