This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Getting all data in the consol and dumping it into a text file  RSS feed

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heya,

I'm trying to copy all the data from written to the console (incl errors, system.outs.. est) and write it to a text file. I know how to write it, but i have no clue how to get the data out of the actual console(Not the log file). Any ideas how this can be done?

Thanks
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the following link.

Hope it helps.
 
Lila Fowler
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but I'm a bit confused, if i'm correct that's basically creating a log file? We have a log file being created and updated constantly(deals with server side), but we also have a console(deals with client side) Some errors that appear in the console don't necessarily appear in the log file and visa versa. I only need to write the console if an exception occurs, i don't need to write it or constantly update it if that makes any sense.

Thanks
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm ... in that case I guess you only need to write err stream, right?
Try the following example: redirect.

Does this help?
If not, what about this one?

Perhaps I just don't understand what you're trying to do. ;)
 
Lila Fowler
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
example: redirect --> seems to be almost exactly what i need, although i need to write the complete console if an exception occurs kind of like copy and paste. The other thing i'm not really getting is where would i implement that code? (sorry for the stupid question ) But the console is a default function in java, what part is actaully accessing the original console data? Would then contain the original data?

Thanks
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the second link ... I guess it's the answer to:

Lila Fowler wrote:Some errors that appear in the console don't necessarily appear in the log


When redirecting the console output to a file, you have to use "2>&1".
Otherwise, you'll only see standard output ...

Lili Fowler wrote:But the console is a default function in java, what part is actually accessing the original console data?Would

1. orgStream = System.out;

then contain the original data?


Well, printing to console output is indeed the default operation of stdin, stdout & stderr.
However with System.setXXX(...), you have the possibility to redirect it to e.g. a file.
So in above case it only contains the ref to the System.out, which is later used to restore the printing back to the console.


Regards,
Rok
 
Marshal
Posts: 60083
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No longer a "beginning" question. Moving thread.
 
Lila Fowler
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, sorry it took me so long to reply but I was moved to to other tickets.

Here is my code, but it still doesn't write the exception to the file, and the things that do write to the file don't display in the console. I need the .txt file to be a mirror image of the console. Any ideas?

Thanks.

Here is my current code.

 
Sheriff
Posts: 21452
95
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That little hello is printed before the exception. That's because the control flow is like this:
- get a reference to the old streams (lines 3-4)
- set up the new streams (lines 5-7)
- test the redirect (lines 8-9)
- throw a RuntimeException (line 10)
- run the finally block to restore the old streams (lines 29-30)
- exit the main method with the RuntimeException (JVM internals)
- print the RuntimeException to the default, restored standard error stream (JVM internals)

If you want to write everything to your custom print stream simply don't call lines 29 and 30. If the JVM exits it will restore the default output and error streams and close all file handles opened from that Java application for you.
 
Lila Fowler
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that writes everything to my file as you said it would(took out lines 29 & 30), but i still need it in my console. Is there a way for the data to be printed in my both in the console as well as the .txt?
Thanks
 
Rob Spoor
Sheriff
Posts: 21452
95
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create or find something called a "tee" output stream (named after the Unix tool "tee"). This basically duplicates data written to it. Apache Commons IO has such a class.

How it would work:
From now on everything written to either System.out or System.err will be written to both the original System.out and the file.
 
Lila Fowler
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks its now writing to both the file & the console, but the text has lost its color & functionality. eg if you had an exception it would be printed in red, similarly with System.err I understand its because you write it to the file and then to the console, but is it maybe possible to let it write to the console first & then to the file? Or a way to check the color and replace it when it is rewritten? I got it to display just red but not both black & red as it would originally write to the console.

Thanks
 
Rob Spoor
Sheriff
Posts: 21452
95
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The console does not use colors. It is your IDE (Eclipse, Netbeans, ...) that adds these colors for System.err.
 
Lila Fowler
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, in that case i think i'm done. Thanks a million
 
Rob Spoor
Sheriff
Posts: 21452
95
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!