一开始自己写application的时候要手动创建好几个模块,最近发现了一个好东西basho-rebar,用rebar创建application可以自动生成三个文件:
lqg_app.erl
lqg_sup.erl
lqg.app.src
然后,再添加个人需要的逻辑业务,一个application很快就出炉了。。。。。。
具体做法: ======这里用了basho-lager(erlang的日志应用)来做实例
1.创建应用文件夹(很简单的一步)
mkdir lapp
cd lapp/ wget http://cloud.github.com/downloads/basho/rebar/rebar && chmod u+x rebar
./rebar create-app appid=lager
lqg@lqg:~/lapp$ ./rebar create-app appid=lager ==> lapp (create-app) Writing src/lager.app.src Writing src/lager_app.erl Writing src/lager_sup.erl
在这里,rebar.config的内容如下:
{erl_opts, [debug_info]}. {deps, [ {lager_amqp_backend, ".*", {git, "https://github.com/jbrisbin/lager_amqp_backend.git", "master"}}, {amqp_client, ".*", {git, "https://github.com/jbrisbin/amqp_client.git", {tag,"rabbitmq_2.7.0"}}} ]}.
lqg@lqg:~/lapp$ ./rebar get-deps ==> lapp (get-deps) Pulling lager_amqp_backend from {git,"https://github.com/jbrisbin/lager_amqp_backend.git", "master"} Cloning into 'lager_amqp_backend'... Pulling amqp_client from {git,"https://github.com/jbrisbin/amqp_client.git", {tag,"rabbitmq_2.7.0"}} Cloning into 'amqp_client'... ==> Entering directory `/home/lqg/lapp/deps/lager_amqp_backend' ==> Entering directory `/home/lqg/lapp/deps/amqp_client' ==> amqp_client (get-deps) Pulling rabbit_common from {git,"git://github.com/jbrisbin/rabbit_common.git", {tag,"rabbitmq_2.7.0"}} Cloning into 'rabbit_common'... ==> Entering directory `/home/lqg/lapp/deps/rabbit_common' ==> rabbit_common (get-deps) ==> Leaving directory `/home/lqg/lapp/deps/rabbit_common' ==> Leaving directory `/home/lqg/lapp/deps/amqp_client' ==> lager_amqp_backend (get-deps) Pulling lager from {git,"https://github.com/basho/lager.git",{tag,"0.9.4"}} Cloning into 'lager'... ==> Entering directory `/home/lqg/lapp/deps/lager' ==> lager (get-deps) ==> Leaving directory `/home/lqg/lapp/deps/lager' ==> Leaving directory `/home/lqg/lapp/deps/lager_amqp_backend'
-module(test). -compile([{parse_transform, lager_transform}]). %%lager的编译要求 -export([t/0]). t()-> lager:info("lqg").
7.启动 erl 进行测试
erl -sname lqg -pa ebin/ -pa deps/*/ebin/
添加启动命令参数,把依赖一并包括进来。。。
lqg@lqg:~/lapp$ erl -sname lqg -pa ebin/ -pa deps/*/ebin/ Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9.1 (abort with ^G) (lqg@lqg)1> application:start(compiler). %%此处是lager的依赖应用,要先启动, ok (lqg@lqg)2> application:start(syntax_tools).%%同上 ok (lqg@lqg)3> application:start(lager). ok 16:39:31.020 [info] Application lager started on node lqg@lqg (lqg@lqg)4> test:t(). 16:39:39.491 [info] lqg %%日志输出 ok (lqg@lqg)5>
在回头看看,现在发布一个应用,很容易了=。=
这里顺便说下lager的一些特点
1.内部定义了6个日志等级 debug<info<notice<warning<error<critcal<alert<emergency
2.支持动态设置日志输出等级 lager:set_loglevel/2,set_loglevel/3
3.支持日志汇总 lager_amqp_backend