From 9393f4d957b1054c562da00d8c3d467d88b13a84 Mon Sep 17 00:00:00 2001 From: Robert Newson Date: Wed, 19 Aug 2015 00:16:30 +0100 Subject: [PATCH] Allow facility override in options --- src/twig.erl | 21 +++++++++++++++------ src/twig_event_handler.erl | 22 +++++++++++++++------- src/twig_int.hrl | 2 +- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/twig.erl b/src/twig.erl index 488dd28..affea40 100644 --- a/src/twig.erl +++ b/src/twig.erl @@ -54,29 +54,38 @@ log(LevelAtom, String) -> log(LevelAtom, Format, Data) -> log(LevelAtom, Format, Data, []). -log(LevelAtom, Format, Data, _Options) -> +log(LevelAtom, Format, Data, Options) -> %% TODO do something useful with options Level = twig_util:level(LevelAtom), + Facility = get_facility(Options), case application:get_env(twig, level) of {ok, Threshold} when Level =< Threshold -> - send_message(Level, Format, Data); + send_message(Level, Facility, Format, Data); undefined when Level =< ?LEVEL_INFO -> - send_message(Level, Format, Data); + send_message(Level, Facility, Format, Data); _ -> ok end. %% internal -send_message(Level, Format, Data) -> - gen_event:sync_notify(error_logger, format(Level, Format, Data)). +send_message(Level, Facility, Format, Data) -> + gen_event:sync_notify(error_logger, format(Level, Facility, Format, Data)). -format(Level, Format, Data) -> +format(Level, Facility, Format, Data) -> %% TODO truncate large messages #twig{ level = Level, + facility = Facility, msg = iolist_to_binary(twig_util:format(Format, Data)), msgid = erlang:get(nonce), pid = self() }. +get_facility(Options) -> + case proplists:get_value(facility, Options) of + undefined -> + undefined; + Facility -> + twig_util:facility(Facility) + end. diff --git a/src/twig_event_handler.erl b/src/twig_event_handler.erl index d7b691b..baa0f65 100644 --- a/src/twig_event_handler.erl +++ b/src/twig_event_handler.erl @@ -37,8 +37,12 @@ init([]) -> {ok, ok, State} = handle_call(load_config, #state{socket=Socket}), {ok, State}. -handle_event(#twig{level=Level, msgid=MsgId, msg=Msg, pid=Pid}, State) -> - write(Level, MsgId, Msg, Pid, State), +handle_event(#twig{level=Level, facility=undefined, msgid=MsgId, msg=Msg, pid=Pid}, + #state{facility = Facility} = State) -> + write(Level, Facility, MsgId, Msg, Pid, State), + {ok, State}; +handle_event(#twig{level=Level, facility=Facility, msgid=MsgId, msg=Msg, pid=Pid}, State) -> + write(Level, Facility, MsgId, Msg, Pid, State), {ok, State}; % OTP standard events @@ -90,12 +94,16 @@ terminate(_Reason, State) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. -write(Level, undefined, Msg, Pid, State) -> - write(Level, "--------", Msg, Pid, State); -write(Level, MsgId, Msg, Pid, State) when is_list(Msg); is_binary(Msg) -> - #state{facility=Facil, appid=App, hostname=Hostname, host=Host, port=Port, +write(Level, MsgId, Msg, Pid, State) -> + #state{facility = Facility} = State, + write(Level, Facility, MsgId, Msg, Pid, State). + +write(Level, Facility, undefined, Msg, Pid, State) -> + write(Level, Facility, "--------", Msg, Pid, State); +write(Level, Facility, MsgId, Msg, Pid, State) when is_list(Msg); is_binary(Msg) -> + #state{appid=App, hostname=Hostname, host=Host, port=Port, socket=Socket} = State, - Pre = io_lib:format("<~B>~B ~s ~s ~s ~p ~s - ", [Facil bor Level, + Pre = io_lib:format("<~B>~B ~s ~s ~s ~p ~s - ", [Facility bor Level, ?SYSLOG_VERSION, twig_util:iso8601_timestamp(), Hostname, App, Pid, MsgId]), send(Socket, Host, Port, [Pre, Msg, $\n]). diff --git a/src/twig_int.hrl b/src/twig_int.hrl index 81ef8b5..82091af 100644 --- a/src/twig_int.hrl +++ b/src/twig_int.hrl @@ -21,4 +21,4 @@ -define(LEVEL_ALERT, 1). -define(LEVEL_EMERG, 0). --record(twig, {level, msgid, msg, pid}). +-record(twig, {level, facility, msgid, msg, pid}).