LogUtil.cpp 11.3 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

S
starlord 已提交
12 13
#include "utils/LogUtil.h"

14
#include <libgen.h>
15
#include <cctype>
S
starlord 已提交
16
#include <string>
17

18
#include <yaml-cpp/yaml.h>
W
Wang XiangYu 已提交
19
#include <boost/filesystem.hpp>
20 21 22 23

#include "config/Config.h"
#include "utils/Log.h"

J
jinhai 已提交
24
namespace milvus {
G
groot 已提交
25 26
namespace server {

H
Heisenberg 已提交
27 28 29 30 31 32 33
namespace {
static int global_idx = 0;
static int debug_idx = 0;
static int warning_idx = 0;
static int trace_idx = 0;
static int error_idx = 0;
static int fatal_idx = 0;
W
Wang XiangYu 已提交
34
static int64_t logs_delete_exceeds = 1;
C
Cai Yudong 已提交
35
static bool enable_log_delete = false;
36
}  // namespace
H
Heisenberg 已提交
37 38

// TODO(yzb) : change the easylogging library to get the log level from parameter rather than filename
S
starlord 已提交
39
void
S
starlord 已提交
40 41 42 43 44
RolloutHandler(const char* filename, std::size_t size, el::Level level) {
    char* dirc = strdup(filename);
    char* basec = strdup(filename);
    char* dir = dirname(dirc);
    char* base = basename(basec);
H
Heisenberg 已提交
45 46

    std::string s(base);
S
starlord 已提交
47 48
    std::string list[] = {"\\", " ", "\'", "\"", "*", "\?", "{", "}", ";", "<",
                          ">",  "|", "^",  "&",  "$", "#",  "!", "`", "~"};
H
Heisenberg 已提交
49 50 51 52 53 54 55 56 57 58 59
    std::string::size_type position;
    for (auto substr : list) {
        position = 0;
        while ((position = s.find_first_of(substr, position)) != std::string::npos) {
            s.insert(position, "\\");
            position += 2;
        }
    }
    int ret;
    std::string m(std::string(dir) + "/" + s);
    s = m;
60 61 62 63 64
    try {
        switch (level) {
            case el::Level::Debug: {
                s.append("." + std::to_string(++debug_idx));
                ret = rename(m.c_str(), s.c_str());
C
Cai Yudong 已提交
65
                if (enable_log_delete && debug_idx - logs_delete_exceeds > 0) {
66 67 68 69 70
                    std::string to_delete = m + "." + std::to_string(debug_idx - logs_delete_exceeds);
                    // std::cout << "remote " << to_delete << std::endl;
                    boost::filesystem::remove(to_delete);
                }
                break;
W
Wang XiangYu 已提交
71
            }
72 73 74
            case el::Level::Warning: {
                s.append("." + std::to_string(++warning_idx));
                ret = rename(m.c_str(), s.c_str());
C
Cai Yudong 已提交
75
                if (enable_log_delete && warning_idx - logs_delete_exceeds > 0) {
76 77 78 79
                    std::string to_delete = m + "." + std::to_string(warning_idx - logs_delete_exceeds);
                    boost::filesystem::remove(to_delete);
                }
                break;
W
Wang XiangYu 已提交
80
            }
81 82 83
            case el::Level::Trace: {
                s.append("." + std::to_string(++trace_idx));
                ret = rename(m.c_str(), s.c_str());
C
Cai Yudong 已提交
84
                if (enable_log_delete && trace_idx - logs_delete_exceeds > 0) {
85 86 87 88
                    std::string to_delete = m + "." + std::to_string(trace_idx - logs_delete_exceeds);
                    boost::filesystem::remove(to_delete);
                }
                break;
W
Wang XiangYu 已提交
89
            }
90 91 92
            case el::Level::Error: {
                s.append("." + std::to_string(++error_idx));
                ret = rename(m.c_str(), s.c_str());
C
Cai Yudong 已提交
93
                if (enable_log_delete && error_idx - logs_delete_exceeds > 0) {
94 95 96 97
                    std::string to_delete = m + "." + std::to_string(error_idx - logs_delete_exceeds);
                    boost::filesystem::remove(to_delete);
                }
                break;
W
Wang XiangYu 已提交
98
            }
99 100 101
            case el::Level::Fatal: {
                s.append("." + std::to_string(++fatal_idx));
                ret = rename(m.c_str(), s.c_str());
C
Cai Yudong 已提交
102
                if (enable_log_delete && fatal_idx - logs_delete_exceeds > 0) {
103 104 105 106
                    std::string to_delete = m + "." + std::to_string(fatal_idx - logs_delete_exceeds);
                    boost::filesystem::remove(to_delete);
                }
                break;
W
Wang XiangYu 已提交
107
            }
108 109 110
            default: {
                s.append("." + std::to_string(++global_idx));
                ret = rename(m.c_str(), s.c_str());
C
Cai Yudong 已提交
111
                if (enable_log_delete && global_idx - logs_delete_exceeds > 0) {
112 113 114 115
                    std::string to_delete = m + "." + std::to_string(global_idx - logs_delete_exceeds);
                    boost::filesystem::remove(to_delete);
                }
                break;
W
Wang XiangYu 已提交
116 117
            }
        }
118 119
    } catch (const std::exception& exc) {
        std::cerr << exc.what() << ". Exception throws from RolloutHandler." << std::endl;
H
Heisenberg 已提交
120 121 122
    }
}

S
starlord 已提交
123
Status
W
Wang XiangYu 已提交
124
InitLog(bool trace_enable, bool debug_enable, bool info_enable, bool warning_enable, bool error_enable,
W
Wang XiangYu 已提交
125
        bool fatal_enable, const std::string& logs_path, int64_t max_log_file_size, int64_t delete_exceeds) {
W
Wang XiangYu 已提交
126 127 128 129 130 131 132 133
    el::Configurations defaultConf;
    defaultConf.setToDefault();
    defaultConf.setGlobally(el::ConfigurationType::Format, "[%datetime][%level]%msg");
    defaultConf.setGlobally(el::ConfigurationType::ToFile, "true");
    defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false");
    defaultConf.setGlobally(el::ConfigurationType::SubsecondPrecision, "3");
    defaultConf.setGlobally(el::ConfigurationType::PerformanceTracking, "false");

B
BossZou 已提交
134 135
    std::string logs_reg_path = logs_path.rfind('/') == logs_path.length() - 1 ? logs_path : logs_path + "/";
    std::string global_log_path = logs_reg_path + "milvus-%datetime{%y-%M-%d-%H:%m}-global.log";
W
Wang XiangYu 已提交
136 137 138
    defaultConf.set(el::Level::Global, el::ConfigurationType::Filename, global_log_path.c_str());
    defaultConf.set(el::Level::Global, el::ConfigurationType::Enabled, "true");

B
BossZou 已提交
139
    std::string info_log_path = logs_reg_path + "milvus-%datetime{%y-%M-%d-%H:%m}-info.log";
W
Wang XiangYu 已提交
140 141 142 143 144 145 146
    defaultConf.set(el::Level::Info, el::ConfigurationType::Filename, info_log_path.c_str());
    if (info_enable) {
        defaultConf.set(el::Level::Info, el::ConfigurationType::Enabled, "true");
    } else {
        defaultConf.set(el::Level::Info, el::ConfigurationType::Enabled, "false");
    }

B
BossZou 已提交
147
    std::string debug_log_path = logs_reg_path + "milvus-%datetime{%y-%M-%d-%H:%m}-debug.log";
W
Wang XiangYu 已提交
148 149 150 151 152 153 154
    defaultConf.set(el::Level::Debug, el::ConfigurationType::Filename, debug_log_path.c_str());
    if (debug_enable) {
        defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "true");
    } else {
        defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false");
    }

B
BossZou 已提交
155
    std::string warning_log_path = logs_reg_path + "milvus-%datetime{%y-%M-%d-%H:%m}-warning.log";
W
Wang XiangYu 已提交
156 157 158 159 160 161 162
    defaultConf.set(el::Level::Warning, el::ConfigurationType::Filename, warning_log_path.c_str());
    if (warning_enable) {
        defaultConf.set(el::Level::Warning, el::ConfigurationType::Enabled, "true");
    } else {
        defaultConf.set(el::Level::Warning, el::ConfigurationType::Enabled, "false");
    }

B
BossZou 已提交
163
    std::string trace_log_path = logs_reg_path + "milvus-%datetime{%y-%M-%d-%H:%m}-trace.log";
W
Wang XiangYu 已提交
164 165 166 167 168 169 170
    defaultConf.set(el::Level::Trace, el::ConfigurationType::Filename, trace_log_path.c_str());
    if (trace_enable) {
        defaultConf.set(el::Level::Trace, el::ConfigurationType::Enabled, "true");
    } else {
        defaultConf.set(el::Level::Trace, el::ConfigurationType::Enabled, "false");
    }

B
BossZou 已提交
171
    std::string error_log_path = logs_reg_path + "milvus-%datetime{%y-%M-%d-%H:%m}-error.log";
W
Wang XiangYu 已提交
172 173 174 175 176 177 178
    defaultConf.set(el::Level::Error, el::ConfigurationType::Filename, error_log_path.c_str());
    if (error_enable) {
        defaultConf.set(el::Level::Error, el::ConfigurationType::Enabled, "true");
    } else {
        defaultConf.set(el::Level::Error, el::ConfigurationType::Enabled, "false");
    }

B
BossZou 已提交
179
    std::string fatal_log_path = logs_reg_path + "milvus-%datetime{%y-%M-%d-%H:%m}-fatal.log";
W
Wang XiangYu 已提交
180 181 182 183 184 185 186
    defaultConf.set(el::Level::Fatal, el::ConfigurationType::Filename, fatal_log_path.c_str());
    if (fatal_enable) {
        defaultConf.set(el::Level::Fatal, el::ConfigurationType::Enabled, "true");
    } else {
        defaultConf.set(el::Level::Fatal, el::ConfigurationType::Enabled, "false");
    }

C
Cai Yudong 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    if (max_log_file_size < CONFIG_LOGS_MAX_LOG_FILE_SIZE_MIN ||
        max_log_file_size > CONFIG_LOGS_MAX_LOG_FILE_SIZE_MAX) {
        return Status(SERVER_UNEXPECTED_ERROR, "max_log_file_size must in range[" +
                                                   std::to_string(CONFIG_LOGS_MAX_LOG_FILE_SIZE_MIN) + ", " +
                                                   std::to_string(CONFIG_LOGS_MAX_LOG_FILE_SIZE_MAX) + "], now is " +
                                                   std::to_string(max_log_file_size));
    }
    max_log_file_size *= 1024 * 1024;
    defaultConf.setGlobally(el::ConfigurationType::MaxLogFileSize, std::to_string(max_log_file_size));
    el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
    el::Helpers::installPreRollOutCallback(RolloutHandler);
    el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);

    // set delete_exceeds = 0 means disable throw away log file even they reach certain limit.
    if (delete_exceeds != 0) {
        if (delete_exceeds < CONFIG_LOGS_LOG_ROTATE_NUM_MIN || delete_exceeds > CONFIG_LOGS_LOG_ROTATE_NUM_MAX) {
            return Status(SERVER_UNEXPECTED_ERROR, "delete_exceeds must in range[" +
                                                       std::to_string(CONFIG_LOGS_LOG_ROTATE_NUM_MIN) + ", " +
                                                       std::to_string(CONFIG_LOGS_LOG_ROTATE_NUM_MAX) + "], now is " +
                                                       std::to_string(delete_exceeds));
W
Wang XiangYu 已提交
207
        }
C
Cai Yudong 已提交
208 209
        enable_log_delete = true;
        logs_delete_exceeds = delete_exceeds;
W
Wang XiangYu 已提交
210
    }
211

W
Wang XiangYu 已提交
212
    el::Loggers::reconfigureLogger("default", defaultConf);
S
starlord 已提交
213 214

    return Status::OK();
G
groot 已提交
215 216
}

217 218 219 220 221 222
void
LogConfigInFile(const std::string& path) {
    // TODO(yhz): Check if file exists
    auto node = YAML::LoadFile(path);
    YAML::Emitter out;
    out << node;
223
    LOG_SERVER_INFO_ << "\n\n"
224 225 226 227 228 229 230 231 232
                     << std::string(15, '*') << "Config in file" << std::string(15, '*') << "\n\n"
                     << out.c_str();
}

void
LogConfigInMem() {
    auto& config = Config::GetInstance();
    std::string config_str;
    config.GetConfigJsonStr(config_str, 3);
233
    LOG_SERVER_INFO_ << "\n\n"
234 235 236 237 238 239 240 241 242
                     << std::string(15, '*') << "Config in memory" << std::string(15, '*') << "\n\n"
                     << config_str;
}

void
LogCpuInfo() {
    /*CPU information*/
    std::fstream fcpu("/proc/cpuinfo", std::ios::in);
    if (!fcpu.is_open()) {
243 244
        LOG_SERVER_WARNING_ << "Cannot obtain CPU information. Open file /proc/cpuinfo fail: " << strerror(errno)
                            << "(errno: " << errno << ")";
245 246 247 248 249 250 251 252 253
        return;
    }
    std::stringstream cpu_info_ss;
    cpu_info_ss << fcpu.rdbuf();
    fcpu.close();
    std::string cpu_info = cpu_info_ss.str();

    auto processor_pos = cpu_info.rfind("processor");
    if (std::string::npos == processor_pos) {
254
        LOG_SERVER_WARNING_ << "Cannot obtain CPU information. No sub string \'processor\'";
255 256 257 258
        return;
    }

    auto sub_str = cpu_info.substr(processor_pos);
259
    LOG_SERVER_INFO_ << "\n\n" << std::string(15, '*') << "CPU" << std::string(15, '*') << "\n\n" << sub_str;
260 261
}

S
starlord 已提交
262 263
}  // namespace server
}  // namespace milvus