为什么外部格式化disk_log错误?

时间:2014-05-10 06:17:28

标签: erlang

我尝试使用disk_log模块。

format = internal没问题,format = external会产生错误消息

-module(abiud_log_test).
-compile([{parse_transform, lager_transform}]).

-export([log_test/0]).

log_test()->
    File = "/tmp/test1.log",
    Cmd = "rm " ++ File,
    os:cmd(Cmd),
    Log = 'equipment',
    A =  [{name,Log},{file,File},{type,halt},{format,external},{mode,read_write}],
    {ok,B} = disk_log:open(A),
    lager:debug("abiud_log_test_log_test_1:~p",[B]),
    ok = disk_log:log(B,"aaa"),
    ok = disk_log:log(B,"aaa_1"),
    ok = disk_log:close(B),
    ok.

测试结果如下:

(emacs@yus-iMac.local)74> abiud_log_test:log_test().
* 3: syntax error before: abiud_log_test
(emacs@yus-iMac.local)74> abiud_log_test:log_test().
2014-05-10 14:02:52.234 [debug] |abiud|abiud_log_test|log_test|13|Undefined|Undefined|Undefined|Undefined|Undefined|Undefined|Undefined|abiud_log_test_log_test_1:equipment
** exception error: no match of right hand side value 
                    {error,{format_external,equipment}}
     in function  abiud_log_test:log_test/0 (src/abiud_log_test.erl, line 14)

format = internal

-module(abiud_log_test).
-compile([{parse_transform, lager_transform}]).

-export([log_test/0]).

log_test()->
    File = "/tmp/test1.log",
    Cmd = "rm " ++ File,
    os:cmd(Cmd),
    Log = 'equipment',
    A =  [{name,Log},{file,File},{type,halt},{format,internal},{mode,read_write}],
    {ok,B} = disk_log:open(A),
    lager:debug("abiud_log_test_log_test_1:~p",[B]),
    ok = disk_log:log(B,"aaa"),
    ok = disk_log:log(B,"aaa_1"),
    ok = disk_log:close(B),
    ok.

结果如下:

(emacs@yus-iMac.local)75> abiud_log_test:log_test().
2014-05-10 14:15:57.913 [debug] |abiud|abiud_log_test|log_test|13|Undefined|Undefined|Undefined|Undefined|Undefined|Undefined|Undefined|abiud_log_test_log_test_1:equipment
ok

1 个答案:

答案 0 :(得分:0)

对于外部格式,您应该使用disk_log:blog / 2,因为disk_log文档在http://www.erlang.org/doc/man/disk_log.html#log-2说:" log / 2函数用于内部格式化日志,blog / 2用于外部格式化的日志"

请注意,文档也说"没有检查以确保这一点,这完全是调用者的责任。"。