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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:39:49+00:00 2026-05-11T01:39:49+00:00

I have an appender that I only want the first X characters (for this

  • 0

I have an appender that I only want the first X characters (for this example, we’ll say 5) of the message to display.

I am using a PatternLayout but I can’t get the message to truncate the message correctly.

For example, if my log message is

The quick brown fox

I just want to see :

The q

When I use this in the Pattern

%.5m

I get

n fox

since those are the last 5 characters.

I’ve looked the PattenLayout javadoc, but could not find anything. I know this is a little strange to not want to see the entire message, but for this specific appender it makes sense. I do log the entire message in a different appender. I would like to avoid writing a custom class if possible.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T01:39:50+00:00Added an answer on May 11, 2026 at 1:39 am

    Truncation is done from the beginning of a message by default (differs from the printf in C, which does it from the end).

    The proper pattern should be:

    %.-5m 

    EDIT:

    I just tried this, and log4j does not like that pattern. However, the supplied pattern will work fine in LOGBack, if you can make the switch over. If you cannot switch your logging provider, you could make a one-off modification to log4j. The interesting bit of code appears on lines 75-76 of org.apache.log4j.helpers.PatternConverter:

    if(len > max)   sbuf.append(s.substring(len-max)); 

    This should read:

    if(len > max)   sbuf.append(s.substring(0,max)); 

    You can simply make the modification and recompile the jar for your use, or you can subclass PatternConverter to perform the proper truncation. This will also require a new version of PatternLayout, which contains the createPatternParser method, which you will need to override in your subclass to instantiate your new version of PatternConverter.

    As a side note, be aware of the licensing ramifications of modifying open source code in your specific project.

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

Sidebar

Related Questions

Using log4j, I used to have an appender that logs directly to one of
I have a page that is currently using the datetime microformat to display a
I have a question that is similar to this question , but mine concerns
I have a java app that uses log4j. Config: log4j.rootLogger=info, file log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File=${user.home}/logs/app.log log4j.appender.file.layout=org.apache.log4j.PatternLayout
I have a text file that's appended to over time and periodically I want
I have a rolling file appender configured with this: <appender name=RollingLogFileAppender type=log4net.Appender.RollingFileAppender> <file value=appname
i want to make a new data structure using arrays for this query it
I have an NSIS installer script that I'm using to deploy a py2exe-built app
Im using doxygen outside of its design, but well within its capability. I have
I have some columns in a data store using 12 decimal precision. I want

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.