Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect output from erlang shell into a file

Tags:

erlang

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.

like image 313
Konstantin Avatar asked Jan 26 '10 16:01

Konstantin


1 Answers

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.

like image 123
Zed Avatar answered Sep 23 '22 07:09

Zed