diff --git a/src/shrpx_accesslog.cc b/src/shrpx_accesslog.cc index e29b4fc0..290d0798 100644 --- a/src/shrpx_accesslog.cc +++ b/src/shrpx_accesslog.cc @@ -31,6 +31,7 @@ #include #include "shrpx_config.h" +#include "shrpx_downstream.h" namespace shrpx { @@ -55,10 +56,10 @@ void upstream_connect(const std::string& client_ip) { char datestr[64]; get_datestr(datestr); - fprintf(stderr, "[%s] Accepted %s\n", datestr, client_ip.c_str()); + fprintf(stderr, "%s [%s] ACCEPT\n", client_ip.c_str(), datestr); fflush(stderr); if(get_config()->use_syslog) { - syslog(LOG_INFO, "Accepted %s\n", client_ip.c_str()); + syslog(LOG_INFO, "%s ACCEPT\n", client_ip.c_str()); } } @@ -66,11 +67,11 @@ void upstream_spdy_stream(const std::string& client_ip, int32_t stream_id) { char datestr[64]; get_datestr(datestr); - fprintf(stderr, "[%s] %s SPDY stream_id=%d\n", datestr, client_ip.c_str(), + fprintf(stderr, "%s [%s] SYN_STREAM %d\n", client_ip.c_str(), datestr, stream_id); fflush(stderr); if(get_config()->use_syslog) { - syslog(LOG_INFO, "%s SPDY stream_id=%d\n", client_ip.c_str(), stream_id); + syslog(LOG_INFO, "%s SYN_STREAM %d\n", client_ip.c_str(), stream_id); } } @@ -79,12 +80,70 @@ void upstream_spdy_stream_close(const std::string& client_ip, { char datestr[64]; get_datestr(datestr); - fprintf(stderr, "[%s] %s SPDY stream_id=%d closed\n", - datestr, client_ip.c_str(), stream_id); + fprintf(stderr, "%s [%s] STREAM_CLOSE %d\n", + client_ip.c_str(), datestr, stream_id); fflush(stderr); if(get_config()->use_syslog) { - syslog(LOG_INFO, "%s SPDY stream_id=%d closed\n", - client_ip.c_str(), stream_id); + syslog(LOG_INFO, "%s STREAM_CLOSE %d\n", client_ip.c_str(), stream_id); + } +} + +namespace { +const char* status_code_color(int status_code) +{ + if(status_code <= 199) { + return "\033[1;36m"; + } else if(status_code <= 299) { + return "\033[1;32m"; + } else if(status_code <= 399) { + return "\033[1;34m"; + } else if(status_code <= 499) { + return "\033[1;31m"; + } else if(status_code <= 599) { + return "\033[1;35m"; + } else { + return ""; + } +} +} // namespace + +void upstream_response(const std::string& client_ip, int status_code, + Downstream *downstream) +{ + char datestr[64]; + get_datestr(datestr); + if(downstream) { + fprintf(stderr, "%s%s [%s] %d%s %d \"%s %s HTTP/%u.%u\"\n", + get_config()->tty ? status_code_color(status_code) : "", + client_ip.c_str(), datestr, + status_code, + get_config()->tty ? "\033[0m" : "", + downstream->get_stream_id(), + downstream->get_request_method().c_str(), + downstream->get_request_path().c_str(), + downstream->get_request_major(), + downstream->get_request_minor()); + fflush(stderr); + if(get_config()->use_syslog) { + syslog(LOG_INFO, "%s %d %d \"%s %s HTTP/%u.%u\"\n", + client_ip.c_str(), + status_code, + downstream->get_stream_id(), + downstream->get_request_method().c_str(), + downstream->get_request_path().c_str(), + downstream->get_request_major(), + downstream->get_request_minor()); + } + } else { + fprintf(stderr, "%s%s [%s] %d%s 0 \"-\"\n", + get_config()->tty ? status_code_color(status_code) : "", + client_ip.c_str(), datestr, + status_code, + get_config()->tty ? "\033[0m" : ""); + if(get_config()->use_syslog) { + syslog(LOG_INFO, "%s %d 0 \"-\"\n", client_ip.c_str(), status_code); + } + fflush(stderr); } } diff --git a/src/shrpx_accesslog.h b/src/shrpx_accesslog.h index e53d7046..921c1223 100644 --- a/src/shrpx_accesslog.h +++ b/src/shrpx_accesslog.h @@ -31,10 +31,14 @@ namespace shrpx { +class Downstream; + void upstream_connect(const std::string& client_ip); void upstream_spdy_stream(const std::string& client_ip, int32_t stream_id); void upstream_spdy_stream_close(const std::string& client_ip, int32_t stream_id); +void upstream_response(const std::string& client_ip, int status_code, + Downstream *downstream); } // namespace shrpx diff --git a/src/shrpx_https_upstream.cc b/src/shrpx_https_upstream.cc index 0f1bc973..f6b89d03 100644 --- a/src/shrpx_https_upstream.cc +++ b/src/shrpx_https_upstream.cc @@ -35,6 +35,7 @@ #include "shrpx_http.h" #include "shrpx_config.h" #include "shrpx_error.h" +#include "shrpx_accesslog.h" #include "util.h" using namespace spdylay; @@ -544,6 +545,10 @@ int HttpsUpstream::error_reply(int status_code) if(downstream) { downstream->set_response_state(Downstream::MSG_COMPLETE); } + if(get_config()->accesslog) { + upstream_response(this->get_client_handler()->get_ipaddr(), status_code, + downstream); + } return 0; } @@ -647,6 +652,10 @@ int HttpsUpstream::on_downstream_header_complete(Downstream *downstream) ULOG(FATAL, this) << "evbuffer_add() failed"; return -1; } + if(get_config()->accesslog) { + upstream_response(this->get_client_handler()->get_ipaddr(), + downstream->get_response_http_status(), downstream); + } return 0; } diff --git a/src/shrpx_spdy_upstream.cc b/src/shrpx_spdy_upstream.cc index fff488dc..3917e082 100644 --- a/src/shrpx_spdy_upstream.cc +++ b/src/shrpx_spdy_upstream.cc @@ -698,6 +698,10 @@ int SpdyUpstream::error_reply(Downstream *downstream, int status_code) << spdylay_strerror(rv); DIE(); } + if(get_config()->accesslog) { + upstream_response(get_client_handler()->get_ipaddr(), + status_code, downstream); + } return 0; } @@ -795,6 +799,11 @@ int SpdyUpstream::on_downstream_header_complete(Downstream *downstream) ULOG(FATAL, this) << "spdylay_submit_response() failed"; return -1; } + if(get_config()->accesslog) { + upstream_response(get_client_handler()->get_ipaddr(), + downstream->get_response_http_status(), + downstream); + } return 0; }