You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by co...@apache.org on 2023/03/03 14:20:27 UTC

svn commit: r1908025 - in /httpd/httpd/branches/2.4.x: ./ changes-entries/rotatlogs_truncate_rotated.txt docs/manual/programs/rotatelogs.xml support/rotatelogs.c

Author: covener
Date: Fri Mar  3 14:20:27 2023
New Revision: 1908025

URL: http://svn.apache.org/viewvc?rev=1908025&view=rev
Log:
backport 1906428,1906433,1907993 from trunk

add rotatelogs -T to truncate all but the first-opened file.

+ docs tweak for compat



Added:
    httpd/httpd/branches/2.4.x/changes-entries/rotatlogs_truncate_rotated.txt
      - copied unchanged from r1906433, httpd/httpd/trunk/changes-entries/rotatlogs_truncate_rotated.txt
Modified:
    httpd/httpd/branches/2.4.x/   (props changed)
    httpd/httpd/branches/2.4.x/docs/manual/programs/rotatelogs.xml
    httpd/httpd/branches/2.4.x/support/rotatelogs.c

Propchange: httpd/httpd/branches/2.4.x/
------------------------------------------------------------------------------
  Merged /httpd/httpd/trunk:r1906428,1906433,1907993

Modified: httpd/httpd/branches/2.4.x/docs/manual/programs/rotatelogs.xml
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/docs/manual/programs/rotatelogs.xml?rev=1908025&r1=1908024&r2=1908025&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/docs/manual/programs/rotatelogs.xml (original)
+++ httpd/httpd/branches/2.4.x/docs/manual/programs/rotatelogs.xml Fri Mar  3 14:20:27 2023
@@ -104,6 +104,13 @@ the filename, however format strings con
 will be respected.
 </dd>
 
+<dt><code>-T</code></dt>
+<dd>Causes all but the initial logfile to be truncated when opened.
+This is useful when the format string contains something that will
+loop around, such as the day of the month. Available in 2.4.56 and later.
+</dd>
+
+
 <dt><code>-v</code></dt>
 <dd>Produce verbose output on STDERR. The output contains
 the result of the configuration parsing, and all file open and
@@ -242,6 +249,18 @@ an offset.</dd>
      in this scenario that a separate process (such as tail) would
      process the file in real time.</p>
 
+<example>
+<highlight language="config">
+     CustomLog "|bin/rotatelogs -T /var/log/logfile.%d 86400" common
+</highlight>
+</example>
+
+<p>If the server is started (or restarted) on the first of the month, this 
+appends to <code>/var/log/logfile.01</code>.  When a log entry is written on the
+second of the month, <code>/var/log/logfile.02</code> is truncated and new entries
+will be added to the top. This example keeps approximately 1 months worth of 
+logs without external maintenance.</p>
+
 </section>
 
 <section id="portability"><title>Portability</title>

Modified: httpd/httpd/branches/2.4.x/support/rotatelogs.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/support/rotatelogs.c?rev=1908025&r1=1908024&r2=1908025&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/support/rotatelogs.c (original)
+++ httpd/httpd/branches/2.4.x/support/rotatelogs.c Fri Mar  3 14:20:27 2023
@@ -65,6 +65,7 @@ struct rotate_config {
     int echo;
     char *szLogRoot;
     int truncate;
+    int truncate_rotated_only;
     const char *linkfile;
     const char *postrotate_prog;
 #if APR_FILES_AS_SOCKETS
@@ -109,9 +110,9 @@ static void usage(const char *argv0, con
     }
     fprintf(stderr,
 #if APR_FILES_AS_SOCKETS
-            "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-D] [-t] [-e] [-c] [-n number] <logfile> "
+            "Usage: %s [-vlfDtTec] [-L linkname] [-p prog] [-n number] <logfile> "
 #else
-            "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-D] [-t] [-e] [-n number] <logfile> "
+            "Usage: %s [-vlfDtTe] [-L linkname] [-p prog] [-n number] <logfile> "
 #endif
             "{<rotation time in seconds>|<rotation size>(B|K|M|G)} "
             "[offset minutes from UTC]\n\n",
@@ -145,6 +146,7 @@ static void usage(const char *argv0, con
             "  -f       Force opening of log on program start.\n"
             "  -D       Create parent directories of log file.\n" 
             "  -t       Truncate logfile instead of rotating, tail friendly.\n"
+            "  -T       Truncate logfiles opened for rotation, but not the initial logfile.\n"
             "  -e       Echo log to stdout for further processing.\n"
 #if APR_FILES_AS_SOCKETS
             "  -c       Create log even if it is empty.\n"
@@ -380,6 +382,8 @@ static void doRotate(rotate_config_t *co
     apr_status_t rv;
     struct logfile newlog;
     int thisLogNum = -1;
+    int oldreason = status->rotateReason;
+    int truncate = config->truncate;
 
     /* Retrieve local-time-adjusted-Unix-time. */
     now = get_now(config, &offset);
@@ -459,8 +463,17 @@ static void doRotate(rotate_config_t *co
     if (config->verbose) {
         fprintf(stderr, "Opening file %s\n", newlog.name);
     }
-    rv = apr_file_open(&newlog.fd, newlog.name, APR_WRITE | APR_CREATE | APR_APPEND
-                       | (config->truncate || (config->num_files > 0 && status->current.fd) ? APR_TRUNCATE : 0), 
+
+    if (!truncate) {
+        /* -n and -T truncate subsequent files only. */
+        if (status->current.fd &&
+               (config->num_files > 0 || config->truncate_rotated_only)) {
+            truncate = 1;
+        }
+    }
+    rv = apr_file_open(&newlog.fd, newlog.name,
+                       APR_WRITE | APR_CREATE | APR_APPEND
+                       | (truncate ? APR_TRUNCATE : 0),
                        APR_OS_DEFAULT, newlog.pool);
     if (rv == APR_SUCCESS) {
         /* Handle post-rotate processing. */
@@ -474,6 +487,19 @@ static void doRotate(rotate_config_t *co
 
         /* New log file is now 'current'. */
         status->current = newlog;
+
+        /* The first write to the initial file hasn't checked for size.
+         * In the normalized timestamp case and the custom strftime case with
+         * any reasonable accuracy, it's futile as the rotation will pick the
+         * same filename again. 
+         * For -n, when not truncating, check and rotate.
+         */
+        if (config->num_files > 0 && oldreason == ROTATE_NEW && !config->truncate) {
+            checkRotate(config, status);
+            if (status->rotateReason != ROTATE_NONE) {
+                doRotate(config, status);
+            }
+        }
     }
     else {
         char *error = apr_psprintf(newlog.pool, "%pm", &rv);
@@ -585,9 +611,9 @@ int main (int argc, const char * const a
     apr_pool_create(&status.pool, NULL);
     apr_getopt_init(&opt, status.pool, argc, argv);
 #if APR_FILES_AS_SOCKETS
-    while ((rv = apr_getopt(opt, "lL:p:fDtvecn:", &c, &opt_arg)) == APR_SUCCESS) {
+    while ((rv = apr_getopt(opt, "lL:p:fDtTvecn:", &c, &opt_arg)) == APR_SUCCESS) {
 #else
-    while ((rv = apr_getopt(opt, "lL:p:fDtven:", &c, &opt_arg)) == APR_SUCCESS) {
+    while ((rv = apr_getopt(opt, "lL:p:fDtTven:", &c, &opt_arg)) == APR_SUCCESS) {
 #endif
         switch (c) {
         case 'l':
@@ -612,6 +638,9 @@ int main (int argc, const char * const a
         case 't':
             config.truncate = 1;
             break;
+        case 'T':
+            config.truncate_rotated_only = 1;
+            break;
         case 'v':
             config.verbose = 1;
             break;