[Erlang 学习笔记] 使用 rebar 创建 application(basho- lager 应用实例)

一开始自己写application的时候要手动创建好几个模块,最近发现了一个好东西basho-rebar,用rebar创建application可以自动生成三个文件:

lqg_app.erl

lqg_sup.erl

lqg.app.src

然后,再添加个人需要的逻辑业务,一个application很快就出炉了。。。。。。

具体做法: ======这里用了basho-lager(erlang的日志应用)来做实例


1.创建应用文件夹(很简单的一步)

mkdir lapp

2.进入创建好的应用文件夹,下载 rebar 执行文件到此处

cd lapp/
wget http://cloud.github.com/downloads/basho/rebar/rebar && chmod u+x rebar

3.使用rebar创建application

./rebar create-app appid=lager

这是能看到 在路径 lapp/src/下生成了三个文件

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

4。由于lager有依赖的应用,所以要把以来解决,这里可以通过 rebar的配置文件去把依赖自动下载补全,当然前提是写好了配置文件 rebar.config

在这里,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"}}}
]}.

接着,执行 ./rebar get-deps 把依赖下载回来,路径是  lapp/deps/*

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'

5.这时候基本完成了,差个测试代码,随便写个  test.erl

-module(test).

-compile([{parse_transform, lager_transform}]).  %%lager的编译要求

-export([t/0]).

t()->
    lager:info("lqg").

6.都准备好之后就要编译了,这里编译不使用不需要makefile了,直接用rebar来执行    ./rebar compile  

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




你可能感兴趣的:(erlang,git,测试,application,compiler,makefile)