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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:15:06+00:00 2026-06-12T11:15:06+00:00

I know how to output classname and methodname with log4j, both fields can be

  • 0

I know how to output classname and methodname with log4j, both fields can be aligned and padded separately, e.g. this pattern:

[%d{ISO8601}] %-5p %-10C{1}:%-10M %m%n

produces something like:

[2012-09-20 08:25:12,111] WARN  Class     :method     Logtext1 which should align
[2012-09-20 08:25:12,111] WARN  ClassTwo  :methodName Logtext2 which should align

This will align the logged text (in most cases), but the classname and methodname are ugly spaced.

So what I would like to output is something like:

[2012-09-20 08:25:12,111] WARN  Class:method          Logtext1 which should align
[2012-09-20 08:25:12,111] WARN  ClassTwo:methodName   Logtext2 which should align

Is this possible using only configuration?

Can you suggest another way of outputting methodname and classname, keeping the logged message text aligned?

  • 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-12T11:15:07+00:00Added an answer on June 12, 2026 at 11:15 am

    It seems like this is not possible using only configuration.
    The next simpler solution seems to be a custom PatternLayout, which then can be used in the log4j.ini with the character ‘b’:

    log4j.appender.filelog.layout= your.full.namespace.BPatternLayout
    log4j.appender.filelog.layout.ConversionPattern=[%d{ISO8601}] %-5p %-30b %m%n

    Hope it helps someone else, too.

    BPatternLayout.java:

    import org.apache.log4j.PatternLayout;
    import org.apache.log4j.helpers.PatternParser;
    
    public class BPatternLayout extends PatternLayout
    {
        @Override
        protected PatternParser createPatternParser(String pattern)
        {
            return new BPatternParser(pattern);
        }
    }
    

    BPatternParser.java:

    import org.apache.log4j.helpers.FormattingInfo;
    import org.apache.log4j.helpers.PatternConverter;
    import org.apache.log4j.helpers.PatternParser;
    import org.apache.log4j.spi.LocationInfo;
    import org.apache.log4j.spi.LoggingEvent;
    
    public class BPatternParser extends PatternParser
    {
        public BPatternParser(String pattern)
        {
            super(pattern);
        }
    
        @Override
        protected void finalizeConverter(char c)
        {
            switch (c)
            {
            case 'b':
                currentLiteral.setLength(0);
                addConverter(new BPatternConverter(formattingInfo));
                break;
            default:
                super.finalizeConverter(c);
            }   
        }
    
        private class BPatternConverter extends PatternConverter
        {
            BPatternConverter(FormattingInfo formattingInfo)
            {
                super(formattingInfo);
            }
    
            @Override
            protected String convert(LoggingEvent evt)
            {
                LocationInfo locationInfo = evt.getLocationInformation();
                return getPartialClassName(locationInfo, 1) + ":" + locationInfo.getMethodName();
            }
    
            /**
             * Code from org.apache.log4j.helpers.PatternParser.NamedPatternConverter
             */
            private String getPartialClassName(LocationInfo loc, int precision)
            {
                String n = loc.getClassName();
                if (precision <= 0) {
                    return n;
                }
    
                int len = n.length();
    
                // We substract 1 from 'len' when assigning to 'end' to avoid out of
                // bounds exception in return r.substring(end+1, len). This can happen if
                // precision is 1 and the category name ends with a dot.
                int end = len - 1;
                for (int i = precision; i > 0; i--)
                {
                    end = n.lastIndexOf('.', end - 1);
                    if (end == -1)
                        return n;
                }
                return n.substring(end + 1, len);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how I can show the output from this piece
I would like to know how can I output a number with 2 decimal
Grammar: http://pastebin.com/ef2jt8Rg y.output: http://pastebin.com/AEKXrrRG I don't know where is those conflicts, someone can help
How to get the matlab's output? NOT the whole output(I know diary can help)
I know the output is four one three two. Can any one explain how?
I would like to know how to output an array of doubles to the
I know that it is possible to read commands output with a pipe? But
Does anyone know how to set Visual Studio 2008 to output the current date/time
I am eager to know how to print the output on a terminal screen
Does anyone know of a way to save the console.log output in Chrome to

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.