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
You can create you own
java.util.logging.Formatter
. For that you can extend it this way: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 withString.format()
to a cleaner and easier to read code:Check this page on how to use
String.format()
to format string into tables.