couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject [5/5] couch commit: updated refs/heads/1994-merge-rcouch to 3f51842
Date Thu, 13 Feb 2014 03:25:56 GMT
couch_log: make sure that the the log file can be set in the ini

For some reason noone noticed until now, but changing the log file was
only possible in the app.config until now. This change fix this by doing
the following:

- when couch_log starts it check if the log file handler has already
  been started using the app.config. If not it start a new handler
- when the filename change after a config update using the API, the old
  file handler is shutdown and a new one is started with the new config.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/3f518421
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/3f518421
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/3f518421

Branch: refs/heads/1994-merge-rcouch
Commit: 3f5184216066fc71451607426b52e973a4b04f03
Parents: 10e26f0
Author: benoitc <bchesneau@gmail.com>
Authored: Wed Feb 12 01:56:39 2014 +0100
Committer: Paul J. Davis <paul.joseph.davis@gmail.com>
Committed: Wed Feb 12 21:24:14 2014 -0600

----------------------------------------------------------------------
 src/couch_log.erl | 77 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 75 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/3f518421/src/couch_log.erl
----------------------------------------------------------------------
diff --git a/src/couch_log.erl b/src/couch_log.erl
index 82158f6..787f25a 100644
--- a/src/couch_log.erl
+++ b/src/couch_log.erl
@@ -144,6 +144,9 @@ init([]) ->
     Sasl = couch_config:get("log", "include_sasl", "true") =:= "true",
     LevelByModule = couch_config:get("log_level_by_module"),
 
+    %% maybe start the log file backend
+    maybe_start_logfile_backend(Filename, ALevel),
+
     %% initialise the ets table if needed
     case ets:info(?MODULE) of
         undefined -> ets:new(?MODULE, [named_table]);
@@ -172,7 +175,7 @@ handle_call({set_level_integer, Module, NewLevel}, _From, State) ->
     ets:insert(?MODULE, {Module, NewLevel}),
     {ok, ok, State#state{level = NewLevel}}.
 
-handle_cast(config_update, State) ->
+handle_cast(config_update, #state{log_file=OldFilename}=State) ->
     Filename = log_file(),
     ALevel = list_to_atom(couch_config:get("log", "level", "info")),
     Level = level_integer(ALevel),
@@ -180,6 +183,12 @@ handle_cast(config_update, State) ->
     %% set default module
     ets:insert(?MODULE, {level, Level}),
 
+    %% should we restart the file backend with a new config?
+    if OldFilename =/= Filename ->
+            restart_logfile_backend(OldFilename, Filename, ALevel);
+        true -> ok
+    end,
+
     %% set log level
     set_loglevel(Filename, ALevel),
 
@@ -246,6 +255,50 @@ read(Bytes, Offset) ->
     ok = file:close(Fd),
     Chunk.
 
+
+maybe_start_logfile_backend(Filename, Level) ->
+    Started = case application:get_env(lager, handlers) of
+        undefined -> false;
+        {ok, Handlers} ->
+            LogFiles = lists:foldl(fun
+                    ({lager_file_backend, Config}, Acc) ->
+                        [hfile(Config) | Acc];
+                    (_, Acc) ->
+                        Acc
+                end, [], Handlers),
+            lists:member(Filename, LogFiles)
+    end,
+
+    case Started of
+        true -> ok;
+        false ->
+            Config = [{file, Filename},
+                      {level, Level},
+                      {formatter, lager_default_formatter},
+                      {formatter_config,
+                       ["[", time, "] [", pid, "] [", severity, "] ",
+                        message, "\n"]}],
+            HandlerId = {lager_file_backend, Filename},
+            {ok, _} = supervisor:start_child(lager_handler_watcher_sup,
+                                             [lager_event, HandlerId, Config])
+    end.
+
+
+restart_logfile_backend(OldFilename, Filename, Level) ->
+    ok = gen_event:delete_handler(lager_event, {lager_file_backend,
+                                                OldFilename}, []),
+
+    %% restart a new handler
+    Config = [{file, Filename},
+              {level, Level},
+              {formatter, lager_default_formatter},
+              {formatter_config,
+               ["[", time, "] [", pid, "] [", severity, "] ", message, "\n"]}],
+    HandlerId = {lager_file_backend, Filename},
+    {ok, _} = supervisor:start_child(lager_handler_watcher_sup,
+                                     [lager_event, HandlerId, Config]),
+    ok.
+
 set_loglevel(Filename, ALevel) ->
     %% set default log level
     lager:set_loglevel(lager_console_backend, ALevel),
@@ -260,10 +313,30 @@ set_loglevel(Filename, ALevel) ->
                 end, Handlers)
     end.
 
-
 log_file() ->
     DefaultLogFile = case application:get_env(couch, log_file) of
         undefined -> "couchdb.log";
         FName -> FName
     end,
     couch_config:get("log", "file", DefaultLogFile).
+
+hfile({FileName, LogLevel}) when is_list(FileName), is_atom(LogLevel) ->
+    %% backwards compatability hack
+    FileName;
+hfile({FileName, LogLevel, _Size, _Date, _Count})
+        when is_list(FileName), is_atom(LogLevel) ->
+    %% backwards compatability hack
+    FileName;
+hfile([{FileName, LogLevel, _Size, _Date, _Count}, {Formatter, _FormatterConfig}])
+    when is_list(FileName), is_atom(LogLevel), is_atom(Formatter) ->
+    %% backwards compatability hack
+    FileName;
+hfile([LogFile,{Formatter}]) ->
+    %% backwards compatability hack
+    hfile([LogFile,{Formatter,[]}]);
+hfile([{FileName, LogLevel}, {Formatter, _FormatterConfig}])
+    when is_list(FileName), is_atom(LogLevel), is_atom(Formatter) ->
+    %% backwards compatability hack
+   FileName;
+hfile(LogFileConfig) when is_list(LogFileConfig) ->
+    proplists:get_value(file, LogFileConfig).


Mime
View raw message