配置应用
配置应用
可以使用配置参数 来对应用进行配置。它们在 .app 文件中是一个由键 env 指定的 {Par,Val} 元组列表。
{application, ch_app,
[{description, "Channel allocator"},
{vsn, "1"},
{modules, [ch_app, ch_sup, ch3]},
{registered, [ch3]},
{applications, [kernel, stdlib, sasl]},
{mod, {ch_app,[]}},
{env, [{file, "/usr/local/log"}]}
]}.
Par 必须是一个原子, Val 可以是任意值。应用可以通过调用 application:get_env(App,Par) 或一些其他类似函数来获取配置参数的值,参见 application(3) 。
例如:
% erl
Erlang (BEAM) emulator version 5.2.3.6 [hipe] [threads:0]
Eshell V5.2.3.6 (abort with ^G)
1> application:start(ch_app).
ok
2> application:get_env(ch_app, file).
{ok,"/usr/local/log"}
.app 文件中的值可以被系统配置文件 中的值所覆盖。系统配置文件是一个包含相关应用的配置参数的文件。
[{Application1, [{Par11,Val11},...]},
...,
{ApplicationN, [{ParN1,ValN1},...]}].
系统配置要被命名为 Name.config 并且要使用命令行参数 -configName 来启动Erlang。更多信息参见 config(4) 。
例如:创建一个文件 test.config 包含一下内容:
[{ch_app, [{file, "testlog"}]}].
file 的值将覆盖在 .app 文件中所定义的 file 的值:
% erl -config test
Erlang (BEAM) emulator version 5.2.3.6 [hipe] [threads:0]
Eshell V5.2.3.6 (abort with ^G)
1> application:start(ch_app).
ok
2> application:get_env(ch_app, file).
{ok,"testlog"}
如果使用了 发布处理 ,那么只能使用一个系统配置文件同时该文件必须叫做 sys.config 。
在 .app 文件中的值,也包括系统配置文件中的值,都可以直接在命令行中被覆盖:
% erl -ApplName Par1 Val1 ... ParN ValN
例如:
% erl -ch_app file '"testlog"'
Erlang (BEAM) emulator version 5.2.3.6 [hipe] [threads:0]
Eshell V5.2.3.6 (abort with ^G)
1> application:start(ch_app).
ok
2> application:get_env(ch_app, file).
{ok,"testlog"}
