From 5d49f8a25005945b7260facc1a985c294746616b Mon Sep 17 00:00:00 2001
From: Siri Hansen
Date: Mon, 14 May 2018 20:27:55 +0200
Subject: Add update_logger_config/1 and update_handler_config/2 to logger
---
lib/kernel/doc/src/logger.xml | 54 ++++++++++++++++++++++++++++++++--------
lib/kernel/src/logger.erl | 12 +++++++++
lib/kernel/src/logger_server.erl | 15 +++++++----
lib/kernel/test/logger_SUITE.erl | 27 ++++++++++++++------
4 files changed, 85 insertions(+), 23 deletions(-)
(limited to 'lib/kernel')
diff --git a/lib/kernel/doc/src/logger.xml b/lib/kernel/doc/src/logger.xml
index 239b9553b1..183102f240 100644
--- a/lib/kernel/doc/src/logger.xml
+++ b/lib/kernel/doc/src/logger.xml
@@ -645,11 +645,12 @@ start(_, []) ->
Set configuration data for the logger. This overwrites the
current logger configuration.
To modify the existing configuration,
- use set_logger_config/2
- , or read the current configuration
+ use
+ update_logger_config/1, or, if a more
+ complex merge is needed, read the current configuration
with get_logger_config/0
- , then merge in your added or updated
- associations before writing it back.
+ , then do the merge before writing the new
+ configuration back with this function.
If a key is removed compared to the current configuration,
the default value is used.
@@ -662,7 +663,23 @@ start(_, []) ->
Add or update configuration data for the logger. If the
given Key already exists, its associated
value will be changed to Value. If it
- doesn't exist, it will be added.
+ does not exist, it will be added.
+
+
+
+
+
+ Update configuration data for the logger.
+
+ Update configuration data for the logger. This function
+ behaves as if it was implemented as follows:
+
+{ok,Old} = logger:get_logger_config(),
+logger:set_logger_config(maps:merge(Old,Config)).
+
+ To overwrite the existing configuration without any merge,
+ use set_logger_config/1
+ .
@@ -673,11 +690,12 @@ start(_, []) ->
Set configuration data for the specified handler. This
overwrites the current handler configuration.
To modify the existing configuration,
- use set_handler_config/3
- , or read the current configuration
+ use
+ update_handler_config/2, or, if a more
+ complex merge is needed, read the current configuration
with get_handler_config/1
- , then merge in your added or updated
- associations before writing it back.
+ , then do the merge before writing the new
+ configuration back with this function.
If a key is removed compared to the current configuration,
and the key is know by Logger, the default value is used. If
it is a custom key, then it is up to the handler
@@ -694,11 +712,27 @@ start(_, []) ->
Add or update configuration data for the specified
handler. If the given Key already
exists, its associated value will be changed
- to Value. If it doesn't exist, it will
+ to Value. If it does not exist, it will
be added.
+
+
+ Update configuration data for the specified handler.
+
+ Update configuration data for the specified handler. This function
+ behaves as if it was implemented as follows:
+
+{ok,{_,Old}} = logger:get_handler_config(HandlerId),
+logger:set_handler_config(HandlerId,maps:merge(Old,Config)).
+
+ To overwrite the existing configuration without any merge,
+ use set_handler_config/2
+ .
+
+
+
Compare the severity of two log levels.
diff --git a/lib/kernel/src/logger.erl b/lib/kernel/src/logger.erl
index 2c7466fdf6..a839f97e62 100644
--- a/lib/kernel/src/logger.erl
+++ b/lib/kernel/src/logger.erl
@@ -40,6 +40,7 @@
set_module_level/2, reset_module_level/1,
set_logger_config/1, set_logger_config/2,
set_handler_config/2, set_handler_config/3,
+ update_logger_config/1, update_handler_config/2,
get_logger_config/0, get_handler_config/1,
add_handlers/1]).
@@ -361,6 +362,17 @@ set_handler_config(HandlerId,Key,Value) ->
set_handler_config(HandlerId,Config) ->
logger_server:set_config(HandlerId,Config).
+-spec update_logger_config(Config) -> ok | {error,term()} when
+ Config :: config().
+update_logger_config(Config) ->
+ logger_server:update_config(logger,Config).
+
+-spec update_handler_config(HandlerId,Config) -> ok | {error,term()} when
+ HandlerId :: handler_id(),
+ Config :: config().
+update_handler_config(HandlerId,Config) ->
+ logger_server:update_config(HandlerId,Config).
+
-spec get_logger_config() -> {ok,Config} when
Config :: config().
get_logger_config() ->
diff --git a/lib/kernel/src/logger_server.erl b/lib/kernel/src/logger_server.erl
index bd0c325f2b..275b9c476f 100644
--- a/lib/kernel/src/logger_server.erl
+++ b/lib/kernel/src/logger_server.erl
@@ -27,7 +27,7 @@
add_filter/2, remove_filter/2,
set_module_level/2, reset_module_level/1,
cache_module_level/1,
- set_config/2, set_config/3]).
+ set_config/2, set_config/3, update_config/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -92,10 +92,7 @@ cache_module_level(Module) ->
set_config(Owner,Key,Value) ->
- case sanity_check(Owner,Key,Value) of
- ok -> call({update_config,Owner,#{Key=>Value}});
- Error -> Error
- end.
+ update_config(Owner,#{Key=>Value}).
set_config(Owner,Config0) ->
case sanity_check(Owner,Config0) of
@@ -106,6 +103,14 @@ set_config(Owner,Config0) ->
Error
end.
+update_config(Owner, Config) ->
+ case sanity_check(Owner,Config) of
+ ok ->
+ call({update_config,Owner,Config});
+ Error ->
+ Error
+ end.
+
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
diff --git a/lib/kernel/test/logger_SUITE.erl b/lib/kernel/test/logger_SUITE.erl
index 1b75d8278e..64968cbca2 100644
--- a/lib/kernel/test/logger_SUITE.erl
+++ b/lib/kernel/test/logger_SUITE.erl
@@ -227,15 +227,31 @@ change_config(_Config) ->
h1,#{conf_call=>fun() -> logger:set_module_level(?MODULE,debug) end}),
{ok,{?MODULE,C2}} = logger:get_handler_config(h1),
- %% Change one key only
+ %% Change handler config: Single key
{error,fail} = logger:set_handler_config(h1,conf_call,fun() -> {error,fail} end),
ok = logger:set_handler_config(h1,custom,custom),
[changing_config] = test_server:messages_get(),
{ok,{?MODULE,#{custom:=custom}=C3}} = logger:get_handler_config(h1),
C2 = maps:remove(custom,C3),
+ %% Change handler config: Map
+ ok = logger:update_handler_config(h1,#{custom=>new_custom}),
+ [changing_config] = test_server:messages_get(),
+ {ok,{_,C4}} = logger:get_handler_config(h1),
+ C4 = C3#{custom:=new_custom},
+
+ %% Change logger config: Single key
+ {ok,LConfig0} = logger:get_logger_config(),
+ ok = logger:set_logger_config(level,warning),
+ {ok,LConfig1} = logger:get_logger_config(),
+ LConfig1 = LConfig0#{level:=warning},
+
+ %% Change logger config: Map
+ ok = logger:update_logger_config(#{level=>error}),
+ {ok,LConfig2} = logger:get_logger_config(),
+ LConfig2 = LConfig1#{level:=error},
+
%% Overwrite logger config - check that defaults are added
- {ok,LConfig} = logger:get_logger_config(),
ok = logger:set_logger_config(#{filter_default=>stop}),
{ok,#{level:=info,filters:=[],filter_default:=stop}=LC1} =
logger:get_logger_config(),
@@ -246,13 +262,8 @@ change_config(_Config) ->
{ok,#{handlers:=HIds2}} = logger_config:get(?LOGGER_TABLE,logger),
HIds1 = lists:sort(HIds2),
- %% Change one key only
- ok = logger:set_logger_config(level,warning),
- {ok,#{level:=warning,filters:=[],filter_default:=stop}} =
- logger:get_logger_config(),
-
%% Cleanup
- ok = logger:set_logger_config(LConfig),
+ ok = logger:set_logger_config(LConfig0),
[] = test_server:messages_get(),
ok.
--
cgit v1.2.3