diff -cpr --exclude=CVS --exclude=msvc head/doc/src/sgml/config.sgml work/doc/src/sgml/config.sgml
*** head/doc/src/sgml/config.sgml	2009-09-12 16:40:50.511013454 +0900
--- work/doc/src/sgml/config.sgml	2009-09-12 16:56:20.933013054 +0900
*************** FROM pg_stat_activity;
*** 3164,3169 ****
--- 3164,3183 ----
           character could be used too.
          </para>
         </tip>
+       </listitem>
+      </varlistentry>
+ 
+      <varlistentry id="guc-syslog-line-prefix" xreflabel="syslog_line_prefix">
+       <term><varname>syslog_line_prefix</varname> (<type>string</type>)</term>
+       <indexterm>
+        <primary><varname>syslog_line_prefix</> configuration parameter</primary>
+       </indexterm>
+       <listitem>
+        <para>
+          This is a <function>printf</>-style string that is output at the
+          beginning of each syslog and eventlog line.
+          Same as <varname>log_line_prefix</>.
+        </para>
  
         <tip>
          <para>
diff -cpr --exclude=CVS --exclude=msvc head/src/backend/utils/error/elog.c work/src/backend/utils/error/elog.c
*** head/src/backend/utils/error/elog.c	2009-07-04 04:14:25.000000000 +0900
--- work/src/backend/utils/error/elog.c	2009-09-12 17:06:20.354011870 +0900
*************** extern bool redirection_done;
*** 90,95 ****
--- 90,96 ----
  /* GUC parameters */
  int			Log_error_verbosity = PGERROR_VERBOSE;
  char	   *Log_line_prefix = NULL;		/* format for extra log line info */
+ char	   *syslog_line_prefix = NULL;	/* format for extra syslog line info */
  int			Log_destination = LOG_DESTINATION_STDERR;
  
  #ifdef HAVE_SYSLOG
*************** static bool openlog_done = false;
*** 108,118 ****
  static char *syslog_ident = NULL;
  static int	syslog_facility = LOG_LOCAL0;
  
! static void write_syslog(int level, const char *line);
  #endif
  
  #ifdef WIN32
! static void write_eventlog(int level, const char *line);
  #endif
  
  /* We provide a small stack of ErrorData records for re-entrant cases */
--- 109,119 ----
  static char *syslog_ident = NULL;
  static int	syslog_facility = LOG_LOCAL0;
  
! static void write_syslog(int elevel, const char *line);
  #endif
  
  #ifdef WIN32
! static void write_eventlog(int elevel, const char *line);
  #endif
  
  /* We provide a small stack of ErrorData records for re-entrant cases */
*************** static char formatted_log_time[FORMATTED
*** 144,150 ****
  	} while (0)
  
  
! static void log_line_prefix(StringInfo buf, ErrorData *edata);
  static void send_message_to_server_log(ErrorData *edata);
  static void send_message_to_frontend(ErrorData *edata);
  static char *expand_fmt_string(const char *fmt, ErrorData *edata);
--- 145,154 ----
  	} while (0)
  
  
! static void log_line_prefix(StringInfo buf, const ErrorData *edata,
! 				const char *prefix);
! static void format_message(StringInfo buf, const ErrorData *edata,
! 				const char *prefix);
  static void send_message_to_server_log(ErrorData *edata);
  static void send_message_to_frontend(ErrorData *edata);
  static char *expand_fmt_string(const char *fmt, ErrorData *edata);
*************** set_syslog_parameters(const char *ident,
*** 1464,1476 ****
   * Write a message line to syslog
   */
  static void
! write_syslog(int level, const char *line)
  {
  	static unsigned long seq = 0;
  
  	int			len;
  	const char *nlpos;
  
  	/* Open syslog connection if not done yet */
  	if (!openlog_done)
  	{
--- 1468,1511 ----
   * Write a message line to syslog
   */
  static void
! write_syslog(int elevel, const char *line)
  {
  	static unsigned long seq = 0;
  
+ 	int			level;
  	int			len;
  	const char *nlpos;
  
+ 	switch (elevel)
+ 	{
+ 		case DEBUG5:
+ 		case DEBUG4:
+ 		case DEBUG3:
+ 		case DEBUG2:
+ 		case DEBUG1:
+ 			level = LOG_DEBUG;
+ 			break;
+ 		case LOG:
+ 		case COMMERROR:
+ 		case INFO:
+ 			level = LOG_INFO;
+ 			break;
+ 		case NOTICE:
+ 		case WARNING:
+ 			level = LOG_NOTICE;
+ 			break;
+ 		case ERROR:
+ 			level = LOG_WARNING;
+ 			break;
+ 		case FATAL:
+ 			level = LOG_ERR;
+ 			break;
+ 		case PANIC:
+ 		default:
+ 			level = LOG_CRIT;
+ 			break;
+ 	}
+ 
  	/* Open syslog connection if not done yet */
  	if (!openlog_done)
  	{
*************** write_syslog(int level, const char *line
*** 1567,1573 ****
   * Write a message line to the windows event log
   */
  static void
! write_eventlog(int level, const char *line)
  {
  	int			eventlevel = EVENTLOG_ERROR_TYPE;
  	static HANDLE evtHandle = INVALID_HANDLE_VALUE;
--- 1602,1608 ----
   * Write a message line to the windows event log
   */
  static void
! write_eventlog(int elevel, const char *line)
  {
  	int			eventlevel = EVENTLOG_ERROR_TYPE;
  	static HANDLE evtHandle = INVALID_HANDLE_VALUE;
*************** write_eventlog(int level, const char *li
*** 1582,1588 ****
  		}
  	}
  
! 	switch (level)
  	{
  		case DEBUG5:
  		case DEBUG4:
--- 1617,1623 ----
  		}
  	}
  
! 	switch (elevel)
  	{
  		case DEBUG5:
  		case DEBUG4:
*************** setup_formatted_start_time(void)
*** 1677,1683 ****
   * Format tag info for log lines; append to the provided buffer.
   */
  static void
! log_line_prefix(StringInfo buf, ErrorData *edata)
  {
  	/* static counter for line numbers */
  	static long log_line_number = 0;
--- 1712,1718 ----
   * Format tag info for log lines; append to the provided buffer.
   */
  static void
! log_line_prefix(StringInfo buf, const ErrorData *edata, const char *prefix)
  {
  	/* static counter for line numbers */
  	static long log_line_number = 0;
*************** unpack_sql_state(int sql_state)
*** 2060,2149 ****
  	return buf;
  }
  
- 
- /*
-  * Write error report to server's log
-  */
  static void
! send_message_to_server_log(ErrorData *edata)
  {
- 	StringInfoData buf;
- 
- 	initStringInfo(&buf);
- 
  	formatted_log_time[0] = '\0';
  
! 	log_line_prefix(&buf, edata);
! 	appendStringInfo(&buf, "%s:  ", error_severity(edata->elevel));
  
  	if (Log_error_verbosity >= PGERROR_VERBOSE)
! 		appendStringInfo(&buf, "%s: ", unpack_sql_state(edata->sqlerrcode));
  
  	if (edata->message)
! 		append_with_tabs(&buf, edata->message);
  	else
! 		append_with_tabs(&buf, _("missing error text"));
  
  	if (edata->cursorpos > 0)
! 		appendStringInfo(&buf, _(" at character %d"),
  						 edata->cursorpos);
  	else if (edata->internalpos > 0)
! 		appendStringInfo(&buf, _(" at character %d"),
  						 edata->internalpos);
  
! 	appendStringInfoChar(&buf, '\n');
  
  	if (Log_error_verbosity >= PGERROR_DEFAULT)
  	{
  		if (edata->detail_log)
  		{
! 			log_line_prefix(&buf, edata);
! 			appendStringInfoString(&buf, _("DETAIL:  "));
! 			append_with_tabs(&buf, edata->detail_log);
! 			appendStringInfoChar(&buf, '\n');
  		}
  		else if (edata->detail)
  		{
! 			log_line_prefix(&buf, edata);
! 			appendStringInfoString(&buf, _("DETAIL:  "));
! 			append_with_tabs(&buf, edata->detail);
! 			appendStringInfoChar(&buf, '\n');
  		}
  		if (edata->hint)
  		{
! 			log_line_prefix(&buf, edata);
! 			appendStringInfoString(&buf, _("HINT:  "));
! 			append_with_tabs(&buf, edata->hint);
! 			appendStringInfoChar(&buf, '\n');
  		}
  		if (edata->internalquery)
  		{
! 			log_line_prefix(&buf, edata);
! 			appendStringInfoString(&buf, _("QUERY:  "));
! 			append_with_tabs(&buf, edata->internalquery);
! 			appendStringInfoChar(&buf, '\n');
  		}
  		if (edata->context)
  		{
! 			log_line_prefix(&buf, edata);
! 			appendStringInfoString(&buf, _("CONTEXT:  "));
! 			append_with_tabs(&buf, edata->context);
! 			appendStringInfoChar(&buf, '\n');
  		}
  		if (Log_error_verbosity >= PGERROR_VERBOSE)
  		{
  			/* assume no newlines in funcname or filename... */
  			if (edata->funcname && edata->filename)
  			{
! 				log_line_prefix(&buf, edata);
! 				appendStringInfo(&buf, _("LOCATION:  %s, %s:%d\n"),
  								 edata->funcname, edata->filename,
  								 edata->lineno);
  			}
  			else if (edata->filename)
  			{
! 				log_line_prefix(&buf, edata);
! 				appendStringInfo(&buf, _("LOCATION:  %s:%d\n"),
  								 edata->filename, edata->lineno);
  			}
  		}
--- 2095,2176 ----
  	return buf;
  }
  
  static void
! format_message(StringInfo buf, const ErrorData *edata, const char *prefix)
  {
  	formatted_log_time[0] = '\0';
  
! 	log_line_prefix(buf, edata, prefix);
! 	appendStringInfo(buf, "%s:  ", error_severity(edata->elevel));
  
  	if (Log_error_verbosity >= PGERROR_VERBOSE)
! 		appendStringInfo(buf, "%s: ", unpack_sql_state(edata->sqlerrcode));
  
  	if (edata->message)
! 		append_with_tabs(buf, edata->message);
  	else
! 		append_with_tabs(buf, _("missing error text"));
  
  	if (edata->cursorpos > 0)
! 		appendStringInfo(buf, _(" at character %d"),
  						 edata->cursorpos);
  	else if (edata->internalpos > 0)
! 		appendStringInfo(buf, _(" at character %d"),
  						 edata->internalpos);
  
! 	appendStringInfoChar(buf, '\n');
  
  	if (Log_error_verbosity >= PGERROR_DEFAULT)
  	{
  		if (edata->detail_log)
  		{
! 			log_line_prefix(buf, edata, prefix);
! 			appendStringInfoString(buf, _("DETAIL:  "));
! 			append_with_tabs(buf, edata->detail_log);
! 			appendStringInfoChar(buf, '\n');
  		}
  		else if (edata->detail)
  		{
! 			log_line_prefix(buf, edata, prefix);
! 			appendStringInfoString(buf, _("DETAIL:  "));
! 			append_with_tabs(buf, edata->detail);
! 			appendStringInfoChar(buf, '\n');
  		}
  		if (edata->hint)
  		{
! 			log_line_prefix(buf, edata, prefix);
! 			appendStringInfoString(buf, _("HINT:  "));
! 			append_with_tabs(buf, edata->hint);
! 			appendStringInfoChar(buf, '\n');
  		}
  		if (edata->internalquery)
  		{
! 			log_line_prefix(buf, edata, prefix);
! 			appendStringInfoString(buf, _("QUERY:  "));
! 			append_with_tabs(buf, edata->internalquery);
! 			appendStringInfoChar(buf, '\n');
  		}
  		if (edata->context)
  		{
! 			log_line_prefix(buf, edata, prefix);
! 			appendStringInfoString(buf, _("CONTEXT:  "));
! 			append_with_tabs(buf, edata->context);
! 			appendStringInfoChar(buf, '\n');
  		}
  		if (Log_error_verbosity >= PGERROR_VERBOSE)
  		{
  			/* assume no newlines in funcname or filename... */
  			if (edata->funcname && edata->filename)
  			{
! 				log_line_prefix(buf, edata, prefix);
! 				appendStringInfo(buf, _("LOCATION:  %s, %s:%d\n"),
  								 edata->funcname, edata->filename,
  								 edata->lineno);
  			}
  			else if (edata->filename)
  			{
! 				log_line_prefix(buf, edata, prefix);
! 				appendStringInfo(buf, _("LOCATION:  %s:%d\n"),
  								 edata->filename, edata->lineno);
  			}
  		}
*************** send_message_to_server_log(ErrorData *ed
*** 2156,2218 ****
  		debug_query_string != NULL &&
  		!edata->hide_stmt)
  	{
! 		log_line_prefix(&buf, edata);
! 		appendStringInfoString(&buf, _("STATEMENT:  "));
! 		append_with_tabs(&buf, debug_query_string);
! 		appendStringInfoChar(&buf, '\n');
  	}
  
! #ifdef HAVE_SYSLOG
! 	/* Write to syslog, if enabled */
! 	if (Log_destination & LOG_DESTINATION_SYSLOG)
! 	{
! 		int			syslog_level;
  
! 		switch (edata->elevel)
! 		{
! 			case DEBUG5:
! 			case DEBUG4:
! 			case DEBUG3:
! 			case DEBUG2:
! 			case DEBUG1:
! 				syslog_level = LOG_DEBUG;
! 				break;
! 			case LOG:
! 			case COMMERROR:
! 			case INFO:
! 				syslog_level = LOG_INFO;
! 				break;
! 			case NOTICE:
! 			case WARNING:
! 				syslog_level = LOG_NOTICE;
! 				break;
! 			case ERROR:
! 				syslog_level = LOG_WARNING;
! 				break;
! 			case FATAL:
! 				syslog_level = LOG_ERR;
! 				break;
! 			case PANIC:
! 			default:
! 				syslog_level = LOG_CRIT;
! 				break;
! 		}
  
! 		write_syslog(syslog_level, buf.data);
! 	}
  #endif   /* HAVE_SYSLOG */
  
  #ifdef WIN32
! 	/* Write to eventlog, if enabled */
! 	if (Log_destination & LOG_DESTINATION_EVENTLOG)
  	{
! 		write_eventlog(edata->elevel, buf.data);
  	}
- #endif   /* WIN32 */
  
  	/* Write to stderr, if enabled */
  	if ((Log_destination & LOG_DESTINATION_STDERR) || whereToSendOutput == DestDebug)
  	{
  		/*
  		 * Use the chunking protocol if we know the syslogger should be
  		 * catching stderr output, and we are not ourselves the syslogger.
--- 2183,2237 ----
  		debug_query_string != NULL &&
  		!edata->hide_stmt)
  	{
! 		log_line_prefix(buf, edata, prefix);
! 		appendStringInfoString(buf, _("STATEMENT:  "));
! 		append_with_tabs(buf, debug_query_string);
! 		appendStringInfoChar(buf, '\n');
  	}
+ }
  
! /*
!  * Write error report to server's log
!  */
! static void
! send_message_to_server_log(ErrorData *edata)
! {
! 	StringInfoData buf;
  
! 	initStringInfo(&buf);
  
! #if defined(HAVE_SYSLOG) || defined(WIN32)
! 	if (Log_destination & (LOG_DESTINATION_SYSLOG | LOG_DESTINATION_EVENTLOG))
! 	{
! 		format_message(&buf, edata, syslog_line_prefix);
! 
! #ifdef HAVE_SYSLOG
! 		/* Write to syslog, if enabled */
! 		if (Log_destination & LOG_DESTINATION_SYSLOG)
! 			write_syslog(edata->elevel, buf.data);
  #endif   /* HAVE_SYSLOG */
  
  #ifdef WIN32
! 		/* Write to eventlog, if enabled */
! 		if (Log_destination & LOG_DESTINATION_EVENTLOG)
! 			write_eventlog(edata->elevel, buf.data);
! #endif   /* WIN32 */
! 	}
! #endif
! 
! 	/* reset message buffer if prefixes are different */
! 	if (Log_line_prefix != syslog_line_prefix &&
! 		(Log_line_prefix == NULL || syslog_line_prefix == NULL ||
! 		 strcmp(Log_line_prefix, syslog_line_prefix) != 0))
  	{
! 		resetStringInfo(&buf);
  	}
  
  	/* Write to stderr, if enabled */
  	if ((Log_destination & LOG_DESTINATION_STDERR) || whereToSendOutput == DestDebug)
  	{
+ 		format_message(&buf, edata, Log_line_prefix);
+ 
  		/*
  		 * Use the chunking protocol if we know the syslogger should be
  		 * catching stderr output, and we are not ourselves the syslogger.
*************** send_message_to_server_log(ErrorData *ed
*** 2238,2244 ****
--- 2257,2267 ----
  
  	/* If in the syslogger process, try to write messages direct to file */
  	if (am_syslogger)
+ 	{
+ 		if (buf.len == 0)
+ 			format_message(&buf, edata, Log_line_prefix);
  		write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_STDERR);
+ 	}
  
  	/* Write to CSV log if enabled */
  	if (Log_destination & LOG_DESTINATION_CSVLOG)
*************** send_message_to_server_log(ErrorData *ed
*** 2260,2265 ****
--- 2283,2290 ----
  			if (!(Log_destination & LOG_DESTINATION_STDERR) &&
  				whereToSendOutput != DestDebug)
  			{
+ 				if (buf.len == 0)
+ 					format_message(&buf, edata, Log_line_prefix);
  				/* write message to stderr unless we just sent it above */
  				write(fileno(stderr), buf.data, buf.len);
  			}
diff -cpr --exclude=CVS --exclude=msvc head/src/backend/utils/misc/guc.c work/src/backend/utils/misc/guc.c
*** head/src/backend/utils/misc/guc.c	2009-09-12 16:41:11.589011652 +0900
--- work/src/backend/utils/misc/guc.c	2009-09-12 16:56:20.942013087 +0900
*************** static struct config_string ConfigureNam
*** 2126,2131 ****
--- 2126,2140 ----
  	},
  
  	{
+ 		{"syslog_line_prefix", PGC_SIGHUP, LOGGING_WHAT,
+ 			gettext_noop("Controls information prefixed to each syslog and eventlog line."),
+ 			gettext_noop("If blank, no prefix is used.")
+ 		},
+ 		&Log_line_prefix,
+ 		"", NULL, NULL
+ 	},
+ 
+ 	{
  		{"log_line_prefix", PGC_SIGHUP, LOGGING_WHAT,
  			gettext_noop("Controls information prefixed to each log line."),
  			gettext_noop("If blank, no prefix is used.")
diff -cpr --exclude=CVS --exclude=msvc head/src/backend/utils/misc/postgresql.conf.sample work/src/backend/utils/misc/postgresql.conf.sample
*** head/src/backend/utils/misc/postgresql.conf.sample	2009-09-12 16:41:11.783013462 +0900
--- work/src/backend/utils/misc/postgresql.conf.sample	2009-09-12 16:56:20.944017812 +0900
***************
*** 334,339 ****
--- 334,340 ----
  #log_duration = off
  #log_hostname = off
  #log_line_prefix = ''			# special values:
+ #syslog_line_prefix = ''
  					#   %u = user name
  					#   %d = database name
  					#   %r = remote host and port
diff -cpr --exclude=CVS --exclude=msvc head/src/include/utils/elog.h work/src/include/utils/elog.h
*** head/src/include/utils/elog.h	2009-06-11 23:49:13.000000000 +0900
--- work/src/include/utils/elog.h	2009-09-12 16:56:20.945014308 +0900
*************** typedef enum
*** 330,335 ****
--- 330,336 ----
  
  extern int	Log_error_verbosity;
  extern char *Log_line_prefix;
+ extern char *syslog_line_prefix;
  extern int	Log_destination;
  
  /* Log destination bitmap */
