From a067eb02a55a52167d7ca8f4961663809abc9d41 Mon Sep 17 00:00:00 2001 From: Lucas Pardue Date: Thu, 6 Nov 2014 14:32:56 +0000 Subject: [PATCH] Add LOG_NOTICE level logging for application lifecycle events --- src/shrpx.cc | 26 +++++---------- src/shrpx_log.cc | 51 ++++++++++++++++++++---------- src/shrpx_log.h | 2 +- src/shrpx_ssl.cc | 6 ++-- src/shrpx_thread_event_receiver.cc | 4 +-- 5 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/shrpx.cc b/src/shrpx.cc index 2923ba8c..d10b3647 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -190,9 +190,7 @@ evconnlistener* create_evlistener(ListenHandler *handler, int family) // Otherwise, close fd, and create server socket as usual. if(port == get_config()->port) { - if(LOG_ENABLED(INFO)) { - LOG(INFO) << "Listening on port " << get_config()->port; - } + LOG(NOTICE) << "Listening on port " << get_config()->port; return new_evlistener(handler, fd); } @@ -279,9 +277,7 @@ evconnlistener* create_evlistener(ListenHandler *handler, int family) return nullptr; } - if(LOG_ENABLED(INFO)) { - LOG(INFO) << "Listening on " << host << ", port " << get_config()->port; - } + LOG(NOTICE) << "Listening on " << host << ", port " << get_config()->port; return new_evlistener(handler, fd); } @@ -356,9 +352,7 @@ void exec_binary_signal_cb(evutil_socket_t sig, short events, void *arg) { auto listener_handler = static_cast(arg); - if(LOG_ENABLED(INFO)) { - LOG(INFO) << "Executing new binary"; - } + LOG(NOTICE) << "Executing new binary"; auto pid = fork(); @@ -449,9 +443,7 @@ void graceful_shutdown_signal_cb(evutil_socket_t sig, short events, void *arg) { auto listener_handler = static_cast(arg); - if(LOG_ENABLED(INFO)) { - LOG(INFO) << "Graceful shutdown signal received"; - } + LOG(NOTICE) << "Graceful shutdown signal received"; worker_config->graceful_shutdown = true; @@ -1132,8 +1124,8 @@ Mode: Logging: -L, --log-level= Set the severity level of log output. - must be one of INFO, WARNING, ERROR and FATAL. - Default: WARNING + must be one of INFO, NOTICE, WARNING, ERROR and FATAL. + Default: NOTICE --accesslog-file= Set path to write access log. To reopen file, send USR1 signal to nghttpx. @@ -1216,7 +1208,7 @@ Misc: int main(int argc, char **argv) { - Log::set_severity_level(WARNING); + Log::set_severity_level(NOTICE); create_config(); fill_default_config(); @@ -1887,9 +1879,7 @@ int main(int argc, char **argv) event_loop(); - if(LOG_ENABLED(INFO)) { - LOG(INFO) << "Shutdown momentarily"; - } + LOG(NOTICE) << "Shutdown momentarily"; return 0; } diff --git a/src/shrpx_log.cc b/src/shrpx_log.cc index 3bf7a0fe..c9c51e6a 100644 --- a/src/shrpx_log.cc +++ b/src/shrpx_log.cc @@ -45,20 +45,21 @@ namespace shrpx { namespace { const char *SEVERITY_STR[] = { - "INFO", "WARN", "ERROR", "FATAL" + "INFO", "NOTICE", "WARN", "ERROR", "FATAL" }; } // namespace namespace { const char *SEVERITY_COLOR[] = { "\033[1;32m", // INFO + "\033[1;36m", // NOTICE "\033[1;33m", // WARN "\033[1;31m", // ERROR "\033[1;35m", // FATAL }; } // namespace -int Log::severity_thres_ = WARNING; +int Log::severity_thres_ = NOTICE; void Log::set_severity_level(int severity) { @@ -81,6 +82,8 @@ int severity_to_syslog_level(int severity) switch(severity) { case(INFO): return LOG_INFO; + case(NOTICE): + return LOG_NOTICE; case(WARNING): return LOG_WARNING; case(ERROR): @@ -110,9 +113,14 @@ Log::~Log() } if(get_config()->errorlog_syslog) { - syslog(severity_to_syslog_level(severity_), "[%s] %s (%s:%d)", - SEVERITY_STR[severity_], stream_.str().c_str(), - filename_, linenum_); + if (severity_ == NOTICE) { + syslog(severity_to_syslog_level(severity_), "[%s] %s", + SEVERITY_STR[severity_], stream_.str().c_str()); + } else { + syslog(severity_to_syslog_level(severity_), "[%s] %s (%s:%d)", + SEVERITY_STR[severity_], stream_.str().c_str(), + filename_, linenum_); + } return; } @@ -122,17 +130,28 @@ Log::~Log() auto cached_time = get_config()->cached_time; - rv = snprintf(buf, sizeof(buf), - "%s PID%d [%s%s%s] %s%s:%d%s %s\n", - cached_time->c_str(), - getpid(), - tty ? SEVERITY_COLOR[severity_] : "", - SEVERITY_STR[severity_], - tty ? "\033[0m" : "", - tty ? "\033[1;30m" : "", - filename_, linenum_, - tty ? "\033[0m" : "", - stream_.str().c_str()); + if (severity_ == NOTICE) { + rv = snprintf(buf, sizeof(buf), + "%s PID%d [%s%s%s] %s\n", + cached_time->c_str(), + getpid(), + tty ? SEVERITY_COLOR[severity_] : "", + SEVERITY_STR[severity_], + tty ? "\033[0m" : "", + stream_.str().c_str()); + } else { + rv = snprintf(buf, sizeof(buf), + "%s PID%d [%s%s%s] %s%s:%d%s %s\n", + cached_time->c_str(), + getpid(), + tty ? SEVERITY_COLOR[severity_] : "", + SEVERITY_STR[severity_], + tty ? "\033[0m" : "", + tty ? "\033[1;30m" : "", + filename_, linenum_, + tty ? "\033[0m" : "", + stream_.str().c_str()); + } if(rv < 0) { return; diff --git a/src/shrpx_log.h b/src/shrpx_log.h index d060d79b..baf56fe2 100644 --- a/src/shrpx_log.h +++ b/src/shrpx_log.h @@ -71,7 +71,7 @@ class Downstream; (Log(SEVERITY, __FILE__, __LINE__) << "[DHTTP2:" << HTTP2 << "] ") enum SeverityLevel { - INFO, WARNING, ERROR, FATAL + INFO, NOTICE, WARNING, ERROR, FATAL }; class Log { diff --git a/src/shrpx_ssl.cc b/src/shrpx_ssl.cc index f558a9c7..a29152fd 100644 --- a/src/shrpx_ssl.cc +++ b/src/shrpx_ssl.cc @@ -943,8 +943,10 @@ bool check_http2_requirement(SSL *ssl) case TLS1_2_VERSION: break; default: - LOG(INFO) << "TLSv1.2 was not negotiated. " - << "HTTP/2 must not be negotiated."; + if(LOG_ENABLED(INFO)) { + LOG(INFO) << "TLSv1.2 was not negotiated. " + << "HTTP/2 must not be negotiated."; + } return false; } diff --git a/src/shrpx_thread_event_receiver.cc b/src/shrpx_thread_event_receiver.cc index 4d5ee87f..2ae84515 100644 --- a/src/shrpx_thread_event_receiver.cc +++ b/src/shrpx_thread_event_receiver.cc @@ -84,9 +84,7 @@ void ThreadEventReceiver::on_read(bufferevent *bev) } if(wev.type == GRACEFUL_SHUTDOWN) { - if(LOG_ENABLED(INFO)) { - LOG(INFO) << "Graceful shutdown commencing"; - } + LOG(NOTICE) << "Graceful shutdown commencing"; worker_config->graceful_shutdown = true;