]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Support rotate=N option on access_log
authorAmos Jeffries <squid3@treenet.co.nz>
Sun, 18 Jan 2015 16:34:13 +0000 (08:34 -0800)
committerAmos Jeffries <squid3@treenet.co.nz>
Sun, 18 Jan 2015 16:34:13 +0000 (08:34 -0800)
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.

14 files changed:
src/adaptation/icap/icap_log.cc
src/cache_cf.cc
src/cf.data.pre
src/log/CustomLog.h
src/log/File.cc
src/log/File.h
src/log/ModDaemon.cc
src/log/ModStdio.cc
src/log/ModSyslog.cc
src/log/ModUdp.cc
src/log/TcpLogger.cc
src/log/TcpLogger.h
src/log/access_log.cc
src/store_log.cc

index 29f41f5bd552121f450dea7160bcfab769eede69..a2c06d0717946423c496693ca76ce7a18c7e9485 100644 (file)
@@ -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);
         }
     }
 }
index 6b571d5ddd206b791af4baaa763ed73127ed787c..39f82accdc5407828c5d9fbb0214d985be1b4d7c 100644 (file)
@@ -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);
 
index d15691cb34e334873e01bedf0e7d7b81c797bafd..21d149dc2bf51ebcabe66d4887db93bcf4b064dc 100644 (file)
@@ -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
        <pid>'.
 
-       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
index f93a2ce66d13ba403d87388af41e27cf0ff436ed..5b2b588cbcf278d65be466f64e8b7b458fc96105 100644 (file)
@@ -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_ */
index a6e6fc642d871066ba6e1f0e3d699e2c67386fb8..c80d2d62cda8892fe65baeea7c751b1f5b74d800 100644 (file)
@@ -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
index 62c73e767e355c303f1036e306cd67486fcd5014..fd9f646c9e20dd7066a968eecaa22a2824d16fda 100644 (file)
@@ -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;
index 7f563d8fc62a758671c74dab861776bf263265a6..35b69c620940b17f390b6b032cb382d6a6c29853 100644 (file)
@@ -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);
index 2d6c461adfefbb1a1ea23a6614554fe5578403de..bcf9b552f49f08493bea6496773625c8d805f410 100644 (file)
@@ -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);
     }
index 0079a6479f22cb33a4b2fafed1b275d6310b6f58..b0ba9edd78d412804ade4e9c2de3e681dd9f266a 100644 (file)
@@ -120,7 +120,7 @@ logfile_mod_syslog_flush(Logfile *)
 }
 
 static void
-logfile_mod_syslog_rotate(Logfile *)
+logfile_mod_syslog_rotate(Logfile *, const int16_t)
 {
 }
 
index a1d94720229b43d1dd3a8ac44dd63e68852e8203..eaaccbc947b641a87fb0e462f2d46500b6a15e07 100644 (file)
@@ -104,7 +104,7 @@ logfile_mod_udp_lineend(Logfile *)
 }
 
 static void
-logfile_mod_udp_rotate(Logfile *)
+logfile_mod_udp_rotate(Logfile *, const int16_t)
 {
 }
 
index a4b272429a831f12c2a0acc172ff27525782a094..67f6e2fca880fc9b09c7f9a87b6f20d656853012 100644 (file)
@@ -423,7 +423,7 @@ Log::TcpLogger::EndLine(Logfile * lf)
 }
 
 void
-Log::TcpLogger::Rotate(Logfile *)
+Log::TcpLogger::Rotate(Logfile *, const int16_t)
 {
 }
 
index 0a3d43b1d855c346cd961defa489a6e99d577c37..4a4b9531d040fe866a8787ebbd12288c36c89491 100644 (file)
@@ -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);
index 157a603637c224336d773972f115df16880dbd5f..4b383b9f16d381d252ef9a9373cc30173fd48938 100644 (file)
@@ -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
 }
index 7b44cf516e93df681e6415238163aa9bedb2d1f3..b29bd38bfb77c3a0d5978df0cb229474a7d612e4 100644 (file)
@@ -95,7 +95,7 @@ storeLogRotate(void)
     if (NULL == storelog)
         return;
 
-    logfileRotate(storelog);
+    logfileRotate(storelog, Config.Log.rotateNumber);
 }
 
 void