This commit is contained in:
gabime 2019-12-11 01:24:51 +02:00
parent b278baf94e
commit 2b3000dddc
3 changed files with 71 additions and 26 deletions

View File

@ -9,14 +9,16 @@
int main(int, char *[])
{
spdlog::set_pattern("%v");
try
{
auto cfg = spdlog::cfg::from_env();
for (auto &item : cfg)
{
spdlog::info("logger: '{}' level: '{}' pattern: '{}'", item.first, item.second.level_name, item.second.pattern);
}
spdlog::cfg::env::init();
spdlog::info("Hello");
// auto cfg = spdlog::cfg::from_env();
// for (auto &item : cfg)
// {
// spdlog::info("logger: '{}' level: '{}' pattern: '{}'", item.first, item.second.level_name, item.second.pattern);
// }
}
catch (spdlog::spdlog_ex &ex)
{

View File

@ -17,6 +17,17 @@
namespace spdlog {
namespace cfg {
namespace env {
struct logger_cfg
{
std::string level_name;
std::string pattern;
};
using cfg_map = std::unordered_map<std::string, logger_cfg>;
using name_val_pair = std::pair<std::string, std::string>;
// inplace convert to lowercase
inline std::string &to_lower_(std::string &str)
{
@ -34,8 +45,6 @@ inline std::string &trim_(std::string &str)
return str;
}
using name_val_pair = std::pair<std::string, std::string>;
// return (name,value) pair from given "name=value" string.
// return empty string on missing parts
// "key=val" => ("key", "val")
@ -67,7 +76,7 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(con
std::unordered_map<std::string, std::string> rv;
while (std::getline(token_stream, token, ','))
{
if(token.empty())
if (token.empty())
{
continue;
}
@ -83,7 +92,7 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(con
return rv;
}
SPDLOG_INLINE cfg_map from_env()
inline cfg_map from_env_()
{
using details::os::getenv;
cfg_map configs;
@ -145,5 +154,28 @@ SPDLOG_INLINE cfg_map from_env()
return configs;
}
SPDLOG_INLINE void init()
{
auto cfg_map = from_env_();
for (const auto &logger_cfg : cfg_map)
{
auto &logger_name = logger_cfg.first;
auto level = level::from_str(logger_cfg.second.level_name);
auto &pattern = logger_cfg.second.pattern;
if (logger_name == "*")
{
spdlog::set_level(level);
spdlog::set_pattern(pattern);
}
else if (auto logger = spdlog::get(logger_name))
{
logger->set_level(level);
logger->set_pattern(pattern);
}
}
}
} // namespace env
} // namespace cfg
} // namespace spdlog

View File

@ -7,25 +7,36 @@
#include <string>
#include <unordered_map>
// config spdlog from environment variables
//
// Init levels and patterns from env variables SPDLOG_LEVEL and SPDLOG_PATTERN.
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
//
// Examples:
//
// set global level to debug:
// export SPDLOG_LEVEL=debug
//
// turn off all logging except for logger1:
// export SPDLOG_LEVEL="off,logger1=debug"
//
// turn off all logging except for logger1 and logger2:
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
//
// set global pattern:
// export SPDLOG_PATTERN="[%x] [%l] [%n] %v"
//
// set pattern for logger1:
// export SPDLOG_PATTERN="logger1=%v,*=[%x] [%l] [%n] %v"
//
// set global pattern and different pattern for logger1:
// export SPDLOG_PATTERN="[%x] [%l] [%n] %v,logger1=[%u] %v"
namespace spdlog {
namespace cfg {
struct logger_cfg
{
std::string level_name;
std::string pattern;
};
using cfg_map = std::unordered_map<std::string, logger_cfg>;
namespace env {
void init();
}
// Init levels and patterns from env variabls SPDLOG_LEVEL & SPDLOG_PATTERN
// Examples:
// export SPDLOG_LEVEL=debug
// export SPDLOG_LEVEL=logger1=%v,*=[%x] [%l] [%n] %v
// export SPDLOG_LEVEL=logger1=debug,logger2=info,*=error
// export SPDLOG_PATTERN=[%x] [%l] [%n] %v
//
// Note: will set the level to info if finds unknown level in SPDLOG_LEVEL
cfg_map from_env();
} // namespace cfg
} // namespace spdlog