repmgr: don't display timestamp in log output

Differentiate between repmgr and repmgrd output
This commit is contained in:
Ian Barwick
2016-12-05 10:44:30 +09:00
parent 227f0190f7
commit 9788b2bd29
5 changed files with 45 additions and 13 deletions

View File

@@ -752,7 +752,7 @@ reload_config(t_configuration_options *orig_options)
{ {
log_notice(_("restarting logging with changed parameters\n")); log_notice(_("restarting logging with changed parameters\n"));
logger_shutdown(); logger_shutdown();
logger_init(orig_options, progname(), false); logger_init(orig_options, progname());
} }
if (config_changed == true) if (config_changed == true)

28
log.c
View File

@@ -48,6 +48,11 @@ int log_level = LOG_NOTICE;
int last_log_level = LOG_NOTICE; int last_log_level = LOG_NOTICE;
int verbose_logging = false; int verbose_logging = false;
int terse_logging = false; int terse_logging = false;
/*
* Global variable to be set by the main application to ensure any log output
* emitted before logger_init is called, is output in the correct format
*/
int logger_output_mode = OM_DAEMON;
extern void extern void
stderr_log_with_level(const char *level_name, int level, const char *fmt, ...) stderr_log_with_level(const char *level_name, int level, const char *fmt, ...)
@@ -62,9 +67,7 @@ stderr_log_with_level(const char *level_name, int level, const char *fmt, ...)
static void static void
_stderr_log_with_level(const char *level_name, int level, const char *fmt, va_list ap) _stderr_log_with_level(const char *level_name, int level, const char *fmt, va_list ap)
{ {
time_t t; char buf[100];
struct tm *tm;
char buff[100];
/* /*
* Store the requested level so that if there's a subsequent * Store the requested level so that if there's a subsequent
@@ -74,10 +77,21 @@ _stderr_log_with_level(const char *level_name, int level, const char *fmt, va_li
if (log_level >= level) if (log_level >= level)
{ {
/* Format log line prefix with timestamp if in daemon mode */
if (logger_output_mode == OM_DAEMON)
{
time_t t;
struct tm *tm;
time(&t); time(&t);
tm = localtime(&t); tm = localtime(&t);
strftime(buff, 100, "[%Y-%m-%d %H:%M:%S]", tm); strftime(buf, 100, "[%Y-%m-%d %H:%M:%S]", tm);
fprintf(stderr, "%s [%s] ", buff, level_name); fprintf(stderr, "%s [%s] ", buf, level_name);
}
else
{
fprintf(stderr, "%s: ", level_name);
}
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
@@ -142,7 +156,7 @@ log_verbose(int level, const char *fmt, ...)
bool bool
logger_init(t_configuration_options *opts, const char *ident, bool stderr_only) logger_init(t_configuration_options *opts, const char *ident)
{ {
char *level = opts->loglevel; char *level = opts->loglevel;
char *facility = opts->logfacility; char *facility = opts->logfacility;
@@ -180,7 +194,7 @@ logger_init(t_configuration_options *opts, const char *ident, bool stderr_only)
* STDERR only logging requested - finish here without setting up any further * STDERR only logging requested - finish here without setting up any further
* logging facility. * logging facility.
*/ */
if (stderr_only == true) if (logger_output_mode == OM_COMMAND_LINE)
return true; return true;
if (facility && *facility) if (facility && *facility)

6
log.h
View File

@@ -25,6 +25,9 @@
#define REPMGR_SYSLOG 1 #define REPMGR_SYSLOG 1
#define REPMGR_STDERR 2 #define REPMGR_STDERR 2
#define OM_COMMAND_LINE 1
#define OM_DAEMON 2
extern void extern void
stderr_log_with_level(const char *level_name, int level, const char *fmt,...) stderr_log_with_level(const char *level_name, int level, const char *fmt,...)
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4))); __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
@@ -116,7 +119,7 @@ int detect_log_level(const char *level);
/* Logger initialisation and shutdown */ /* Logger initialisation and shutdown */
bool logger_init(t_configuration_options * opts, const char *ident, bool stderr_only); bool logger_init(t_configuration_options * opts, const char *ident);
bool logger_shutdown(void); bool logger_shutdown(void);
@@ -132,5 +135,6 @@ extern int log_type;
extern int log_level; extern int log_level;
extern int verbose_logging; extern int verbose_logging;
extern int terse_logging; extern int terse_logging;
extern int logger_output_mode;
#endif /* _REPMGR_LOG_H_ */ #endif /* _REPMGR_LOG_H_ */

View File

@@ -265,6 +265,13 @@ main(int argc, char **argv)
exit(1); exit(1);
} }
/*
* Tell the logger we're a command-line program - this will
* ensure any output logged before the logger is initialized
* will be formatted correctly
*/
logger_output_mode = OM_COMMAND_LINE;
initialize_conninfo_params(&source_conninfo, true); initialize_conninfo_params(&source_conninfo, true);
/* /*
@@ -837,7 +844,7 @@ main(int argc, char **argv)
* which makes little sense for a command line program. * which makes little sense for a command line program.
*/ */
logger_init(&options, progname(), true); logger_init(&options, progname());
if (runtime_options.verbose) if (runtime_options.verbose)
logger_set_verbose(); logger_set_verbose();

View File

@@ -207,6 +207,13 @@ main(int argc, char **argv)
} }
} }
/*
* Tell the logger we're a daemon - this will ensure any output logged
* before the logger is initialized will be formatted correctly
*/
logger_output_mode = OM_DAEMON;
/* /*
* Parse the configuration file, if provided. If no configuration file * Parse the configuration file, if provided. If no configuration file
* was provided, or one was but was incomplete, parse_config() will * was provided, or one was but was incomplete, parse_config() will
@@ -246,7 +253,7 @@ main(int argc, char **argv)
strerror(errno)); strerror(errno));
} }
logger_init(&local_options, progname(), false); logger_init(&local_options, progname());
if (verbose) if (verbose)
logger_set_verbose(); logger_set_verbose();