Skip to content
This repository
Browse code

Merge pull request #102 from basho/adt-backend-id

Support backend modules defining their own gen_event handler ID
  • Loading branch information...
commit dda46da731919a83869b7c46bf099cd11f726c72 2 parents 5cfaba3 + da4e0cf
Andrew Thompson authored December 12, 2012
18  src/lager_app.erl
@@ -79,17 +79,21 @@ stop(Handlers) ->
79 79
 expand_handlers([]) ->
80 80
     [];
81 81
 expand_handlers([{lager_file_backend, Configs}|T]) ->
82  
-    [ to_config(Config) || Config <- Configs] ++
  82
+    [ {lager_file_backend:config_to_id(Config), Config} || Config <- Configs] ++
83 83
       expand_handlers(T);
  84
+expand_handlers([{Mod, Config}|T]) when is_atom(Mod) ->
  85
+    %% allow the backend to generate a gen_event handler id, if it wants to
  86
+    code:load_file(Mod),
  87
+    Res = case erlang:function_exported(Mod, config_to_id, 1) of
  88
+        true ->
  89
+            {Mod:config_to_id(Config), Config};
  90
+        false ->
  91
+            {Mod, Config}
  92
+    end,
  93
+    [Res | expand_handlers(T)];
84 94
 expand_handlers([H|T]) ->
85 95
     [H | expand_handlers(T)].
86 96
 
87  
-to_config({Name,Severity}) ->
88  
-    {{lager_file_backend, Name}, {Name, Severity}};
89  
-to_config({Name,_Severity,_Size,_Rotation,_Count}=Config) ->
90  
-    {{lager_file_backend, Name}, Config};
91  
-to_config([{Name,_Severity,_Size,_Rotation,_Count}, _Format] = Config) ->
92  
-    {{lager_file_backend, Name}, Config}.
93 97
 
94 98
 
95 99
 
11  src/lager_file_backend.erl
@@ -41,6 +41,8 @@
41 41
 -export([init/1, handle_call/2, handle_event/2, handle_info/2, terminate/2,
42 42
         code_change/3]).
43 43
 
  44
+-export([config_to_id/1]).
  45
+
44 46
 -record(state, {
45 47
         name :: string(),
46 48
         level :: integer(),
@@ -121,6 +123,15 @@ terminate(_Reason, #state{fd=FD}) ->
121 123
 code_change(_OldVsn, State, _Extra) ->
122 124
     {ok, State}.
123 125
 
  126
+%% convert the config into a gen_event handler ID
  127
+config_to_id({Name,_Severity}) ->
  128
+    {?MODULE, Name};
  129
+config_to_id({Name,_Severity,_Size,_Rotation,_Count}) ->
  130
+    {?MODULE, Name};
  131
+config_to_id([{Name,_Severity,_Size,_Rotation,_Count}, _Format]) ->
  132
+    {?MODULE, Name}.
  133
+
  134
+
124 135
 write(#state{name=Name, fd=FD, inode=Inode, flap=Flap, size=RotSize,
125 136
         count=Count} = State, Level, Msg) ->
126 137
     case lager_util:ensure_logfile(Name, FD, Inode, true) of

0 notes on commit dda46da

Please sign in to comment.
Something went wrong with that request. Please try again.