Skip to content

Commit

Permalink
feature: set loggers config file over CLI arg
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
  • Loading branch information
xDimon committed Sep 25, 2024
1 parent 0115a6b commit 79df088
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/application/impl/app_configuration_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ namespace kagome::application {
desc.add_options()
("help,h", "show this help message")
("version,v", "show version information")
("logcfg", po::value<std::string>(), "optional, path to config file of logger")
("log,l", po::value<std::vector<std::string>>(),
"Sets a custom logging filter. Syntax is `<target>=<level>`, e.g. -llibp2p=off.\n"
"Log levels (most to least verbose) are trace, debug, verbose, info, warn, error, critical, off. By default, all targets log `info`.\n"
Expand Down
30 changes: 30 additions & 0 deletions core/log/configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "log/configurator.hpp"

#include <boost/program_options.hpp>

namespace kagome::log {

namespace {
Expand Down Expand Up @@ -138,4 +140,32 @@ namespace kagome::log {
Configurator::Configurator(std::shared_ptr<PrevConfigurator> previous,
filesystem::path path)
: ConfiguratorFromYAML(std::move(previous), std::move(path)) {}

std::optional<filesystem::path> Configurator::getLogConfigFile(
int argc, const char **argv) {
namespace po = boost::program_options;
po::options_description desc("General options");
desc.add_options()
// clang-format off
("logcfg", po::value<std::string>(), "optional, path to logging config file")
// clang-format on
;

po::variables_map vm;

po::parsed_options parsed = po::command_line_parser(argc, argv)
.options(desc)
.allow_unregistered()
.run();
po::store(parsed, vm);
po::notify(vm);

if (auto it = vm.find("logcfg"); it != vm.end()) {
if (not it->second.defaulted()) {
return it->second.as<std::string>();
}
}
return std::nullopt;
}

} // namespace kagome::log
3 changes: 3 additions & 0 deletions core/log/configurator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ namespace kagome::log {

explicit Configurator(std::shared_ptr<PrevConfigurator> previous,
filesystem::path path);

static std::optional<filesystem::path> getLogConfigFile(int argc,
const char **argv);
};

} // namespace kagome::log
114 changes: 114 additions & 0 deletions examples/logger_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# ----------------
sinks:
- name: console
type: console
stream: stdout
thread: name
color: true
latency: 0
groups:
- name: main
sink: console
level: info
is_fallback: true
children:
- name: libp2p
level: off
- name: kagome
children:
- name: profile
- name: injector
- name: application
- name: rpc
children:
- name: rpc_transport
- name: api
children:
- name: author_api
- name: authorship
- name: blockchain
children:
- name: block_tree
- name: block_storage
- name: digest_tracker
- name: offchain
- name: authority
- name: crypto
children:
- name: bip39
- name: key_store
- name: ed25519
- name: ecdsa
- name: consensus
children:
- name: timeline
- name: babe
children:
- name: babe_lottery
- name: block_appender
- name: block_executor
- name: block_validator
- name: babe_config_repo
- name: grandpa
children:
- name: voting_round
- name: parachain
children:
- name: pvf_executor
- name: dispute
- name: runtime
children:
- name: runtime_api
- name: host_api
children:
- name: elliptic_curves_extension
- name: memory_extension
- name: io_extension
- name: crypto_extension
- name: storage_extension
- name: child_storage_extension
- name: offchain_extension
- name: misc_extension
- name: runtime_cache
- name: binaryen
- name: wavm
- name: wasmedge
- name: metrics
- name: telemetry
- name: network
children:
- name: reputation
- name: synchronizer
- name: authority_discovery
- name: kagome_protocols
children:
- name: block_announce_protocol
- name: grandpa_protocol
- name: propagate_transactions_protocol
- name: sync_protocol
- name: state_protocol
- name: warp_sync_protocol
- name: parachain_protocols
children:
- name: collation_protocol_vstaging
- name: validation_protocol_vstaging
- name: req_collation_protocol
- name: req_chunk_protocol
- name: req_available_data_protocol
- name: req_statement_protocol
- name: req_pov_protocol
- name: dispute_protocol
- name: req_attested_candidate_protocol
- name: changes_trie
- name: storage
children:
- name: trie
- name: trie_pruner
- name: transactions
- name: pubsub
- name: threads
- name: others
children:
- name: testing
- name: debug
# ----------------
31 changes: 27 additions & 4 deletions node/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,32 @@ int main(int argc, const char **argv, const char **env) {

soralog::util::setThreadName("kagome");

auto logging_system = std::make_shared<soralog::LoggingSystem>(
std::make_shared<kagome::log::Configurator>(
std::make_shared<libp2p::log::Configurator>()));
// Logging system
auto logging_system = [&] {
auto custom_log_config_path =
kagome::log::Configurator::getLogConfigFile(argc - 1, argv + 1);
if (custom_log_config_path.has_value()) {
if (not std::filesystem::is_regular_file(
custom_log_config_path.value())) {
std::cerr << "Provided wrong path to config file of logging\n";
exit(EXIT_FAILURE);
}
}

auto libp2p_log_configurator =
std::make_shared<libp2p::log::Configurator>();

auto kagome_log_configurator =
custom_log_config_path.has_value()
? std::make_shared<kagome::log::Configurator>(
std::move(libp2p_log_configurator),
custom_log_config_path.value())
: std::make_shared<kagome::log::Configurator>(
std::move(libp2p_log_configurator));

return std::make_shared<soralog::LoggingSystem>(
std::move(kagome_log_configurator));
}();

auto r = logging_system->configure();
if (not r.message.empty()) {
Expand Down Expand Up @@ -162,7 +185,7 @@ int main(int argc, const char **argv, const char **env) {
}

else if (name.substr(0, 1) == "-") {
// The first argument is not subcommand, run as node
// The first argument isn't subcommand, run as node
exit_code = run_node(argc, argv);

} else {
Expand Down

0 comments on commit 79df088

Please sign in to comment.