log: add log_stderr_prefix option

Allows you to set a prefix for debug log messages send to stderr (e.g.,
"debug ").

Signed-off-by: Sage Weil <sage@redhat.com>
This commit is contained in:
Sage Weil 2017-12-07 16:39:30 -06:00
parent 8a6434c20e
commit c142ae3f15
4 changed files with 17 additions and 1 deletions

View File

@ -202,6 +202,7 @@ public:
"log_max_recent",
"log_to_syslog",
"err_to_syslog",
"log_stderr_prefix",
"log_to_stderr",
"err_to_stderr",
"log_to_graylog",
@ -236,6 +237,10 @@ public:
log->reopen_log_file();
}
if (changed.count("log_stderr_prefix")) {
log->set_log_stderr_prefix(conf->get_val<string>("log_stderr_prefix"));
}
if (changed.count("log_max_new")) {
log->set_max_new(conf->log_max_new);

View File

@ -345,6 +345,9 @@ std::vector<Option> get_global_options() {
.set_daemon_default(true)
.set_description("send critical error log lines to stderr"),
Option("log_stderr_prefix", Option::TYPE_STR, Option::LEVEL_ADVANCED)
.set_description("String to prefix log messages with when sent to stderr"),
Option("log_to_syslog", Option::TYPE_BOOL, Option::LEVEL_BASIC)
.set_default(false)
.set_description("send log lines to syslog facility"),

View File

@ -133,6 +133,11 @@ void Log::set_log_file(string fn)
m_log_file = fn;
}
void Log::set_log_stderr_prefix(const std::string& p)
{
m_log_stderr_prefix = p;
}
void Log::reopen_log_file()
{
pthread_mutex_lock(&m_flush_mutex);
@ -336,7 +341,7 @@ void Log::_flush(EntryQueue *t, EntryQueue *requeue, bool crash)
}
if (do_stderr) {
cerr << buf << std::endl;
cerr << m_log_stderr_prefix << buf << std::endl;
}
if (do_fd) {
buf[buflen] = '\n';

View File

@ -44,6 +44,8 @@ class Log : private Thread
int m_stderr_log, m_stderr_crash;
int m_graylog_log, m_graylog_crash;
std::string m_log_stderr_prefix;
shared_ptr<Graylog> m_graylog;
bool m_stop;
@ -70,6 +72,7 @@ public:
void set_log_file(std::string fn);
void reopen_log_file();
void chown_log_file(uid_t uid, gid_t gid);
void set_log_stderr_prefix(const std::string& p);
void flush();