为什么erlang io:format输出丢失?我需要做什么来恢复它?

为什么erlang io:format输出丢失?我需要做什么来恢复它?,erlang,format,gen-server,Erlang,Format,Gen Server,我正在写一个gen_服务器,我们称之为gen_服务器,没有什么特别之处。它使用的库(emysql)在尝试查找数据库服务器(在gen_server_db:init()中)时可能会遇到连接故障。当我捕获异常并尝试将某些内容打印到控制台时,我得到了zip。在下面的代码示例中,io:format消息都无法到达控制台。我似乎记得很久以前听到过这样的原因,但我想不起为什么 init(Args) -> % I've condensed the way I actually get this st

我正在写一个gen_服务器,我们称之为gen_服务器,没有什么特别之处。它使用的库(emysql)在尝试查找数据库服务器(在gen_server_db:init()中)时可能会遇到连接故障。当我捕获异常并尝试将某些内容打印到控制台时,我得到了zip。在下面的代码示例中,io:format消息都无法到达控制台。我似乎记得很久以前听到过这样的原因,但我想不起为什么

init(Args) ->
    % I've condensed the way I actually get this stuff to one line, but if I have a
    % database online I connect properly, so I know that I'm getting the Host, User, etc.  
    {Host, User, Password, DB, PoolSize} = application:get_env(gen_server_db, config),

    init_mysql_connection(gen_server_db_pool, PoolSize, User, Password, Host, DB),

    % When the net connection to the db is down, I never get here.
    ok.

init_mysql_connection(bgo_db_pool, PoolSize, User, Password, Host, DB) ->
    try
        emysql:add_pool(bgo_db_pool, PoolSize, User, Password, Host, 3306, DB, utf8)
    catch
        exit:failed_to_connect_to_database ->
            io:format("Cannot connect to the mysql database server.  Retrying in 1 sec.~n"),
            timer:sleep(1000),
            init_mysql_connection(bgo_db_pool, PoolSize, User, Password, Host, DB);
        Error:Reason ->
            io:format("Database connection error: ~p:~p~n", [Error, Reason]),
            1/0
    end.

通过io:format发送的所有I/O将发送到正在运行的进程的当前端口。然后,该流程负责使用io:fwrite打印消息


如果您只想在编码时转储一些调试信息,可以使用erlang:display/1

我建议您使用error\u logger模块而不是io:format。io:format将消息输出到tty控制台,这是一种标准输出。如果gen_服务器由控制台启动,您将看到这些消息,但是,如果服务器以分离模式启动,您将永远看不到这些消息

这是一本手册,您需要启动SASL

http://www.erlang.org/doc/man/error_logger.html
在配置文件中,您需要定义此日志的输出文件

....
{sasl, [{sasl_error_logger, {file,"/tmp/sasl_error.log"}},{errlog_type, all}]}
....

看起来组长重定向并没有发生,所以看不到任何输出的一个可能原因是代码实际上并没有做到这一点。您是否尝试过在init(Args)中调用io:format()作为第一件事来说服自己init代码可以运行?另外,尝试启动SASL以在gen_服务器死机时获取崩溃报告。最后,我在控制台上使用了全局:register_name(io_format_target,group_leader())和io:format(全局:whereis_name(io_format_target),“foo~p~n”,以及[bar])。