diff --git a/src/common/LogClient.cc b/src/common/LogClient.cc index 0dce8bd69a0..86c82fb6e42 100644 --- a/src/common/LogClient.cc +++ b/src/common/LogClient.cc @@ -86,10 +86,8 @@ void LogClient::do_log(clog_type type, const std::string& s) // log to syslog? if (cct->_conf->clog_to_syslog) { - ostringstream oss; - oss << e; - string str(oss.str()); - syslog(clog_type_to_syslog_prio(e.type) | LOG_USER, "%s", str.c_str()); + e.log_to_syslog(cct->_conf->clog_to_syslog_level, + cct->_conf->clog_to_syslog_facility); } // log to monitor? diff --git a/src/common/LogEntry.cc b/src/common/LogEntry.cc index 80801bcf3a3..97b173f1fd8 100644 --- a/src/common/LogEntry.cc +++ b/src/common/LogEntry.cc @@ -1,28 +1,14 @@ #include +#include + #include "LogEntry.h" #include "Formatter.h" +#include "include/stringify.h" + -int clog_type_to_syslog_prio(clog_type t) -{ - switch (t) { - case CLOG_DEBUG: - return LOG_DEBUG; - case CLOG_INFO: - return LOG_INFO; - case CLOG_WARN: - return LOG_WARNING; - case CLOG_ERROR: - return LOG_ERR; - case CLOG_SEC: - return LOG_CRIT; - default: - assert(0); - return 0; - } -} // ---- // LogEntryKey @@ -56,6 +42,104 @@ void LogEntryKey::generate_test_instances(list& o) // ---- +int clog_type_to_syslog_level(clog_type t) +{ + switch (t) { + case CLOG_DEBUG: + return LOG_DEBUG; + case CLOG_INFO: + return LOG_INFO; + case CLOG_WARN: + return LOG_WARNING; + case CLOG_ERROR: + return LOG_ERR; + case CLOG_SEC: + return LOG_CRIT; + default: + assert(0); + return 0; + } +} + +int string_to_syslog_level(string s) +{ + if (boost::iequals(s, "debug")) + return LOG_DEBUG; + if (boost::iequals(s, "info") || + boost::iequals(s, "notice")) + return LOG_INFO; + if (boost::iequals(s, "warning") || + boost::iequals(s, "warn")) + return LOG_WARNING; + if (boost::iequals(s, "error") || + boost::iequals(s, "err")) + return LOG_ERR; + if (boost::iequals(s, "crit") || + boost::iequals(s, "critical") || + boost::iequals(s, "emerg")) + return LOG_CRIT; + + // err on the side of noise! + return LOG_DEBUG; +} + +int string_to_syslog_facility(string s) +{ + if (boost::iequals(s, "auth")) + return LOG_AUTH; + if (boost::iequals(s, "authpriv")) + return LOG_AUTHPRIV; + if (boost::iequals(s, "cron")) + return LOG_CRON; + if (boost::iequals(s, "daemon")) + return LOG_DAEMON; + if (boost::iequals(s, "ftp")) + return LOG_FTP; + if (boost::iequals(s, "kern")) + return LOG_KERN; + if (boost::iequals(s, "local0")) + return LOG_LOCAL0; + if (boost::iequals(s, "local1")) + return LOG_LOCAL1; + if (boost::iequals(s, "local2")) + return LOG_LOCAL2; + if (boost::iequals(s, "local3")) + return LOG_LOCAL3; + if (boost::iequals(s, "local4")) + return LOG_LOCAL4; + if (boost::iequals(s, "local5")) + return LOG_LOCAL5; + if (boost::iequals(s, "local6")) + return LOG_LOCAL6; + if (boost::iequals(s, "local7")) + return LOG_LOCAL7; + if (boost::iequals(s, "lpr")) + return LOG_LPR; + if (boost::iequals(s, "mail")) + return LOG_MAIL; + if (boost::iequals(s, "news")) + return LOG_NEWS; + if (boost::iequals(s, "syslog")) + return LOG_SYSLOG; + if (boost::iequals(s, "user")) + return LOG_USER; + if (boost::iequals(s, "uucp")) + return LOG_UUCP; + + // default to USER + return LOG_USER; +} + +void LogEntry::log_to_syslog(string level, string facility) +{ + int min = string_to_syslog_level(level); + int l = clog_type_to_syslog_level(type); + if (l <= min) { + int f = string_to_syslog_facility(facility); + syslog(l | f, "%s", stringify(*this).c_str()); + } +} + void LogEntry::encode(bufferlist& bl) const { ENCODE_START(2, 2, bl); diff --git a/src/common/LogEntry.h b/src/common/LogEntry.h index cea68013016..81c23537d31 100644 --- a/src/common/LogEntry.h +++ b/src/common/LogEntry.h @@ -35,7 +35,10 @@ typedef enum { /* * Given a clog log_type, return the equivalent syslog priority */ -int clog_type_to_syslog_prio(clog_type t); +int clog_type_to_syslog_level(clog_type t); + +int string_to_syslog_level(string s); +int string_to_syslog_facility(string s); struct LogEntryKey { @@ -66,6 +69,8 @@ struct LogEntry { LogEntryKey key() const { return LogEntryKey(who, stamp, seq); } + void log_to_syslog(string level, string facility); + void encode(bufferlist& bl) const; void decode(bufferlist::iterator& bl); void dump(Formatter *f) const; diff --git a/src/common/config_opts.h b/src/common/config_opts.h index 2fb52a87622..573e87f9a4a 100644 --- a/src/common/config_opts.h +++ b/src/common/config_opts.h @@ -42,8 +42,12 @@ OPTION(log_flush_on_exit, OPT_BOOL, true) OPTION(clog_to_monitors, OPT_BOOL, true) OPTION(clog_to_syslog, OPT_BOOL, false) +OPTION(clog_to_syslog_level, OPT_STR, "info") // this level and above +OPTION(clog_to_syslog_facility, OPT_STR, "daemon") OPTION(mon_cluster_log_to_syslog, OPT_BOOL, false) +OPTION(mon_cluster_log_to_syslog_level, OPT_STR, "info") // this level and above +OPTION(mon_cluster_log_to_syslog_facility, OPT_STR, "daemon") OPTION(mon_cluster_log_file, OPT_STR, "/var/log/ceph/$cluster.log") DEFAULT_SUBSYS(0, 5) diff --git a/src/mon/LogMonitor.cc b/src/mon/LogMonitor.cc index f06de5217a0..84ff0dd0743 100644 --- a/src/mon/LogMonitor.cc +++ b/src/mon/LogMonitor.cc @@ -128,7 +128,8 @@ void LogMonitor::update_from_paxos() string s = ss.str(); if (g_conf->mon_cluster_log_to_syslog) { - syslog(clog_type_to_syslog_prio(le.type) | LOG_USER, "%s", s.c_str()); + le.log_to_syslog(g_conf->mon_cluster_log_to_syslog_level, + g_conf->mon_cluster_log_to_syslog_facility); } if (g_conf->mon_cluster_log_file.length()) { blog.append(s + "\n");