Do You know any neat solution to log an InputStream, can by into file. InputStream from a Process never ends and generate some stream from time to time. Thanks Bartek
Asked
Active
Viewed 3,090 times
1
-
Can you please be specific? Are you trying to write to a log file in order to store information when an error occurs? Please include some background information about the project if you can. – Daniel Lopez Nov 19 '12 at 16:22
-
2Are you looking for something like Apache `log4j`? It's a library that allows you to write data from your application to a log file. More details here: http://logging.apache.org/log4j/1.2/ – svz Nov 19 '12 at 16:27
-
Are you trying to record STDOUT and STDIN in a file? – Daniel Lopez Nov 19 '12 at 16:28
-
the bast way will be to use log4j somehow :) There is a simple bash script which periodically prints out on the screen something, lets say every 5 minutes a date+time, process never ends. Lunched by `Process p = Runtime.getRuntime().exec(command)` then InputStream from this process needs to be stored into a file – user1806952 Nov 19 '12 at 16:41
-
From 1.7 on, if you are using ProcessBuilder you can redirect IO Streams easily using ProcessBuilder().inheritIO(). [see this question](http://stackoverflow.com/questions/14165517/processbuilder-forwarding-stdout-and-stderr-of-started-processes-without-blocki) – simo.3792 Oct 17 '14 at 07:14
-
this question is a duplicate of https://stackoverflow.com/questions/16434699 – squiddle Feb 23 '17 at 22:56
2 Answers
0
If you want to log never-ending stream transparently - I believe you need a Tee filter - see answers in this question. E.g. you can use Commons IO.

Community
- 1
- 1

Konstantin V. Salikhov
- 4,554
- 2
- 35
- 48
0
For you case is the most sutable will be log4j
.
But you can redirect System.err
and System.out
into console or smth else and it'll be write all errors in specific stream.
upd:
# Root logger option log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\\loging.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L
- %m%n
# Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

Dmitry Zagorulkin
- 8,370
- 4
- 37
- 60
-
Do You have any proposal how to redirect InputStream into a file using log4j? – user1806952 Nov 21 '12 at 13:00
-
ok, here is system.out redirected but how to redirect different InputStream (for example Process), can be programmatically. – user1806952 Nov 29 '12 at 12:29