Results 1 to 4 of 4
  1. #1
    suddublr is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Formatting of log message using log4j

    Hello group,

    I need to log the messages to a file, where each attributes of the log message should be of constant length.

    output:
    ------
    param1 param2 param3 [line 1]
    pot test serve [ line 2]

    The message attributes passed to the logger api is just a space seperated like
    logMessage("param1 param2 param3"); logMessage("pot test serve");

    I searched in log4j manuals, but could not figure out how to parse the %m so that to pad extra spaces to make the output as desired.

    Any pointer as how to proceed ? .

  2. #2
    suddublr is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    The format got deleted, the actual output should be

    Hello123454<space><space><space>Test<space><space> <space>hi
    ghg<space><space><space><space>ss11<space><space>< space>sssss

  3. #3
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    You can do that programatically.

    For this you need to know the maximum lenth of each column value.

    If you know the maximum column length, then keep adding the values to a StringBuilder. Before adding just check the length of value and fill the difference with spaces.

    Once you are done with complete line contents, pass on the stringbuilder object to log4j for logging.

    I hope that makes sense to you.

  4. #4
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    use something like this in your log4J properties file:

    log4j.appender.A2=org.apache.log4j.RollingFileAppe nder
    log4j.appender.A2.File=/app/ibot/java.log
    log4j.appender.A2.layout=org.apache.log4j.PatternL ayout
    log4j.appender.A2.layout.ConversionPattern=<%d{ISO 8601}> %-5p %-15.50c{1} %m %n
    log4j.appender.A2.MaxFileSize=10MB
    log4j.appender.A2.MaxBackupIndex=10

    more information on formatting can be found at:

    PatternLayout (Apache Log4j 1.2.16 API)

Similar Threads

  1. Replies: 1
    Last Post: 09-20-2010, 09:32 AM
  2. Replies: 6
    Last Post: 06-04-2010, 09:43 PM
  3. formatting..
    By sireesha in forum New To Java
    Replies: 16
    Last Post: 06-26-2009, 08:11 PM
  4. How to insert a Linebreak in a log4j message?
    By MJalbert in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-06-2008, 02:03 PM
  5. Formatting the date
    By yuchuang in forum New To Java
    Replies: 5
    Last Post: 05-07-2007, 07:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •