Is there a way to redirect data printed by io:format()
from erlang shell into a file ? I know that I can open a file (IoDevice) and write data directly into it, but it requires code change and I don't want to do now.
When a process calls io:format()
and similar functions, the process sends io request messages to its group_leader process. So a simple hack is to open a file, and set it as the group_leader of the processes producing the output. Here is an example of redirecting the shell process's output to a file.
1> {ok, F} = file:open("z", [write]).
{ok,<0.36.0>}
2> group_leader(F, self()).
3> io:format("Where am I going to appear?~n").
4>
This will only redirect the current shell process, so you will have to set the group_leader for all processes which you would like to redirect to the file.
The solution can be refined of course, for example by spawning a server process which proxies io request messages to rotated files, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With