Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bipthelin committed Aug 31, 2012
1 parent 152127c commit dac8d21
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LICENSE
@@ -0,0 +1,7 @@
Copyright (c) 2012 KIVRA

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
51 changes: 51 additions & 0 deletions Makefile
@@ -0,0 +1,51 @@
.PHONY: deps test

all: deps compile

compile:
./rebar compile

deps:
./rebar get-deps

clean:
./rebar clean

distclean: clean
./rebar delete-deps

test:
./rebar eunit

##
## Doc targets
##
docs:
./rebar doc

APPS = kernel stdlib sasl erts ssl tools os_mon runtime_tools crypto inets \
xmerl webtool snmp public_key mnesia eunit syntax_tools compiler
COMBO_PLT =COMBO_PLT $(HOME)/.riak_combo_dialyzer_plt

check_plt: compile
dialyzer --check_plt_plt --plt $(COMBO_PLT) --apps $(APPS)

build_plt: compile
dialyzer --dialyzerbuild_plt --output_plt $(COMBO_PLT) --apps $(APPS)

dialyzer: compile
@echo
@echo Use "'make check_plt'" to check PLT prior to using this target.
@echo Use "'make build_plt'" to build PLT prior to using this target.
@echoho
@sleep 1
dialyzer -Wno_return --plt $(COMBO_PLT) ebin | \
fgrep -v -f ./dialyzer.ignore-warnings

cleanplt:
@echo
@echo "Are you sure? It takes about 1/2 hour to re-build."
@echo Deleting $(COMBO_PLTT) in 5 seconds.
@echo
sleep 5
rm $(COMBO_PLT)
Binary file added rebar
Binary file not shown.
11 changes: 11 additions & 0 deletions rebar.config
@@ -0,0 +1,11 @@
{erl_opts, [
warnings_as_errors
,warn_export_all
]
}.
{cover_enabled, true}.
{deps, [
{jsx, "1.3.*", {git, "git://github.com/talentdeficit/jsx.git", {branch, "master"}}}
,{lager, "1.2.*", {git, "git://github.com/basho/lager", {branch, "master"}}}
]
}.
37 changes: 37 additions & 0 deletions src/lager_loggly.app.src
@@ -0,0 +1,37 @@
%% ----------------------------------------------------------------------------
%%
%% lager_loggly: Loggly backend for Lager
%%
%% Copyright (c) 2012 KIVRA
%%
%% Permission is hereby granted, free of charge, to any person obtaining a
%% copy of this software and associated documentation files (the "Software"),
%% to deal in the Software without restriction, including without limitation
%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
%% and/or sell copies of the Software, and to permit persons to whom the
%% Software is furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
%% DEALINGS IN THE SOFTWARE.
%%
%% ----------------------------------------------------------------------------

{application, lager_loggly,
[
{description, "Loggly backend for Lager"},
{vsn, git},
{modules, []},
{applications, [
kernel,
stdlib
]},
{registered, []}
]}.
114 changes: 114 additions & 0 deletions src/lager_loggly_backend.erl
@@ -0,0 +1,114 @@
%% ----------------------------------------------------------------------------
%%
%% lager_loggly: Loggly backend for Lager
%%
%% Copyright (c) 2012 KIVRA
%%
%% Permission is hereby granted, free of charge, to any person obtaining a
%% copy of this software and associated documentation files (the "Software"),
%% to deal in the Software without restriction, including without limitation
%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
%% and/or sell copies of the Software, and to permit persons to whom the
%% Software is furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
%% DEALINGS IN THE SOFTWARE.
%%
%% ----------------------------------------------------------------------------

-module(lager_loggly_backend).

-behaviour(gen_event).

-export([
init/1
,handle_call/2
,handle_event/2
,handle_info/2
,terminate/2
,code_change/3
]).

%%% this is only exported for the spawn call
-export([deferred_log/3]).

-record(state, {
level :: integer()
,retry_interval :: integer()
,retry_times :: integer()
,loggly_url :: binary()
}).

-include_lib("lager/include/lager.hrl").

init([Level, RetryTimes, RetryInterval, LogglyUrl]) ->
application:start(inets),
application:start(crypto),
application:start(public_key),
application:start(ssl),
State = #state{
level = lager_util:level_to_num(Level)
,retry_interval = RetryInterval
,retry_times = RetryTimes
,loggly_url = LogglyUrl
},
{ok, State}.

handle_call(get_loglevel, #state{ level = Level } = State) ->
{ok, Level, State};
handle_call({set_loglevel, Level}, State) ->
{ok, ok, State#state{ level = lager_util:level_to_num(Level) }};
handle_call(_Request, State) ->
{ok, ok, State}.

%% @private
handle_event({log, Level, {_Date, _Time}, [_LevStr, Loc, Message]}, State) ->
Payload = jsx:to_json([
{<<"level">>, convert_level(Level)}
,{<<"location">>, list_to_binary(Loc)}
,{<<"message">>, list_to_binary(Message)}
]),
Request = {State#state.loggly_url, [], "application/json", Payload},
RetryTimes = State#state.retry_times,
RetryInterval = State#state.retry_interval,

%% Spawn a background process to handle sending the email.
%% It will recurse until the message is successfully sent.
spawn(?MODULE, deferred_log, [Request, RetryTimes, RetryInterval]),
{ok, State};
handle_event(_Event, State) ->
{ok, State}.

handle_info(_Info, State) ->
{ok, State}.

terminate(_Reason, _State) ->
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.


%%% Private


deferred_log(_Request, 0, _) ->
ok;
deferred_log(Request, Retries, Interval) ->
case httpc:request(post, Request, [], [{body_format, binary}]) of
{ok, {{_, 200, _}, _H, _B}} -> ok;
_ ->
timer:sleep(Interval * 1000),
deferred_log(Request, Retries - 1, Interval)
end.

convert_level(Level) ->
list_to_binary(atom_to_list(lager_util:num_to_level(Level))).

0 comments on commit dac8d21

Please sign in to comment.