From: Amos Jeffries Date: Sun, 18 Jan 2015 16:34:13 +0000 (-0800) Subject: Support rotate=N option on access_log X-Git-Tag: merge-candidate-3-v1~334 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=efc238716eb68db2888faac60423184a894f5483;p=thirdparty%2Fsquid.git Support rotate=N option on access_log Add a rotate=N option to access_log directive to set per-log what the retained log count will be. At present it is only used by the stdio: logging module, which is also the only one to use logfile_rotate directive. If this option is absent (as will be the common case) the log rotation defaults to using the value of logfile_rotate directive. Also, add missing dump output of other access_log options if they differ from the defaults. The use-cases for this are: 1) Unix fifo logging requires all the stdio: module operations except that the normal rotate/rename operation is NOT performed on the fifo socket. It makes more sense to add this option which can also meet case #2 than to create a whole new module just for fifo. 2) managing only some access_log files with a third-party log manager. Those specific logs need rotate=0, but the Squid managed logs may require non-0 values. --- diff --git a/src/adaptation/icap/icap_log.cc b/src/adaptation/icap/icap_log.cc index 29f41f5bd5..a2c06d0717 100644 --- a/src/adaptation/icap/icap_log.cc +++ b/src/adaptation/icap/icap_log.cc @@ -52,7 +52,7 @@ icapLogRotate() { for (CustomLog* log = Config.Log.icaplogs; log; log = log->next) { if (log->logfile) { - logfileRotate(log->logfile); + logfileRotate(log->logfile, Config.Log.rotateNumber); } } } diff --git a/src/cache_cf.cc b/src/cache_cf.cc index 6b571d5ddd..39f82accdc 100644 --- a/src/cache_cf.cc +++ b/src/cache_cf.cc @@ -4096,6 +4096,7 @@ parse_access_log(CustomLog ** logs) cl->filename = xstrdup(filename); cl->type = Log::Format::CLF_UNKNOWN; + cl->rotateCount = -1; // default: use global logfile_rotate setting. const char *token = ConfigParser::PeekAtToken(); if (!token) { // style #1 @@ -4119,6 +4120,8 @@ parse_access_log(CustomLog ** logs) } } else if (strncasecmp(token, "buffer-size=", 12) == 0) { parseBytesOptionValue(&cl->bufferSize, B_BYTES_STR, token+12); + } else if (strncasecmp(token, "rotate=", 7) == 0) { + cl->rotateCount = xatoi(token + 7); } else if (strncasecmp(token, "logformat=", 10) == 0) { setLogformat(cl, token+10, true); } else if (!strchr(token, '=')) { @@ -4223,42 +4226,53 @@ dump_access_log(StoreEntry * entry, const char *name, CustomLog * logs) switch (log->type) { case Log::Format::CLF_CUSTOM: - storeAppendPrintf(entry, "%s %s", log->filename, log->logFormat->name); + storeAppendPrintf(entry, "%s logformat=%s", log->filename, log->logFormat->name); break; case Log::Format::CLF_NONE: - storeAppendPrintf(entry, "none"); + storeAppendPrintf(entry, "logformat=none"); break; case Log::Format::CLF_SQUID: - storeAppendPrintf(entry, "%s squid", log->filename); + storeAppendPrintf(entry, "%s logformat=squid", log->filename); break; case Log::Format::CLF_COMBINED: - storeAppendPrintf(entry, "%s combined", log->filename); + storeAppendPrintf(entry, "%s logformat=combined", log->filename); break; case Log::Format::CLF_COMMON: - storeAppendPrintf(entry, "%s common", log->filename); + storeAppendPrintf(entry, "%s logformat=common", log->filename); break; #if ICAP_CLIENT case Log::Format::CLF_ICAP_SQUID: - storeAppendPrintf(entry, "%s icap_squid", log->filename); + storeAppendPrintf(entry, "%s logformat=icap_squid", log->filename); break; #endif case Log::Format::CLF_USERAGENT: - storeAppendPrintf(entry, "%s useragent", log->filename); + storeAppendPrintf(entry, "%s logformat=useragent", log->filename); break; case Log::Format::CLF_REFERER: - storeAppendPrintf(entry, "%s referrer", log->filename); + storeAppendPrintf(entry, "%s logformat=referrer", log->filename); break; case Log::Format::CLF_UNKNOWN: break; } + // default is on-error=die + if (!log->fatal) + storeAppendPrintf(entry, " on-error=drop"); + + // default: 64KB + if (log->bufferSize != 64*1024) + storeAppendPrintf(entry, " buffer-size=%d", log->bufferSize); + + if (log->rotateCount >= 0) + storeAppendPrintf(entry, " rotate=%d", log->rotateCount); + if (log->aclList) dump_acl_list(entry, log->aclList); diff --git a/src/cf.data.pre b/src/cf.data.pre index d15691cb34..21d149dc2b 100644 --- a/src/cf.data.pre +++ b/src/cf.data.pre @@ -4248,6 +4248,15 @@ DOC_START support has not been tested for modules other than tcp. + rotate=N Specifies the number of log file rotations to + make when you run 'squid -k rotate'. The default + is to obey the logfile_rotate directive. Setting + rotate=0 will disable the file name rotation, + but the log files are still closed and re-opened. + This will enable you to rename the logfiles + yourself just before sending the rotate signal. + Only supported by the stdio module. + ===== Modules Currently available ===== none Do not log any requests matching these ACL. @@ -4468,13 +4477,20 @@ TYPE: int DEFAULT: 10 LOC: Config.Log.rotateNumber DOC_START - Specifies the number of logfile rotations to make when you + Specifies the default number of logfile rotations to make when you type 'squid -k rotate'. The default is 10, which will rotate with extensions 0 through 9. Setting logfile_rotate to 0 will disable the file name rotation, but the logfiles are still closed and re-opened. This will enable you to rename the logfiles yourself just before sending the rotate signal. + Note, from Squid-3.1 this option is only a default for cache.log, + that log can be rotated separately by using debug_options. + + Note, from Squid-3.6 this option is only a default for access.log + recorded by stdio: module. Those logs can be rotated separately by + using the rotate=N option on their access_log directive. + Note, the 'squid -k rotate' command normally sends a USR1 signal to the running squid process. In certain situations (e.g. on Linux with Async I/O), USR1 is used for other @@ -4482,8 +4498,6 @@ DOC_START in the habit of using 'squid -k rotate' instead of 'kill -USR1 '. - Note, from Squid-3.1 this option is only a default for cache.log, - that log can be rotated separately by using debug_options. DOC_END NAME: mime_table diff --git a/src/log/CustomLog.h b/src/log/CustomLog.h index f93a2ce66d..5b2b588cbc 100644 --- a/src/log/CustomLog.h +++ b/src/log/CustomLog.h @@ -19,7 +19,7 @@ namespace Format class Format; } -/// representaiton of a custom log directive. Currently a POD. +/// representation of a custom log directive. class CustomLog { public: @@ -33,6 +33,8 @@ public: size_t bufferSize; /// whether unrecoverable errors (e.g., dropping a log record) kill worker bool fatal; + /// How many log files to retain when rotating. Default: obey logfile_rotate + int16_t rotateCount; }; #endif /* SQUID_CUSTOMLOG_H_ */ diff --git a/src/log/File.cc b/src/log/File.cc index a6e6fc642d..c80d2d62cd 100644 --- a/src/log/File.cc +++ b/src/log/File.cc @@ -84,10 +84,10 @@ logfileClose(Logfile * lf) } void -logfileRotate(Logfile * lf) +logfileRotate(Logfile * lf, int16_t rotateCount) { debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path); - lf->f_rotate(lf); + lf->f_rotate(lf, rotateCount); } void diff --git a/src/log/File.h b/src/log/File.h index 62c73e767e..fd9f646c9e 100644 --- a/src/log/File.h +++ b/src/log/File.h @@ -31,7 +31,7 @@ typedef void LOGLINESTART(Logfile *); typedef void LOGWRITE(Logfile *, const char *, size_t len); typedef void LOGLINEEND(Logfile *); typedef void LOGFLUSH(Logfile *); -typedef void LOGROTATE(Logfile *); +typedef void LOGROTATE(Logfile *, const int16_t); typedef void LOGCLOSE(Logfile *); class Logfile @@ -60,7 +60,7 @@ public: /* Legacy API */ Logfile *logfileOpen(const char *path, size_t bufsz, int); void logfileClose(Logfile * lf); -void logfileRotate(Logfile * lf); +void logfileRotate(Logfile * lf, int16_t rotateCount); void logfileWrite(Logfile * lf, char *buf, size_t len); void logfileFlush(Logfile * lf); void logfilePrintf(Logfile * lf, const char *fmt,...) PRINTF_FORMAT_ARG2; diff --git a/src/log/ModDaemon.cc b/src/log/ModDaemon.cc index 7f563d8fc6..35b69c6209 100644 --- a/src/log/ModDaemon.cc +++ b/src/log/ModDaemon.cc @@ -269,7 +269,7 @@ logfile_mod_daemon_close(Logfile * lf) } static void -logfile_mod_daemon_rotate(Logfile * lf) +logfile_mod_daemon_rotate(Logfile * lf, const int16_t) { char tb[3]; debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path); diff --git a/src/log/ModStdio.cc b/src/log/ModStdio.cc index 2d6c461adf..bcf9b552f4 100644 --- a/src/log/ModStdio.cc +++ b/src/log/ModStdio.cc @@ -98,14 +98,13 @@ logfile_mod_stdio_flush(Logfile * lf) } static void -logfile_mod_stdio_rotate(Logfile * lf) +logfile_mod_stdio_rotate(Logfile * lf, const int16_t nRotate) { #ifdef S_ISREG struct stat sb; #endif - int i; char from[MAXPATHLEN]; char to[MAXPATHLEN]; l_stdio_t *ll = (l_stdio_t *) lf->data; @@ -123,7 +122,7 @@ logfile_mod_stdio_rotate(Logfile * lf) debugs(0, DBG_IMPORTANT, "Rotate log file " << lf->path); /* Rotate numbers 0 through N up one */ - for (i = Config.Log.rotateNumber; i > 1;) { + for (int16_t i = nRotate; i > 1;) { --i; snprintf(from, MAXPATHLEN, "%s.%d", realpath, i - 1); snprintf(to, MAXPATHLEN, "%s.%d", realpath, i); @@ -135,7 +134,7 @@ logfile_mod_stdio_rotate(Logfile * lf) file_close(ll->fd); /* always close */ - if (Config.Log.rotateNumber > 0) { + if (nRotate > 0) { snprintf(to, MAXPATHLEN, "%s.%d", realpath, 0); xrename(realpath, to); } diff --git a/src/log/ModSyslog.cc b/src/log/ModSyslog.cc index 0079a6479f..b0ba9edd78 100644 --- a/src/log/ModSyslog.cc +++ b/src/log/ModSyslog.cc @@ -120,7 +120,7 @@ logfile_mod_syslog_flush(Logfile *) } static void -logfile_mod_syslog_rotate(Logfile *) +logfile_mod_syslog_rotate(Logfile *, const int16_t) { } diff --git a/src/log/ModUdp.cc b/src/log/ModUdp.cc index a1d9472022..eaaccbc947 100644 --- a/src/log/ModUdp.cc +++ b/src/log/ModUdp.cc @@ -104,7 +104,7 @@ logfile_mod_udp_lineend(Logfile *) } static void -logfile_mod_udp_rotate(Logfile *) +logfile_mod_udp_rotate(Logfile *, const int16_t) { } diff --git a/src/log/TcpLogger.cc b/src/log/TcpLogger.cc index a4b272429a..67f6e2fca8 100644 --- a/src/log/TcpLogger.cc +++ b/src/log/TcpLogger.cc @@ -423,7 +423,7 @@ Log::TcpLogger::EndLine(Logfile * lf) } void -Log::TcpLogger::Rotate(Logfile *) +Log::TcpLogger::Rotate(Logfile *, const int16_t) { } diff --git a/src/log/TcpLogger.h b/src/log/TcpLogger.h index 0a3d43b1d8..4a4b9531d0 100644 --- a/src/log/TcpLogger.h +++ b/src/log/TcpLogger.h @@ -61,7 +61,7 @@ private: static void WriteLine(Logfile *lf, const char *buf, size_t len); static void StartLine(Logfile *lf); static void EndLine(Logfile *lf); - static void Rotate(Logfile *lf); + static void Rotate(Logfile *lf, const int16_t); static void Close(Logfile *lf); static TcpLogger *StillLogging(Logfile *lf); diff --git a/src/log/access_log.cc b/src/log/access_log.cc index 157a603637..4b383b9f16 100644 --- a/src/log/access_log.cc +++ b/src/log/access_log.cc @@ -194,13 +194,14 @@ accessLogRotate(void) for (log = Config.Log.accesslogs; log; log = log->next) { if (log->logfile) { - logfileRotate(log->logfile); + int16_t rc = (log->rotateCount >= 0 ? log->rotateCount : Config.Log.rotateNumber); + logfileRotate(log->logfile, rc); } } #if HEADERS_LOG - logfileRotate(headerslog); + logfileRotate(headerslog, Config.Log.rotateNumber); #endif } diff --git a/src/store_log.cc b/src/store_log.cc index 7b44cf516e..b29bd38bfb 100644 --- a/src/store_log.cc +++ b/src/store_log.cc @@ -95,7 +95,7 @@ storeLogRotate(void) if (NULL == storelog) return; - logfileRotate(storelog); + logfileRotate(storelog, Config.Log.rotateNumber); } void