Formatting logs: alternative to typing many white spaces?

S

Steve

Hi All;

I'm updating a part of an old JSP app that prints/almost prints
columar data into a log. Is there anyway I can get my text to line up
in columns in the log that doesn't involve typing each white space
needed into each string sent to the log.

For example, the log

EVENT 10 white spaces SESSION 10 with spaces USER

Is there an easier way to do it then

String space = " ";

System.out.println("EVENT" + space + "SESSION" + space + "USER");
 
R

Roedy Green

EVENT 10 white spaces SESSION 10 with spaces USER

One method would be to write the log as a CSV file or DataOutputStream
or even a set of serialised objects, possibly GZipped.

Then you write a viewer that displays the log using a JTable with
ASCII export-copy-paste of selected portions.. That would keep the
log compact, which is what I presume is troubling you.
 
A

Andreas Leitgeb

Steve said:
I'm updating a part of an old JSP app that prints/almost prints
columar data into a log. Is there anyway I can get my text to line up
in columns in the log that doesn't involve typing each white space
needed into each string sent to the log.
For example, the log
EVENT 10 white spaces SESSION 10 with spaces USER
Is there an easier way to do it then
String space = " ";
System.out.println("EVENT" + space + "SESSION" + space + "USER");

Depends on what you really want.
Have a look at either System.out.printf or String.format(...)
or java.util.Formatter

With any of these, you can quite concisely pad strings with blanks
to a certain length. This is not exactly what you wrote that you
wanted, but very likely what everybody else would prefer in such
a situation, for writing well-aligned columns of data.

e.g.: String.format("%20s","foo") returns a length 20 string of
17 spaces before the chars 'f', 'o', 'o'. and with "%-20s",
you'll have the spaces afterwards instead. (IIRC)

PS: if you're sure that you need exacty ten spaces, no matter if
the labels ever changed, you could also declare a
static final String tenSpaces=" ";
in one central class and use that in all places.
 
A

Arne Vajhøj

Steve said:
I'm updating a part of an old JSP app that prints/almost prints
columar data into a log. Is there anyway I can get my text to line up
in columns in the log that doesn't involve typing each white space
needed into each string sent to the log.

For example, the log

EVENT 10 white spaces SESSION 10 with spaces USER

Is there an easier way to do it then

String space = " ";

System.out.println("EVENT" + space + "SESSION" + space + "USER");

If you used a logging framework where you could specify
format/patterns, then you would:
- have better control over the formatting
- avoid System.out.println in a web app which is bad

Arne
 
B

beforewisdom

If you used a logging framework where you could specify
format/patterns, then you would:
- have better control over the formatting
- avoid System.out.println in a web app which is bad

Arne

You are forgetting pointy hair bosses who will not spend a dime for 30
seconds of my time to correct something if it works good enough. I
don't have the option to make big changes, just cobble into what is
already there.
 
M

Mark Space

You are forgetting pointy hair bosses who will not spend a dime for 30
seconds of my time to correct something if it works good enough. I
don't have the option to make big changes, just cobble into what is
already there.

And yet they're going to spend far more than 30 seconds of your time for
you to fiddle with a bunch of string constants? And presumably do it
again the next time something changes. Hmmmm...
 
K

Kam-Hung Soh

Depends on what you really want.
Have a look at either System.out.printf or String.format(...)
or java.util.Formatter

.... or System.out.format().
With any of these, you can quite concisely pad strings with blanks
to a certain length. This is not exactly what you wrote that you
wanted, but very likely what everybody else would prefer in such
a situation, for writing well-aligned columns of data.

e.g.: String.format("%20s","foo") returns a length 20 string of
17 spaces before the chars 'f', 'o', 'o'. and with "%-20s",
you'll have the spaces afterwards instead. (IIRC)

PS: if you're sure that you need exacty ten spaces, no matter if
the labels ever changed, you could also declare a
static final String tenSpaces=" ";
in one central class and use that in all places.

Another (more verbose) way to get 10 spaces is to use a "10c" format
specifier and a space character, e.g:

System.out.format("EVENT%10cSESSION%10cUSER\n", ' ', ' ');
 
A

Andreas Leitgeb

Kam-Hung Soh said:
... or System.out.format().

Thanks! I learn something new every day ...
System.out.format("EVENT%10cSESSION%10cUSER\n", ' ', ' ');
or:
System.out.format("EVENT%10cSESSION%<10cUSER\n",' ');

(Another thing I didn't yet know before browsing the docs
once more in the course of writing that response :)
 
K

Kam-Hung Soh

Thanks! I learn something new every day ...


or:
System.out.format("EVENT%10cSESSION%<10cUSER\n",' ');

(Another thing I didn't yet know before browsing the docs
once more in the course of writing that response :)

Thanks, I learnt something new as well! Didn't know about "<" to
refer to the previous format specifier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top