couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From beno...@apache.org
Subject couchdb commit: updated refs/heads/1994-merge-rcouch to 0bc0f70
Date Wed, 12 Feb 2014 01:09:50 GMT
Updated Branches:
  refs/heads/1994-merge-rcouch 0163e2327 -> 0bc0f70eb


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/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/0bc0f70e
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/0bc0f70e
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/0bc0f70e

Branch: refs/heads/1994-merge-rcouch
Commit: 0bc0f70eb6de096e13479569d4a0a88eaa6a8be9
Parents: 0163e23
Author: Benoit Chesneau <bchesneau@gmail.com>
Authored: Wed Feb 12 01:56:39 2014 +0100
Committer: Benoit Chesneau <bchesneau@gmail.com>
Committed: Wed Feb 12 01:56:39 2014 +0100

----------------------------------------------------------------------
 apps/couch/src/couch_log.erl | 77 ++++++++++++++++++++++++++++++++++++++-
 etc/couchdb/app.config       | 11 +-----
 2 files changed, 77 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/0bc0f70e/apps/couch/src/couch_log.erl
----------------------------------------------------------------------
diff --git a/apps/couch/src/couch_log.erl b/apps/couch/src/couch_log.erl
index 82158f6..787f25a 100644
--- a/apps/couch/src/couch_log.erl
+++ b/apps/couch/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).

http://git-wip-us.apache.org/repos/asf/couchdb/blob/0bc0f70e/etc/couchdb/app.config
----------------------------------------------------------------------
diff --git a/etc/couchdb/app.config b/etc/couchdb/app.config
index b6d247c..1db33ec 100644
--- a/etc/couchdb/app.config
+++ b/etc/couchdb/app.config
@@ -8,7 +8,7 @@
                 "{{platform_etc_dir}}/couch.ini",
                 "{{platform_etc_dir}}/local.ini"
                 ]},
-        {log_file, "{{platform_log_dir}}/couchdb.log"}
+        {log_file, "{{platform_log_dir}}/couch.log"}
     ]},
 
 
@@ -16,14 +16,7 @@
           {lager_console_backend, [info,
                                    {lager_default_formatter,
                                           ["[", pid, "] [", severity, "] ",
-                                           message, "\n"]}]},
-
-          {lager_file_backend, [{file, "{{platform_log_dir}}/couchdb.log"},
-                                 {level, info},
-                                 {formatter, lager_default_formatter},
-                                 {formatter_config,
-                                  ["[", time, "] [", pid, "] [", severity, "] ",
-                                   message, "\n"]}]}
+                                           message, "\n"]}]}
                     ]}]},
 
  %% os_mon config


Mime
View raw message