From 5b3fae60b162c601e365ebcc1cc992157d56571b Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Thu, 14 Mar 2013 01:19:48 -0400 Subject: [PATCH] Cleanups suggested by @mshonle --- src/lager.erl | 8 ++++---- src/lager_app.erl | 9 +++++---- src/lager_backend_throttle.erl | 24 +++++++++++++++++++++++- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/lager.erl b/src/lager.erl index fe8ecd6..fe300c1 100644 --- a/src/lager.erl +++ b/src/lager.erl @@ -78,13 +78,13 @@ dispatch_log(Severity, Metadata, Format, Args, Size) when is_atom(Severity)-> _ -> Format end, + LagerMsg = lager_msg:new(Msg, Timestamp, + Severity, Metadata, Destinations), case lager_config:get(async, false) of true -> - gen_event:notify(Pid, {log, lager_msg:new(Msg, Timestamp, - Severity, Metadata, Destinations)}); + gen_event:notify(Pid, {log, LagerMsg}); false -> - gen_event:sync_notify(Pid, {log, lager_msg:new(Msg, Timestamp, - Severity, Metadata, Destinations)}) + gen_event:sync_notify(Pid, {log, LagerMsg}) end; false -> ok diff --git a/src/lager_app.erl b/src/lager_app.erl index f03e42e..bbc4218 100644 --- a/src/lager_app.erl +++ b/src/lager_app.erl @@ -39,10 +39,11 @@ start(_StartType, _StartArgs) -> case application:get_env(lager, async_threshold) of undefined -> ok; - {ok, Threshold} when is_integer(Threshold), Threshold > 0 -> - _ = supervisor:start_child(lager_handler_watcher_sup, [lager_event, lager_backend_throttle, [Threshold]]); - {ok, _BadVal} -> - ok + {ok, Threshold} when is_integer(Threshold), Threshold >= 0 -> + _ = supervisor:start_child(lager_handler_watcher_sup, [lager_event, lager_backend_throttle, Threshold]); + {ok, BadVal} -> + error_logger:error_msg("Invalid value for 'async_threshold': ~p~n", [BadVal]), + throw({error, bad_config}) end, Handlers = case application:get_env(lager, handlers) of diff --git a/src/lager_backend_throttle.erl b/src/lager_backend_throttle.erl index d161d5f..9c65ec8 100644 --- a/src/lager_backend_throttle.erl +++ b/src/lager_backend_throttle.erl @@ -1,3 +1,25 @@ +%% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved. +%% +%% This file is provided to you under the Apache License, +%% Version 2.0 (the "License"); you may not use this file +%% except in compliance with the License. You may obtain +%% a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, +%% software distributed under the License is distributed on an +%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +%% KIND, either express or implied. See the License for the +%% specific language governing permissions and limitations +%% under the License. + +%% @doc A simple gen_event backend used to monitor mailbox size and +%% switch log messages between synchronous and asynchronous modes. +%% A gen_event handler is used because a process getting its own mailbox +%% size doesn't involve getting a lock, and gen_event handlers run in their +%% parent's process. + -module(lager_backend_throttle). -include("lager.hrl"). @@ -12,7 +34,7 @@ async = true }). -init([Hwm]) -> +init(Hwm) -> lager_config:set(async, true), {ok, #state{hwm=Hwm}}.