Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 51EF717B84 for ; Fri, 18 Sep 2015 13:13:15 +0000 (UTC) Received: (qmail 53492 invoked by uid 500); 18 Sep 2015 13:13:12 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 53438 invoked by uid 500); 18 Sep 2015 13:13:12 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 53429 invoked by uid 99); 18 Sep 2015 13:13:12 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Sep 2015 13:13:12 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 13D0BE03D2; Fri, 18 Sep 2015 13:13:12 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: rnewson@apache.org To: commits@couchdb.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: twig commit: updated refs/heads/master to 9393f4d Date: Fri, 18 Sep 2015 13:13:12 +0000 (UTC) Repository: couchdb-twig Updated Branches: refs/heads/master ea18f4f63 -> 9393f4d95 Allow facility override in options Project: http://git-wip-us.apache.org/repos/asf/couchdb-twig/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-twig/commit/9393f4d9 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-twig/tree/9393f4d9 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-twig/diff/9393f4d9 Branch: refs/heads/master Commit: 9393f4d957b1054c562da00d8c3d467d88b13a84 Parents: ea18f4f Author: Robert Newson Authored: Wed Aug 19 00:16:30 2015 +0100 Committer: Robert Newson Committed: Fri Sep 18 14:04:32 2015 +0100 ---------------------------------------------------------------------- src/twig.erl | 21 +++++++++++++++------ src/twig_event_handler.erl | 22 +++++++++++++++------- src/twig_int.hrl | 2 +- 3 files changed, 31 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-twig/blob/9393f4d9/src/twig.erl ---------------------------------------------------------------------- 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. http://git-wip-us.apache.org/repos/asf/couchdb-twig/blob/9393f4d9/src/twig_event_handler.erl ---------------------------------------------------------------------- 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]). http://git-wip-us.apache.org/repos/asf/couchdb-twig/blob/9393f4d9/src/twig_int.hrl ---------------------------------------------------------------------- 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}).