File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes System.setOut in a custom format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.setOut in a custom format" Watch "System.setOut in a custom format" New topic

System.setOut in a custom format

Maikel Geelen

Joined: Apr 09, 2013
Posts: 5
Is it possible to create a custom format for System.setOut.
I want to write everything that comes in the console to a file. So far i'm able to write to a file but is it possible to put it in a format so i have a more organized log file.

This is what i have to write to the file.

This is my formatter, i've used it before with a filehandler.

Is it possible to use this one here too or is it even possible to create another layout for System.setOut
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
Welcome to the Ranch

I presume that the PrintStream you are using supports the format() and printf() methods, so you can use that format String with those methods.
Maikel Geelen

Joined: Apr 09, 2013
Posts: 5
I don't think i would be able to create my layout then.

at the moment my lines in the log file look like this.

And i would like it to look like this

Am i able to use my System.setOut(out) with a FileHandler so i could use TextFormatter
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

So then just changing where the output goes isn't going to be a solution at all. You actually want to intercept the output and reformat it.

But frankly I wouldn't do that. I would just go through the existing code, find all of the places which use System.out, and modify them to use a proper logging system. (Of which there are plenty, readily available.) Otherwise you'll find yourself writing your own logging system, and rewriting basic Java components is a programming sin.
I agree. Here's the link:
subject: System.setOut in a custom format
It's not a secret anymore!