Attached is a patch which replaces the 'log_filename_prefix' configuration 
directive with a similar 'log_filename' directive.  It differs from the 
former in the following ways:

        + allows embedded strftime() escapes ala Apache's rotatelogs;
        + eliminates hard-coded embedding of the postmaster pid;
        + makes the current hard-coded timestamp configurable;
        + changes the default log filename to exclude the PID;

This patch enables us to continue using our existing log-handling utilities 
and filenaming conventions which we now use with Apache's rotatelogs.

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	27 Aug 2004 17:37:09 -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,1936 ****
            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
--- 1932,1935 ----
            When <varname>redirect_stderr</> is enabled, this option
!           sets the name of the created log files.  Any embedded 
!           strftime escapes sequences are interpolated per strftime().
           This option can only be set at server start or in the
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	27 Aug 2004 17:37:11 -0000
***************
*** 64,66 ****
  char *      Log_directory = "pg_log";
! char *      Log_filename_prefix = "postgresql-";
  
--- 64,66 ----
  char *      Log_directory = "pg_log";
! char *      Log_filename = "postgresql-%Y-%m-%d_%H%M%S.log";
  
***************
*** 122,123 ****
--- 122,124 ----
  	char         currentLogDir[MAXPGPATH];
+ 	char         currentLogFilename[MAXPGPATH];
  
***************
*** 219,222 ****
  	last_rotation_time = time(NULL);
! 	/* remember active logfile directory */
  	strncpy(currentLogDir, Log_directory, MAXPGPATH);
  
--- 220,224 ----
  	last_rotation_time = time(NULL);
! 	/* remember active logfile directory and filename */
  	strncpy(currentLogDir, Log_directory, MAXPGPATH);
+ 	strncpy(currentLogFilename, Log_filename, MAXPGPATH);
  
***************
*** 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?
  			 */
--- 242,246 ----
  			/*
! 			 * 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 ****
--- 251,257 ----
  			}
+ 			if (strncmp(Log_filename, currentLogFilename, MAXPGPATH) != 0)
+ 			{
+ 				strncpy(currentLogFilename, Log_filename, MAXPGPATH);
+ 				rotation_requested = true;
+ 			}
  		}
***************
*** 791,796 ****
  	char *filename;
! 	char stamptext[128];
! 
! 	pg_strftime(stamptext, sizeof(stamptext), "%Y-%m-%d_%H%M%S",
! 				pg_localtime(&timestamp));
  
--- 795,797 ----
  	char *filename;
! 	int len;
  
***************
*** 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);
  
--- 800,815 ----
  	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);
! 
! 	/* use strftime() if there are embedded % escape sequences */
! 	if ( strstr(Log_filename, "%") != NULL ) {
! 		struct pg_tm *now;
! 		now = pg_gmtime(&timestamp);
! 		pg_strftime((char*) (filename + len), MAXPGPATH - len, Log_filename, now);
! 	} 
! 	else 
! 		snprintf((char*) (filename + len), MAXPGPATH - len, "%s", Log_filename);
  
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	27 Aug 2004 17:37:18 -0000
***************
*** 1674,1676 ****
  	{
! 		{"log_filename_prefix", PGC_SIGHUP, LOGGING_WHERE,
  		 gettext_noop("Prefix for file names created in the log_directory."),
--- 1674,1676 ----
  	{
! 		{"log_filename", PGC_SIGHUP, LOGGING_WHERE,
  		 gettext_noop("Prefix for file names created in the log_directory."),
***************
*** 1678,1681 ****
  		},
! 		&Log_filename_prefix,
! 		"postgresql-", NULL, NULL
  	},
--- 1678,1681 ----
  		},
! 		&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	27 Aug 2004 17:37:18 -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,178 ----
                              # 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_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	27 Aug 2004 17:37:19 -0000
***************
*** 19,21 ****
  extern char *Log_directory;
! extern char *Log_filename_prefix;
  
--- 19,21 ----
  extern char *Log_directory;
! extern char *Log_filename;
  
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

               http://archives.postgresql.org

Reply via email to