I have a clojure program with which I would like all stdout and stderr
to be written to a file.

Of course there is Unix IO redirection but I am starting my program
from within another clojure program using "(.exec (Runtime/getRuntime)
clj-program-command env)" and that doesn't support IO redirection. Of
course I could use the "bash -c" command within the command to start
my program but it does make things a bit messier with all the quotes
and escaping of quotes, and anyway I would like to achieve this
objective within my clojure program.

I found out that System/out and System/err are separate and different
than *out* and *err*. Changing *out* or *err* does not change System/
out or System/err. My program includes the Jetty server which is
written in Java so it writes to System/out and System/err and not to
*out* and *err*. So to change all stdout and stderr to write to a file
I need to change System/out, System/err and *out* and *err*.

I can do this like so in my core.clj file:

(System/setOut (PrintStream. (FileOutputStream. "/home/user/myproject/
log.txt" true) true))
(System/setErr (PrintStream. (FileOutputStream. "/home/user/myproject/
log.txt" true) true))

(binding [*out* (java.io.PrintWriter. System/out)
             *err*  (java.io.PrintWriter. System/err)]
  (start-my-app))

The only way I know of changing *out* and *err* is to wrap my entire
program inside the "binding" function.

I was wondering what is thought of the idea of making *out* and *err*
dynamic vars? If they were dynamic I could do the following which I
think is nicer and more elegant:

(System/setOut (PrintStream. (FileOutputStream. "/home/user/myproject/
log.txt" true) true))
(System/setErr (PrintStream. (FileOutputStream. "/home/user/myproject/
log.txt" true) true))
(def *out* (java.io.PrintWriter. System/out))
(def *err* (java.io.PrintWriter. System/err))

Better yet, it would be nice if I changed *out* and *err* and that
caused System/out and System/err to change too. So I could do:

(def *out* (java.io.PrintWriter. (FileOutputStream. "/home/user/
myproject/log.txt" true))
(def *err* (java.io.PrintWriter. (FileOutputStream. "/home/user/
myproject/log.txt" true))

And System/out and System/err would also change.

Thoughts?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to