2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "shrpx_log.h"
29 #endif // HAVE_SYSLOG_H
32 #endif // HAVE_UNISTD_H
33 #ifdef HAVE_INTTYPES_H
35 #endif // HAVE_INTTYPES_H
44 #include "shrpx_config.h"
45 #include "shrpx_downstream.h"
49 using namespace nghttp2;
54 const char *SEVERITY_STR[] = {"INFO", "NOTICE", "WARN", "ERROR", "FATAL"};
58 const char *SEVERITY_COLOR[] = {
60 "\033[1;36m", // NOTICE
62 "\033[1;31m", // ERROR
63 "\033[1;35m", // FATAL
67 int Log::severity_thres_ = NOTICE;
69 void Log::set_severity_level(int severity) { severity_thres_ = severity; }
71 int Log::set_severity_level_by_name(const char *name) {
72 for (size_t i = 0, max = array_size(SEVERITY_STR); i < max; ++i) {
73 if (strcmp(SEVERITY_STR[i], name) == 0) {
81 int severity_to_syslog_level(int severity) {
98 Log::Log(int severity, const char *filename, int linenum)
99 : filename_(filename), severity_(severity), linenum_(linenum) {}
108 auto lgconf = log_config();
110 if (!log_enabled(severity_) ||
111 (lgconf->errorlog_fd == -1 && !get_config()->errorlog_syslog)) {
115 if (get_config()->errorlog_syslog) {
116 if (severity_ == NOTICE) {
117 syslog(severity_to_syslog_level(severity_), "[%s] %s",
118 SEVERITY_STR[severity_], stream_.str().c_str());
120 syslog(severity_to_syslog_level(severity_), "[%s] %s (%s:%d)",
121 SEVERITY_STR[severity_], stream_.str().c_str(), filename_,
129 auto tty = lgconf->errorlog_tty;
131 lgconf->update_tstamp(std::chrono::system_clock::now());
132 auto &time_local = lgconf->time_local_str;
134 if (severity_ == NOTICE) {
135 rv = snprintf(buf, sizeof(buf), "%s PID%d [%s%s%s] %s\n",
136 time_local.c_str(), get_config()->pid,
137 tty ? SEVERITY_COLOR[severity_] : "", SEVERITY_STR[severity_],
138 tty ? "\033[0m" : "", stream_.str().c_str());
140 rv = snprintf(buf, sizeof(buf), "%s PID%d [%s%s%s] %s%s:%d%s %s\n",
141 time_local.c_str(), get_config()->pid,
142 tty ? SEVERITY_COLOR[severity_] : "", SEVERITY_STR[severity_],
143 tty ? "\033[0m" : "", tty ? "\033[1;30m" : "", filename_,
144 linenum_, tty ? "\033[0m" : "", stream_.str().c_str());
151 auto nwrite = std::min(static_cast<size_t>(rv), sizeof(buf) - 1);
153 while (write(lgconf->errorlog_fd, buf, nwrite) == -1 && errno == EINTR)
158 template <typename OutputIterator>
159 std::pair<OutputIterator, size_t> copy(const char *src, size_t avail,
160 OutputIterator oitr) {
161 auto nwrite = std::min(strlen(src), avail);
162 auto noitr = std::copy_n(src, nwrite, oitr);
163 return std::make_pair(noitr, avail - nwrite);
167 void upstream_accesslog(const std::vector<LogFragment> &lfv,
168 const LogSpec &lgsp) {
169 auto lgconf = log_config();
171 if (lgconf->accesslog_fd == -1 && !get_config()->accesslog_syslog) {
177 auto downstream = lgsp.downstream;
180 auto avail = sizeof(buf) - 2;
182 lgconf->update_tstamp(lgsp.time_now);
183 auto &time_local = lgconf->time_local_str;
184 auto &time_iso8601 = lgconf->time_iso8601_str;
186 for (auto &lf : lfv) {
188 case SHRPX_LOGF_LITERAL:
189 std::tie(p, avail) = copy(lf.value.get(), avail, p);
191 case SHRPX_LOGF_REMOTE_ADDR:
192 std::tie(p, avail) = copy(lgsp.remote_addr, avail, p);
194 case SHRPX_LOGF_TIME_LOCAL:
195 std::tie(p, avail) = copy(time_local.c_str(), avail, p);
197 case SHRPX_LOGF_TIME_ISO8601:
198 std::tie(p, avail) = copy(time_iso8601.c_str(), avail, p);
200 case SHRPX_LOGF_REQUEST:
201 std::tie(p, avail) = copy(lgsp.method, avail, p);
202 std::tie(p, avail) = copy(" ", avail, p);
203 std::tie(p, avail) = copy(lgsp.path, avail, p);
204 std::tie(p, avail) = copy(" HTTP/", avail, p);
205 std::tie(p, avail) = copy(util::utos(lgsp.major).c_str(), avail, p);
206 if (lgsp.major < 2) {
207 std::tie(p, avail) = copy(".", avail, p);
208 std::tie(p, avail) = copy(util::utos(lgsp.minor).c_str(), avail, p);
211 case SHRPX_LOGF_STATUS:
212 std::tie(p, avail) = copy(util::utos(lgsp.status).c_str(), avail, p);
214 case SHRPX_LOGF_BODY_BYTES_SENT:
216 copy(util::utos(lgsp.body_bytes_sent).c_str(), avail, p);
218 case SHRPX_LOGF_HTTP:
220 auto hd = downstream->get_request_header(lf.value.get());
222 std::tie(p, avail) = copy((*hd).value.c_str(), avail, p);
227 std::tie(p, avail) = copy("-", avail, p);
230 case SHRPX_LOGF_REMOTE_PORT:
231 std::tie(p, avail) = copy(lgsp.remote_port, avail, p);
233 case SHRPX_LOGF_SERVER_PORT:
234 std::tie(p, avail) = copy(util::utos(lgsp.server_port).c_str(), avail, p);
236 case SHRPX_LOGF_REQUEST_TIME: {
237 auto t = std::chrono::duration_cast<std::chrono::milliseconds>(
238 lgsp.request_end_time - lgsp.request_start_time).count();
240 auto frac = util::utos(t % 1000);
241 auto sec = util::utos(t / 1000);
242 if (frac.size() < 3) {
243 frac = std::string(3 - frac.size(), '0') + frac;
248 std::tie(p, avail) = copy(sec.c_str(), avail, p);
251 std::tie(p, avail) = copy(util::utos(lgsp.pid).c_str(), avail, p);
253 case SHRPX_LOGF_ALPN:
254 std::tie(p, avail) = copy(lgsp.alpn, avail, p);
256 case SHRPX_LOGF_NONE:
265 if (get_config()->accesslog_syslog) {
266 syslog(LOG_INFO, "%s", buf);
273 auto nwrite = p - buf;
274 while (write(lgconf->accesslog_fd, buf, nwrite) == -1 && errno == EINTR)
278 int reopen_log_files() {
281 auto lgconf = log_config();
283 if (lgconf->accesslog_fd != -1) {
284 close(lgconf->accesslog_fd);
285 lgconf->accesslog_fd = -1;
288 if (!get_config()->accesslog_syslog && get_config()->accesslog_file) {
290 lgconf->accesslog_fd =
291 util::reopen_log_file(get_config()->accesslog_file.get());
293 if (lgconf->accesslog_fd == -1) {
294 LOG(ERROR) << "Failed to open accesslog file "
295 << get_config()->accesslog_file.get();
300 int new_errorlog_fd = -1;
302 if (!get_config()->errorlog_syslog && get_config()->errorlog_file) {
304 new_errorlog_fd = util::reopen_log_file(get_config()->errorlog_file.get());
306 if (new_errorlog_fd == -1) {
307 if (lgconf->errorlog_fd != -1) {
308 LOG(ERROR) << "Failed to open errorlog file "
309 << get_config()->errorlog_file.get();
311 std::cerr << "Failed to open errorlog file "
312 << get_config()->errorlog_file.get() << std::endl;
319 if (lgconf->errorlog_fd != -1) {
320 close(lgconf->errorlog_fd);
321 lgconf->errorlog_fd = -1;
322 lgconf->errorlog_tty = false;
325 if (new_errorlog_fd != -1) {
326 lgconf->errorlog_fd = new_errorlog_fd;
327 lgconf->errorlog_tty = isatty(lgconf->errorlog_fd);
333 void redirect_stderr_to_errorlog() {
334 auto lgconf = log_config();
336 if (get_config()->errorlog_syslog || lgconf->errorlog_fd == -1) {
340 dup2(lgconf->errorlog_fd, STDERR_FILENO);