Log4Erl 使用小记

from:http://www.hezhiqiang.info/articles/84/log4erl-a-port-of-java-log4j
功能

支持多日志
当前文件Appender仅支持基于大小的日志文件滚动
支持默认Logger,未指定Logger时系统提供默认Logger
5个预定义的日志级别(debug, info, warn, error, fatal)
一个error_logger的日志处理器
支持用户指定日志级别
支持日志格式化
支持控制台日志
支持smtp formatter
支持XML格式的日志
支持syslog
支持在运行时改变Appender的格式和级别.

Step1. Checkout from repository

你可以从 Google code 或者 Github上获取Log4erl的源代码, Google code上的代码已经旧了,建议从Github上clone 代码

git clone git://github.com/ahmednawras/log4erl.git log4erl

Step2. Compile

如图4, 进入Log4erl源代码目录src执行:

make:all([{outdir, "../ebin"}]).

Step3. 安装

你要让Erlang能够找到Log4erl,两种方式

1. 把整个Log4erl目录复制到$ERLANG_HOME/lib目录下面.看上面第三张图.
2. 命令行指定

erl -pz /path/to/log4erl

Step4. 使用

application:start(log4erl).

创建配置文件并调用log4erl:conf(file)初始化

log4erl:conf(“priv/log4erl.conf”).

同样你可以用编程的方式对Log4erl进行配置

log4erl:add_logger(messages_log).
log4erl:add_console_appender(messages_log, cmd_logs, {warn, “[%L] %l%n”}).

好了,现在可以使用它了.

log4erl:info(“Information message”).

关于Log4erl的详细信息请查阅源代码根目录中的 Readme.txt, API.txt, Appenders_API.txt, CONFIGURATION.txt 文件.
Log4erl配置文件格式

logger [] {

}

指定了Logger的名称,你可以去任意你喜欢的名字.如果不指定任何名字,那么log4erl将把它作为默认的Logger使用,例如:

%% default logger
logger {

}

在一个Logger中,可以有一到多个Appender,例如

%% Default logger
%% it includes a file appender and a console appender
logger{
file_appender app2{
dir = “.”,
level = info,
file = my_app,
type = size,
max = 100000,
suffix = log,
rotation = 5,
format = ‘[%L] %I %l%n’
}
console_appender app1{
level = warn,
format = ‘%T %j [%L] %l%n’
}
}

Appender的配置格式如下

{

}

在Appender中你可以用’property=value’的格式来设置Appender的属性,属性剑以’,‘逗号分隔.每种Appender有不同的属性集合.

公共属性:
level = => 日志级别 (例如: warn)
format = => 输出格式 (查看 ‘Appenders.txt’)

file_appender

dir => 输出路径 (例如: /var/log/my_app)
file => 日志文件名称 (例如: my_app_log)
type => size,time. 当前仅实现了基于size的日志滚动
max => 每次日志滚动的最大文件大小
suffix => 日志文件后缀 (例如: log)
rotation => 循环滚动次数,例如为5, 当滚动到第五个日志文件并且日志文件达到指定size的时候就会覆盖前面的日志文件,依次循环

smtp_appender

ip => SMTP服务器IP地址
port => SMTP服务器端口
no_auth
username => 用户名
password => 密码
from => 寄信人地址
to => 收信人地址
tilte => 邮件标题
msg => 邮件内容

syslog_appender

facility => Facility (例如: ftp)
host => 发送syslog消息的目标主机 [可选]
port => syslog 端口[可选]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用不惯sasl的,可以用log4xxx的erlang版,log4erllog4erl Manual: =============== TOC: ==== 1. Features 2. Installation 3. Usage 4. API 5. Configuration 6. Known issues 7. Future development 8. License 1. FEATURES: ============ - Multiple logs - Currently, only size-based log rotation of files for file appender - Support default logger if no logger specified - 5 predifined log levels (debug, info, warn, error, fatal) - A log handler for error_logger - Support for user-specified log levels - Support for a log formatter (similar to Layouts in Log4J) - Support for console log - Support for smtp formatter - Support for XML logs - Support for syslog - Support for changing format and level of appender during run-time 2. INSTALLATION: ================ To compile & install log4erl, download source from google code's website (http://code.google.com/p/log4erl/) or from svn: $> svn checkout http://log4erl.googlecode.com/svn/trunk/ log4erl or from github public repository (http://github.com/ahmednawras/log4erl/). $> cd log4erl $> make or you can run the below from erlang shell: $> cd src $> erl 1> make:all([{outdir, "../ebin"}]). 3. USAGE: ========= 1- In order to use log4erl, you need to first include it in the path. There are 2 ways to do this: a) include the "log4erl" directory in erlang's "lib" directory in the target machine (cp -Rf log4erl /where/erlang/is/lib). $> cp -Rf log4erl /usr/local/lib/erlang/lib/ b) include the "log4erl" ebin directory in the path when running you program $> erl -pz /path/to/log4erl ... 2- Once the log4erl directory is included, you can use its API as described in section "API". but before, you need to run: > application:start(log4erl). 3- Create a configuration file and load it using log4erl:conf(File) > log4erl:conf("priv/log4erl.conf"). 4- Alternatevly, you can create loggers & add appenders to them programmatically as appropriate. You can do this as per the API below. > log4erl:add_logger(messages_log). > log4erl:add_console_appender(messages_log, cmd_logs, {warn, "[%L] %l%n"}). where Conf is the erlang term describing how the log is to be handled You can also add more types of appenders, which is explained in API.txt and Appneders_API.txt. 5- Now, you can log whatever messages you like as per logging functions described in API. > log4erl:info("Information message"). Precedance of log levels are: all = debug < info < warn < error < fatal < none User defined levels are always written except when none level is specified in the logger specification (See below). 4. API: ======= Please look at API.txt file for more information about the API. 5. CONFIGURATION: ================= Please look at CONFIGURATION.txt for more information about how to configure log4erl. 6. KNOWN ISSUES: ================ - Name of both loggers & appenders should be unique and not registered since log4erl will try and register their names. If the name is already registered, nothing will happen. This will be fixed soon. - If you run change_log_format/1,2 and appender crashed, a restart from the supervisor will not record the latest format used. It will only use either the default format or the format used in the argument is supplied. 7. FUTURE DEVELOPMENT: ====================== - Add support for different log persistance methods (e.g files, XML, console, DB, SNMP, syslog...etc) - Add support for time-based log rotation - Multiple configuration format (Erlang terms, XML?, properties files?) - Add support for NDC & MDC ??? Please send your suggestion to ahmed.nawras <at @ at> gmail <dot . dot> com 8. LICENSE: =========== This software is subject to "Mozilla Public License 1.1". You can find the license terms in the file 'LICENSE.txt' shipping along with the source code. You may also get a copy of the license term from the URL: "http://www.mozilla.org/MPL/MPL-1.1.html".

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值