2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright (C) 2017 ScyllaDB
24 #include <seastar/util/log.hh>
25 #include <seastar/util/program-options.hh>
27 #include <seastar/core/sstring.hh>
29 #include <boost/program_options.hpp>
32 #include <unordered_map>
34 /// \addtogroup logging
39 /// \brief Configure application logging at run-time with program options.
44 /// \brief Options for controlling logging at run-time.
46 boost::program_options::options_description get_options_description();
48 using log_level_map = std::unordered_map<sstring, log_level>;
50 /// Logging configuration.
51 struct options : public program_options::option_group {
52 /// \brief Default log level for log messages.
54 /// Valid values are trace, debug, info, warn, error.
56 /// \see \ref log_level.
57 program_options::value<log_level> default_log_level;
58 /// \brief Map of logger name to log level.
60 /// The format is `NAME0=LEVEL0[:NAME1=LEVEL1:...]`.
61 /// Valid logger names can be queried with \p --help-loggers.
62 /// This option can be specified multiple times.
63 program_options::value<log_level_map> logger_log_level;
64 /// Select timestamp style for stdout logs.
67 /// \see logger_timestamp_style.
68 program_options::value<logger_timestamp_style> logger_stdout_timestamps;
69 /// \brief Send log output to output stream.
71 /// As selected by \ref logger_ostream_type.
73 program_options::value<bool> log_to_stdout;
74 /// Send log output to.
76 /// Default: \p stderr.
77 /// \see \ref seastar::logger_ostream_type.
78 program_options::value<seastar::logger_ostream_type> logger_ostream_type;
79 /// Send log output to syslog.
81 /// Default: \p false.
82 program_options::value<bool> log_to_syslog;
84 /// Print colored tag prefix in log messages sent to output stream.
87 program_options::value<bool> log_with_color;
89 options(program_options::option_group* parent_group);
94 /// \brief Print a human-friendly list of the available loggers.
96 void print_available_loggers(std::ostream& os);
99 /// \brief Parse a log-level ({error, warn, info, debug, trace}) string, throwing \c std::runtime_error for an invalid
102 log_level parse_log_level(const sstring&);
105 void parse_map_associations(const std::string& v, std::function<void(std::string, std::string)> consume_key_value);
109 // \brief Parse associations from loggers to log-levels and write the resulting pairs to the output iterator.
111 // \throws \c std::runtime_error for an invalid log-level.
113 template <class OutputIter>
114 void parse_logger_levels(const program_options::string_map& levels, OutputIter out) {
115 std::for_each(levels.begin(), levels.end(), [&out](auto&& pair) {
116 *out++ = std::make_pair(pair.first, parse_log_level(pair.second));
121 /// \brief Extract CLI options into a logging configuration.
123 logging_settings extract_settings(const boost::program_options::variables_map&);
126 /// \brief Extract \ref options into a logging configuration.
128 logging_settings extract_settings(const options&);