aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/logger/composite_ut.cpp
blob: a75122eb57a19f6941834ff0339ccae737dbd8c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "log.h" 
#include <library/cpp/logger/init_context/config.h> 
#include <library/cpp/logger/init_context/yconf.h> 
#include <library/cpp/testing/unittest/registar.h> 
#include <library/cpp/yconf/patcher/unstrict_config.h> 
#include <util/stream/file.h> 
#include <util/system/fs.h> 
 
Y_UNIT_TEST_SUITE(TCompositeLogTest) 
{ 
    TVector<TString> ReadLines(const TString & filename) { 
        TVector<TString> lines; 
        TIFStream fin(filename); 
        TString line; 
        while (fin.ReadLine(line)) { 
            lines.push_back(std::move(line)); 
        } 
        return lines; 
    } 
 
    void Clear(const TString & filename) { 
        NFs::Remove(filename + "1"); 
        NFs::Remove(filename + "2"); 
    } 
 
    void DoTestComposite(const ILogBackendCreator::IInitContext& ctx, const TString & filename) { 
        Clear(filename); 
        { 
            TLog log; 
            { 
                auto creator = ILogBackendCreator::Create(ctx); 
                log.ResetBackend(creator->CreateLogBackend()); 
                log.ReopenLog(); 
            } 
            log.Write(TLOG_ERR, "first\n"); 
            log.Write(TLOG_DEBUG, "second\n"); 
        } 
        auto data1 = ReadLines(filename + "1"); 
        auto data2 = ReadLines(filename + "2"); 
        UNIT_ASSERT_VALUES_EQUAL(data1.size(), 2); 
        UNIT_ASSERT(data1[0] == "first"); 
        UNIT_ASSERT(data1[1] == "second"); 
 
        UNIT_ASSERT_VALUES_EQUAL(data2.size(), 1); 
        UNIT_ASSERT(data2[0] == "first"); 
        Clear(filename); 
    } 
 
    Y_UNIT_TEST(TestCompositeConfig) { 
        TString s(R"( 
{ 
    "LoggerType": "composite", 
    "SubLogger":[ 
        { 
            "LoggerType": "file", 
            "Path": "config_log_1" 
        }, { 
            "LoggerType": "config_log_2", 
            "LogLevel": "INFO" 
        } 
    ] 
})"); 
        TStringInput si(s); 
        NConfig::TConfig cfg = NConfig::TConfig::FromJson(si); 
        //Прогоняем конфигурацию через серализацию и десериализацию 
        TLogBackendCreatorInitContextConfig ctx(cfg); 
        TString newCfg = ILogBackendCreator::Create(ctx)->AsJson().GetStringRobust(); 
        TStringInput si2(newCfg); 
        DoTestComposite(TLogBackendCreatorInitContextConfig(NConfig::TConfig::FromJson(si2)), "config_log_"); 
 
    } 
    Y_UNIT_TEST(TestCompositeYConf) { 
        constexpr const char* CONFIG = R"( 
<Logger> 
    LoggerType: composite 
    <SubLogger> 
        LoggerType: file 
        Path: yconf_log_1 
    </SubLogger> 
    <SubLogger> 
        LoggerType: yconf_log_2 
        LogLevel: INFO 
    </SubLogger> 
</Logger> 
)"; 
        TUnstrictConfig cfg; 
        if (!cfg.ParseMemory(CONFIG)) { 
            TString errors; 
            cfg.PrintErrors(errors); 
            UNIT_ASSERT_C(false, errors); 
        } 
        TLogBackendCreatorInitContextYConf ctx(*cfg.GetFirstChild("Logger")); 
        //Прогоняем конфигурацию через серализацию и десериализацию 
        TUnstrictConfig newCfg; 
        UNIT_ASSERT(newCfg.ParseJson(ILogBackendCreator::Create(ctx)->AsJson())); 
        DoTestComposite(TLogBackendCreatorInitContextYConf(*newCfg.GetRootSection()), "yconf_log_"); 
    } 
};