The newish function Echo is tremendously useful in quick-and-dirty debugging, since you can stick it pretty much anywhere in an expression and get feedback about what's going on. However, like almost everything else in Mathematica (except $Messages on Linux), all of the output it generates is sent straight to STDOUT.
For interactive use/debugging, this is a minor issue. However, a lot of the time I need to debug scripts that are run on the command line using
wolframscript, or even launching them on remote machines using ssh. This means that being able to direct program output to log files, et c. is invaluable. Experimentation suggests changing $Output
doesn't do the trick at all. Here's what happens if you send $Output to a file and use Print:
stream = OpenWrite[];
Block[{$Output = {stream}},
Print["foo"]];
(* note: no output appears in notebook *)
ReadLine@Close@stream
(* "foo" *)
Here's what happens with Echo:
stream = OpenWrite[];
Block[{$Output = {stream}},
Echo["foo"];]
(* note: >> foo appears in notebook *)
ReadLine@Close@stream
(* EndOfFile *)
Note redirecting $Urgent and $Messages doesn't help either. $Echo isn't, despite the promising name, what I'm looking for at all.
Just to check if I might have missed something, I tried to find other stream variables using the following:
Quiet@Select[
Names["System`$*"],
! FreeQ[ToExpression[#, InputForm, OwnValues], _OutputStream] &]
(* {"$Messages", "$Output", "$Urgent"} *)


Echoshould just usePrint. Otherwise, tryBlock[{$Output = {stream}, $Notebooks}, Echo["foo"];]. – ilian Apr 17 '17 at 18:18Echo? If so, check this post: https://mathematica.stackexchange.com/a/99170/1871 – xzczd Apr 18 '17 at 13:41