3

Can someone post how to achieve alignment in log messages:

[10:14:31 main     package1.Class1 <init>  INFO]: initializing data..
[10:14:31 Thread-0 package2.Class2 method1 INFO]: log message

I know I can use log4j but I want to know how to achieve this with JUL. I tried using MessageFormat, FieldPosition but to no avail.

Thanks

1 Answer 1

3

You can create you own java.util.logging.Formatter. For that you can extend it this way:

import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public final class MyFormatter extends Formatter {

    @Override
    public String format(LogRecord record) {
        StringBuilder sb = new StringBuilder();

        // Build output the way you want
        sb.append(new Date(record.getMillis()))
            .append(" \t")
            .append(record.getThreadID())
            .append(" \t")
            .append(record.getSourceMethodName())
            .append(" \t")
            .append(record.getSourceClassName())
            .append(" \t")
            .append(record.getLevel().getLocalizedName())
            .append(": ")
            .append(formatMessage(record))
            .append(System.getProperty("line.separator"));

        return sb.toString();
    }
}

You get the idea.

This is just an example of how you can handle the output and align it using \t. This is not a perfect solution because you don't know beforehand what's the length of SourceMethodName or SourceClassName or any other output field and only one \t may not be enough to align them all, perfectly.

The solution may be in use as many \t as necessary to cover almost all situations you can think of.

That being said, the perfect solution is to save all output infos and, at the end, calculate how many \t to use depending on the lenght of each field.

EDIT:

Instead of \t you can use StringBuilder together with String.format() to a cleaner and easier to read code:

sb.append(String.Format("[%1$-10s %2$-10s %3$-10s]: %4",
    new Data(record.getMillis(),
    record.getSourceMethodName(),
    record.getSourceClassName(),
    formatMessage(record)
));

Check this page on how to use String.format() to format string into tables.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, that's a solution but can it be achieved with MessageFormat#format? Check this out: download.oracle.com/javase/6/docs/api/java/text/…, java.lang.StringBuffer, java.text.FieldPosition) there is 3rd parameter pos, which description says: On input: an alignment field, if desired. On output: the offsets of the alignment field.
FieldPosition has nothing to do with the output alignment. It's a class to handle/change those {}'s fields and respective positions and formats. MessageFormat class is just another way of doing what you already can do with String.format. Why do you want to use it?
Well I've read somewhere that it can be faster than appending manually data and javadocs for it is not uderstandable. Well just out of curiosity:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.