aboutsummaryrefslogblamecommitdiffstats
path: root/lib/kernel/test/logger_simple_SUITE.erl
blob: 0d505b14f54f1a0f4d7620a50f7c498c83e80722 (plain) (tree)


























                                                                           

                                                            
















                                                              

                                    





                                                       
                                                    



























                                                                               
                     



















                                                                                


                                                                              
 












                                                                                               
 
       

                       


















                                                                                               









                                                               










                               
       
 
                           

















                                                                                               









                                                               










                               
       
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2018. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(logger_simple_SUITE).

-compile(export_all).

-include_lib("common_test/include/ct.hrl").
-include_lib("kernel/include/logger.hrl").
-include_lib("kernel/src/logger_internal.hrl").

-import(logger_test_lib, [setup/2, log/3, sync_and_read/3]).

-define(check_no_log,[] = test_server:messages_get()).
-define(check(Expected),
        receive {log,Expected} ->
                [] = test_server:messages_get()
        after 1000 ->
                ct:fail({report_not_received,
                         {line,?LINE},
                         {expected,Expected},
                         {got,test_server:messages_get()}})
        end).

-define(str,"Log from "++atom_to_list(?FUNCTION_NAME)++
            ":"++integer_to_list(?LINE)).
-define(map_rep,#{function=>?FUNCTION_NAME, line=>?LINE}).
-define(keyval_rep,[{function,?FUNCTION_NAME}, {line,?LINE}]).

suite() ->
    [{timetrap,{seconds,30}},
     {ct_hooks, [logger_test_lib]}].

init_per_suite(Config) ->
    #{handlers:=Hs0} = logger:i(),
    Hs = lists:keydelete(cth_log_redirect,1,Hs0),
    [ok = logger:remove_handler(Id) || {Id,_,_} <- Hs],
    Env = [{App,Key,application:get_env(App,Key)} ||
              {App,Key} <- [{kernel,logger_level}]],
    [{env,Env},{logger,Hs}|Config].

end_per_suite(Config) ->
    [application:set_env(App,Key,Val) || {App,Key,Val} <- ?config(env,Config)],
    Hs = ?config(logger,Config),
    [ok = logger:add_handler(Id,Mod,C) || {Id,Mod,C} <- Hs],
    ok.

init_per_group(_Group, Config) ->
    Config.

end_per_group(_Group, _Config) ->
    ok.

init_per_testcase(_TestCase, Config) ->
    Config.

end_per_testcase(Case, Config) ->
    try apply(?MODULE,Case,[cleanup,Config])
    catch error:undef -> ok
    end,
    ok.

groups() ->
    [].

all() -> 
    [start_stop,
     replace_default,
     replace_file,
     replace_disk_log
    ].

start_stop(_Config) ->
    undefined = whereis(logger_simple),
    register(logger_simple,self()),
    {error,_} = logger:add_handler(logger_simple,
                                   logger_simple,
                                   #{filter_default=>log}),    
    unregister(logger_simple),
    ok = logger:add_handler(logger_simple,logger_simple,#{filter_default=>log}),
    Pid = whereis(logger_simple),
    true = is_pid(Pid),
    ok = logger:remove_handler(logger_simple),
    false = is_pid(whereis(logger_simple)),
    ok.
start_stop(cleanup,_Config) ->
    logger:remove_handler(logger_simple).

%% This testcase just tests that it does not crash, the default handler prints
%% to stdout which we cannot read from in a detached slave.
replace_default(Config) ->

    {ok, _, Node} = logger_test_lib:setup(Config, [{logger, [{handler, default, undefined}]}]),
    log(Node, emergency, [M1=?str]),
    log(Node, alert, [M2=?str,[]]),
    log(Node, error, [M3=?map_rep]),
    log(Node, info, [M4=?keyval_rep]),
    log(Node, info, [M41=?keyval_rep++[not_key_val]]),
    rpc:call(Node, error_logger, error_report, [some_type,M5=?map_rep]),
    rpc:call(Node, error_logger, warning_report, ["some_type",M6=?map_rep]),
    log(Node, critical, [M7=?str,[A7=?keyval_rep]]),
    log(Node, notice, [M8=["fake",string,"line:",?LINE]]),

    Env = rpc:call(Node, application, get_env, [kernel, logger, []]),
    ok = rpc:call(Node, logger, add_handlers, [Env]),

    ok.

replace_file(Config) ->

    {ok, _, Node} = logger_test_lib:setup(Config, [{logger, [{handler, default, undefined}]}]),
    log(Node, emergency, [M1=?str]),
    log(Node, alert, [M2=?str,[]]),
    log(Node, error, [M3=?map_rep]),
    log(Node, info, [M4=?keyval_rep]),
    log(Node, info, [M41=?keyval_rep++[not_key_val]]),
    log(Node, critical, [M7=?str,[A7=?keyval_rep]]),
    log(Node, notice, [M8=["fake",string,"line:",?LINE]]),

    File = filename:join(proplists:get_value(priv_dir,Config),
                         atom_to_list(?FUNCTION_NAME)++".log"),

    ok = rpc:call(Node, logger, add_handlers,
                  [[{handler, default, logger_std_h,
                     #{ logger_std_h => #{ type => {file, File} }}}]]),

    {ok,Bin} = sync_and_read(Node, file, File),
    Lines = [unicode:characters_to_list(L) ||
                L <- binary:split(Bin,<<"\n">>,[global,trim])],
    ["=EMERGENCY REPORT===="++_,
     M1,
     "=ALERT REPORT===="++_,
     M2,
     "=ERROR REPORT===="++_,
     _,
     _,
     "=INFO REPORT===="++_,
     _,
     _,
     "=INFO REPORT===="++_,
     _,
     _,
     _,
     "=CRITICAL REPORT===="++_,
     _,
     _,
     "=NOTICE REPORT===="++_,
     _
    ] = Lines,
    ok.

replace_disk_log(Config) ->

    {ok, _, Node} = logger_test_lib:setup(Config, [{logger, [{handler, default, undefined}]}]),
    log(Node, emergency, [M1=?str]),
    log(Node, alert, [M2=?str,[]]),
    log(Node, error, [M3=?map_rep]),
    log(Node, info, [M4=?keyval_rep]),
    log(Node, info, [M41=?keyval_rep++[not_key_val]]),
    log(Node, critical, [M7=?str,[A7=?keyval_rep]]),
    log(Node, notice, [M8=["fake",string,"line:",?LINE]]),

    File = filename:join(proplists:get_value(priv_dir,Config),
                         atom_to_list(?FUNCTION_NAME)++".log"),

    ok = rpc:call(Node, logger, add_handlers,
                  [[{handler, default, logger_disk_log_h,
                     #{ disk_log_opts => #{ file => File }}}]]),
    {ok,Bin} = sync_and_read(Node, disk_log, File),
    Lines = [unicode:characters_to_list(L) ||
                L <- binary:split(Bin,<<"\n">>,[global,trim])],
    ["=EMERGENCY REPORT===="++_,
     M1,
     "=ALERT REPORT===="++_,
     M2,
     "=ERROR REPORT===="++_,
     _,
     _,
     "=INFO REPORT===="++_,
     _,
     _,
     "=INFO REPORT===="++_,
     _,
     _,
     _,
     "=CRITICAL REPORT===="++_,
     _,
     _,
     "=NOTICE REPORT===="++_,
     _
    ] = Lines,
    ok.