From 23a94782d7d62714c19854fcc234b887671d24dd Mon Sep 17 00:00:00 2001 From: Maxim Treskin Date: Thu, 14 Oct 2010 01:31:00 +0700 Subject: [PATCH 1/2] Add gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3594e15 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.beam +*~ From d7cf81f57701375e721c290f64fe76ebb98f1865 Mon Sep 17 00:00:00 2001 From: Maxim Treskin Date: Thu, 14 Oct 2010 01:33:47 +0700 Subject: [PATCH 2/2] Add http_appender. It send logs to http-server specified by URI parameter --- ebin/log4erl.app | 1 + include/log4erl.hrl | 4 ++- priv/log4erl.conf | 10 ++++++ src/http_appender.erl | 76 +++++++++++++++++++++++++++++++++++++++++++ src/log4erl.erl | 9 ++++- 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/http_appender.erl diff --git a/ebin/log4erl.app b/ebin/log4erl.app index e64a057..c14bef3 100644 --- a/ebin/log4erl.app +++ b/ebin/log4erl.app @@ -23,6 +23,7 @@ smtp_appender, smtp_fsm, syslog_appender, + http_appender, xml_appender]}, {registered,[log4erl]}, {applications, [kernel,stdlib]}, diff --git a/include/log4erl.hrl b/include/log4erl.hrl index e717cb5..79a31f9 100644 --- a/include/log4erl.hrl +++ b/include/log4erl.hrl @@ -27,7 +27,7 @@ %% max = seconds (for time) | or kiloBytes (for size) -record(log_type,{type, max, timer}). -%% file_name = the name of the file without counter +%% file_name = the name of the file without counter %% fd = the descriptior for the file %% counter = current counter, used for appending to file_name in case of rotation %% log_type is a log_type record @@ -49,5 +49,7 @@ -record(syslog_appender, {level=?DEFAULT_LEVEL, facility=user,host,port=415, socket, format=?DEFAULT_FORMAT}). +-record(http_appender, {level=?DEFAULT_LEVEL, format=?DEFAULT_FORMAT, uri, timeout = 100}). + %% log record -record(log, {level, msg, data, time, millis}). diff --git a/priv/log4erl.conf b/priv/log4erl.conf index 814354f..bb22e19 100644 --- a/priv/log4erl.conf +++ b/priv/log4erl.conf @@ -60,3 +60,13 @@ logger syslog{ } } +%% A logger with 1 http appender +logger http{ + http_appender app5{ + level = all, + uri = "http://localhost:9999/app5", + timeout = 100, + format = "%l%n" + } +} + diff --git a/src/http_appender.erl b/src/http_appender.erl new file mode 100644 index 0000000..131d09a --- /dev/null +++ b/src/http_appender.erl @@ -0,0 +1,76 @@ +-module(http_appender). + +-include("../include/log4erl.hrl"). + +-behaviour(gen_event). +%% gen_event callbacks +-export([init/1, handle_event/2, handle_call/2, + handle_info/2, terminate/2, code_change/3]). + +-define(DEFAULT_URI, "http://localhost/log"). +-define(DEFAULT_TIMEOUT, 100). + +init({conf, Conf}) -> + Level = proplists:get_value(level, Conf, all), + Uri = proplists:get_value(uri, Conf, ?DEFAULT_URI), + Timeout = proplists:get_value(timeout, Conf, ?DEFAULT_TIMEOUT), + Format = proplists:get_value(format, Conf, ?DEFAULT_FORMAT), + {ok, Form} = log_formatter:parse(Format), + State = #http_appender{ + level = Level, + format = Form, + uri = Uri, + timeout = Timeout + }, + {ok, State}. + +handle_event({change_level, Level}, State) -> + State2 = State#http_appender{level = Level}, + ?LOG2("Changed level to ~p~n",[Level]), + {ok, State2}; +handle_event({log,LLog}, State) -> + ?LOG2("handl_event:log = ~p~n",[LLog]), + do_log(LLog, State), + {ok, State}. + +handle_call({change_format, Format}, State) -> + ?LOG2("Old State in console_appender is ~p~n",[State]), + {ok, Tokens} = log_formatter:parse(Format), + ?LOG2("Adding format of ~p~n",[Tokens]), + S2 = State#http_appender{format=Tokens}, + {ok, ok, S2}; +handle_call({change_level, Level}, State) -> + State2 = State#http_appender{level = Level}, + ?LOG2("Changed level to ~p~n",[Level]), + {ok, ok, State2}; +handle_call(_Request, State) -> + {ok, ok, State}. + +handle_info(_Info, State) -> + {ok, State}. + +terminate(_Reason, _S) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +do_log(#log{level = L} = Log, #http_appender{level=Level} = State) -> + ToLog = log4erl_utils:to_log(L, Level), + case ToLog of + true -> + Uri = State#http_appender.uri, + Timeout = State#http_appender.timeout, + Format = State#http_appender.format, + Msg = log_formatter:format(Log, Format), + Who = node(), + do_send(Uri, Timeout, {Who, L, Msg}); + false -> + ok + end. + +do_send(Uri, Timeout, {Who, Level, Msg}) + when is_atom(Who), is_atom(Level) -> + httpc:request(post, + {Uri, [], "text/plain", io_lib:format("~p: ~p", [Who, Msg])}, + [{timeout, Timeout}], []). diff --git a/src/log4erl.erl b/src/log4erl.erl index daf6684..78cc027 100644 --- a/src/log4erl.erl +++ b/src/log4erl.erl @@ -18,6 +18,7 @@ -export([add_syslog_appender/2, add_syslog_appender/3]). -export([add_xml_appender/2, add_xml_appender/3]). -export([add_dummy_appender/2, add_dummy_appender/3]). +-export([add_http_appender/2, add_http_appender/3]). -export([get_appenders/0, get_appenders/1]). -export([change_format/2, change_format/3]). -export([error_logger_handler/0, error_logger_handler/1]). @@ -54,7 +55,7 @@ add_appender(Appender, Conf) -> %% Appender = {Appender, Name} add_appender(Logger, Appender, Conf) -> try_msg({add_appender, Logger, Appender, Conf}). - + add_console_appender(AName, Conf) -> add_appender(?DEFAULT_LOGGER, {console_appender, AName}, Conf). @@ -85,6 +86,12 @@ add_xml_appender(Name, Conf) -> add_xml_appender(Logger, Name, Conf) -> add_appender(Logger, {xml_appender, Name}, Conf). +add_http_appender(Name, Conf) -> + add_appender(?DEFAULT_LOGGER, {http_appender, Name}, Conf). + +add_http_appender(Logger, Name, Conf) -> + add_appender(Logger, {http_appender, Name}, Conf). + add_dummy_appender(AName, Conf) -> add_appender(?DEFAULT_LOGGER, {dummy_appender, AName}, Conf).