Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP logging #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.beam
*~
1 change: 1 addition & 0 deletions ebin/log4erl.app
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
smtp_appender,
smtp_fsm,
syslog_appender,
http_appender,
xml_appender]},
{registered,[log4erl]},
{applications, [kernel,stdlib]},
Expand Down
4 changes: 3 additions & 1 deletion include/log4erl.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}).
10 changes: 10 additions & 0 deletions priv/log4erl.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

76 changes: 76 additions & 0 deletions src/http_appender.erl
Original file line number Diff line number Diff line change
@@ -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}], []).
9 changes: 8 additions & 1 deletion src/log4erl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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]).
Expand Down Expand Up @@ -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).

Expand Down Expand Up @@ -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).

Expand Down