On Saturday August 28 2004 9:30, Bruce Momjian wrote:
> Are we going to change this before beta2? I have not seen a final
> patch yet.
Attached is a revised patch:
+ changes postgresql.conf option 'log_filename_prefix' to 'log_filename',
and adds strftime() escape interpolation;
+ set default log_filename to 'postgresql-%Y-%m-%d_%H%M%S.log';
+ added postgresql.conf boolean option 'log_truncate_on_rotation', default
false;
+ log truncation is performed only on time-driven rotations and only when
log_truncate_on_rotation is true;
I did not add UTC offset logic nor logic to shift to top of the hour/day for
rotation periods of 60/1440 minutes, but would like to add that shortly if
time permits.
Ed
Index: doc/src/sgml/runtime.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/runtime.sgml,v
retrieving revision 1.279
diff -C1 -r1.279 runtime.sgml
*** doc/src/sgml/runtime.sgml 24 Aug 2004 00:06:50 -0000 1.279
--- doc/src/sgml/runtime.sgml 30 Aug 2004 01:46:31 -0000
***************
*** 1927,1930 ****
! <varlistentry id="guc-log-filename-prefix" xreflabel="log_filename_prefix">
! <term><varname>log_filename_prefix</varname> (<type>string</type>)</term>
<listitem>
--- 1927,1930 ----
! <varlistentry id="guc-log-filename" xreflabel="log_filename">
! <term><varname>log_filename</varname> (<type>string</type>)</term>
<listitem>
***************
*** 1932,1938 ****
When <varname>redirect_stderr</> is enabled, this option
! sets the prefix of the file names of the created log files.
! The postmaster PID and the current time are appended to this
! prefix to form an exact log file name.
! This option can only be set at server start or in the
! <filename>postgresql.conf</filename> configuration file.
</para>
--- 1932,1955 ----
When <varname>redirect_stderr</> is enabled, this option
! sets the base name of the file name of the created log files.
! Any strftime escapes will be interpolated per strftime().
! If no strftime escapes are present, each new rotation will
! append the epoch of the new log file's open time. For example,
! if <varname>log_filename</> where 'server_log', then an
! example log file upon rotation might be 'server_log.1093827753'
! for a log starting at Sun Aug 29 19:02:33 2004 MST.
! </para>
! </listitem>
! </varlistentry>
!
! <varlistentry id="guc-log-truncate-on-rotation" xreflabel="log_truncate_on_rotation">
! <term><varname>log_truncate_on_rotation</varname> (<type>boolean</type>)</term>
! <listitem>
! <para>
! When log rotation is enabled, this option will cause the
! log rotation to truncate any existing log file by the same
! name as the new log file in the rotation. Note this only
! applies to time-based log rotations; no log files will be
! truncated or overwritten on restarts or on size-based rotations.
! Set to false to append to any existing files rather than
! truncate them.
</para>
Index: src/backend/postmaster/syslogger.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/syslogger.c,v
retrieving revision 1.5
diff -C1 -r1.5 syslogger.c
*** src/backend/postmaster/syslogger.c 9 Aug 2004 20:28:48 -0000 1.5
--- src/backend/postmaster/syslogger.c 30 Aug 2004 01:46:33 -0000
***************
*** 64,66 ****
char * Log_directory = "pg_log";
! char * Log_filename_prefix = "postgresql-";
--- 64,67 ----
char * Log_directory = "pg_log";
! char * Log_filename = "postgresql-%Y-%m-%d_%H%M%S.log";
! bool Log_truncate_on_rotation = false;
***************
*** 82,83 ****
--- 83,86 ----
+ static bool time_based_rotation = false;
+
/* These must be exported for EXEC_BACKEND case ... annoying */
***************
*** 122,123 ****
--- 125,127 ----
char currentLogDir[MAXPGPATH];
+ char currentLogFilename[MAXPGPATH];
***************
*** 219,222 ****
last_rotation_time = time(NULL);
! /* remember active logfile directory */
strncpy(currentLogDir, Log_directory, MAXPGPATH);
--- 223,227 ----
last_rotation_time = time(NULL);
! /* remember active logfile directory and filename */
strncpy(currentLogDir, Log_directory, MAXPGPATH);
+ strncpy(currentLogFilename, Log_filename, MAXPGPATH);
***************
*** 234,235 ****
--- 239,242 ----
+ time_based_rotation = false;
+
if (got_SIGHUP)
***************
*** 240,247 ****
/*
! * Check if the log directory changed in postgresql.conf. If so,
! * force rotation to make sure we're writing the logfiles in the
! * right place.
! *
! * XXX is it worth responding similarly to a change of
! * Log_filename_prefix?
*/
--- 247,251 ----
/*
! * Check if the log directory or filename prefix changed in
! * postgresql.conf. If so, force rotation to make sure we're
! * writing the logfiles in the right place.
*/
***************
*** 252,253 ****
--- 256,262 ----
}
+ if (strncmp(Log_filename, currentLogFilename, MAXPGPATH) != 0)
+ {
+ strncpy(currentLogFilename, Log_filename, MAXPGPATH);
+ rotation_requested = true;
+ }
}
***************
*** 266,268 ****
if (elapsed_secs >= Log_RotationAge * 60)
! rotation_requested = true;
}
--- 275,277 ----
if (elapsed_secs >= Log_RotationAge * 60)
! rotation_requested = time_based_rotation = true;
}
***************
*** 737,739 ****
! fh = fopen(filename, "a");
if (!fh)
--- 746,752 ----
! if (Log_truncate_on_rotation && time_based_rotation)
! fh = fopen(filename, "w");
! else
! fh = fopen(filename, "a");
!
if (!fh)
***************
*** 791,796 ****
char *filename;
! char stamptext[128];
!
! pg_strftime(stamptext, sizeof(stamptext), "%Y-%m-%d_%H%M%S",
! pg_localtime(×tamp));
--- 804,807 ----
char *filename;
! int len;
! struct pg_tm *now;
***************
*** 799,807 ****
if (is_absolute_path(Log_directory))
! snprintf(filename, MAXPGPATH, "%s/%s%05u_%s.log",
! Log_directory, Log_filename_prefix,
! (unsigned int) PostmasterPid, stamptext);
else
! snprintf(filename, MAXPGPATH, "%s/%s/%s%05u_%s.log",
! DataDir, Log_directory, Log_filename_prefix,
! (unsigned int) PostmasterPid, stamptext);
--- 810,827 ----
if (is_absolute_path(Log_directory))
! snprintf(filename, MAXPGPATH, "%s/", Log_directory);
else
! snprintf(filename, MAXPGPATH, "%s/%s/", DataDir, Log_directory);
!
! len = strnlen(filename, MAXPGPATH);
! now = pg_gmtime(×tamp);
!
! if (strstr(Log_filename, "%"))
! pg_strftime((char*) (filename + len), MAXPGPATH - len, Log_filename, now);
! else
! {
! /* no strftime() escapes, so append timestamp to new filename */
! snprintf((char*) (filename + len), MAXPGPATH - len, "%s", Log_filename);
! len = strnlen(filename, MAXPGPATH);
! snprintf((char*) (filename + len), MAXPGPATH - len, ".%d", timestamp);
! }
Index: src/backend/utils/misc/guc.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/misc/guc.c,v
retrieving revision 1.232
diff -C1 -r1.232 guc.c
*** src/backend/utils/misc/guc.c 16 Aug 2004 02:12:29 -0000 1.232
--- src/backend/utils/misc/guc.c 30 Aug 2004 01:46:39 -0000
***************
*** 813,814 ****
--- 813,822 ----
},
+ {
+ {"log_truncate_on_rotation", PGC_POSTMASTER, LOGGING_WHERE,
+ gettext_noop("Truncate existing logfiles by same name during log rotation"),
+ NULL
+ },
+ &Log_truncate_on_rotation,
+ false, NULL, NULL
+ },
***************
*** 1673,1681 ****
},
{
! {"log_filename_prefix", PGC_SIGHUP, LOGGING_WHERE,
! gettext_noop("Prefix for file names created in the log_directory."),
NULL
},
! &Log_filename_prefix,
! "postgresql-", NULL, NULL
},
--- 1681,1690 ----
},
+
{
! {"log_filename", PGC_SIGHUP, LOGGING_WHERE,
! gettext_noop("File name to be used for log files."),
NULL
},
! &Log_filename,
! "postgresql-%Y-%m-%d_%H%M%S.log", NULL, NULL
},
Index: src/backend/utils/misc/postgresql.conf.sample
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/misc/postgresql.conf.sample,v
retrieving revision 1.126
diff -C1 -r1.126 postgresql.conf.sample
*** src/backend/utils/misc/postgresql.conf.sample 12 Aug 2004 19:03:36 -0000 1.126
--- src/backend/utils/misc/postgresql.conf.sample 30 Aug 2004 01:46:40 -0000
***************
*** 175,177 ****
# May be specified absolute or relative to PGDATA
! #log_filename_prefix = 'postgresql_' # Prefix for logfile names.
#log_rotation_age = 1440 # Automatic rotation of logfiles will happen after
--- 175,185 ----
# May be specified absolute or relative to PGDATA
! #log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # Logfile name, may include
! # strftime() escapes
! #log_truncate_on_rotation = false # If true, any existing logfile by the
! # same name as the new logfile upon time-driven
! # rotation only will be truncated. Note
! # truncation only occurs on time-driven rotation,
! # not on restarts or size-driven rotation.
! # Default is false, meaning append to existing
! # files in all cases.
#log_rotation_age = 1440 # Automatic rotation of logfiles will happen after
Index: src/include/postmaster/syslogger.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/postmaster/syslogger.h,v
retrieving revision 1.1
diff -C1 -r1.1 syslogger.h
*** src/include/postmaster/syslogger.h 5 Aug 2004 23:32:12 -0000 1.1
--- src/include/postmaster/syslogger.h 30 Aug 2004 01:46:40 -0000
***************
*** 19,21 ****
extern char *Log_directory;
! extern char *Log_filename_prefix;
--- 19,22 ----
extern char *Log_directory;
! extern char *Log_filename;
! extern bool Log_truncate_on_rotation;
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend