Skip to content

Commit 469e8f0

Browse files
committed
PGPRO-584: Fix logging options
Replace --log-level by --log-level-console and --log-level-file
1 parent e85e459 commit 469e8f0

File tree

8 files changed

+78
-60
lines changed

8 files changed

+78
-60
lines changed

src/configure.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
#include "pg_probackup.h"
1111

12-
static void opt_log_level(pgut_option *opt, const char *arg);
12+
static void opt_log_level_console(pgut_option *opt, const char *arg);
13+
static void opt_log_level_file(pgut_option *opt, const char *arg);
1314
static void opt_compress_alg(pgut_option *opt, const char *arg);
1415

1516
static pgBackupConfig *cur_config = NULL;
@@ -41,10 +42,10 @@ do_configure(bool show_only)
4142
if (replica_timeout != 300) /* 300 is default value */
4243
config->replica_timeout = replica_timeout;
4344

44-
if (log_to_file != LOGGER_NONE)
45-
config->log_to_file = LOG_TO_FILE;
46-
if (log_level != LOGGER_NONE)
47-
config->log_level = LOG_LEVEL;
45+
if (log_level_console != LOG_NONE)
46+
config->log_level_console = LOG_LEVEL_CONSOLE;
47+
if (log_level_file != LOG_NONE)
48+
config->log_level_file = LOG_LEVEL_FILE;
4849
if (log_filename)
4950
config->log_filename = log_filename;
5051
if (error_log_filename)
@@ -90,8 +91,8 @@ pgBackupConfigInit(pgBackupConfig *config)
9091
config->master_user = NULL;
9192
config->replica_timeout = INT_MIN; /* INT_MIN means "undefined" */
9293

93-
config->log_to_file = INT_MIN; /* INT_MIN means "undefined" */
94-
config->log_level = INT_MIN; /* INT_MIN means "undefined" */
94+
config->log_level_console = INT_MIN; /* INT_MIN means "undefined" */
95+
config->log_level_file = INT_MIN; /* INT_MIN means "undefined" */
9596
config->log_filename = NULL;
9697
config->error_log_filename = NULL;
9798
config->log_directory = NULL;
@@ -135,10 +136,10 @@ writeBackupCatalogConfig(FILE *out, pgBackupConfig *config)
135136
fprintf(out, "replica_timeout = %d\n", config->replica_timeout);
136137

137138
fprintf(out, "#Logging parameters:\n");
138-
if (config->log_to_file != INT_MIN)
139-
fprintf(out, "log = %d\n", config->log_to_file);
140-
if (config->log_level != INT_MIN)
141-
fprintf(out, "log-level = %s\n", deparse_log_level(config->log_level));
139+
if (config->log_level_console != INT_MIN)
140+
fprintf(out, "log-level-console = %s\n", deparse_log_level(config->log_level_console));
141+
if (config->log_level_file != INT_MIN)
142+
fprintf(out, "log-level-file = %s\n", deparse_log_level(config->log_level_file));
142143
if (config->log_filename)
143144
fprintf(out, "log-filename = %s\n", config->log_filename);
144145
if (config->error_log_filename)
@@ -198,8 +199,8 @@ readBackupCatalogConfigFile(void)
198199
{ 'f', 0, "compress-algorithm", opt_compress_alg, SOURCE_CMDLINE },
199200
{ 'u', 0, "compress-level", &(config->compress_level), SOURCE_CMDLINE },
200201
/* logging options */
201-
{ 'b', 0, "log", &(config->log_to_file), SOURCE_CMDLINE },
202-
{ 'f', 0, "log-level", opt_log_level, SOURCE_CMDLINE },
202+
{ 'f', 0, "log-level-console", opt_log_level_console, SOURCE_CMDLINE },
203+
{ 'f', 0, "log-level-file", opt_log_level_file, SOURCE_CMDLINE },
203204
{ 's', 0, "log-filename", &(config->log_filename), SOURCE_CMDLINE },
204205
{ 's', 0, "error-log-filename", &(config->error_log_filename), SOURCE_CMDLINE },
205206
{ 's', 0, "log-directory", &(config->log_directory), SOURCE_CMDLINE },
@@ -234,9 +235,15 @@ readBackupCatalogConfigFile(void)
234235
}
235236

236237
static void
237-
opt_log_level(pgut_option *opt, const char *arg)
238+
opt_log_level_console(pgut_option *opt, const char *arg)
238239
{
239-
cur_config->log_level = parse_log_level(arg);
240+
cur_config->log_level_console = parse_log_level(arg);
241+
}
242+
243+
static void
244+
opt_log_level_file(pgut_option *opt, const char *arg)
245+
{
246+
cur_config->log_level_file = parse_log_level(arg);
240247
}
241248

242249
static void

src/help.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ help_pg_probackup(void)
6666
printf(_("\n %s init -B backup-path [-l]\n"), PROGRAM_NAME);
6767

6868
printf(_("\n %s set-config -B backup-dir --instance=instance_name\n"), PROGRAM_NAME);
69-
printf(_(" [--log-level=log-level]\n"));
69+
printf(_(" [--log-level-console=log-level-console]\n"));
70+
printf(_(" [--log-level-file=log-level-file]\n"));
7071
printf(_(" [--log-filename=log-filename]\n"));
7172
printf(_(" [--error-log-filename=error-log-filename]\n"));
7273
printf(_(" [--log-directory=log-directory]\n"));
@@ -132,7 +133,6 @@ help_init(void)
132133
{
133134
printf(_("%s init -B backup-path -D pgdata-dir [-l]\n\n"), PROGRAM_NAME);
134135
printf(_(" -B, --backup-path=backup-path location of the backup storage area\n"));
135-
printf(_(" -l, --log store messages in a log file\n"));
136136
}
137137

138138
static void
@@ -153,7 +153,6 @@ help_backup(void)
153153
printf(_(" -b, --backup-mode=backup-mode backup mode=FULL|PAGE|PTRACK\n"));
154154
printf(_(" --instance=instance_name name of the instance\n"));
155155
printf(_(" -C, --smooth-checkpoint do smooth checkpoint before backup\n"));
156-
printf(_(" -l, --log store messages in a log file\n"));
157156
printf(_(" --stream stream the transaction log and include it in the backup\n"));
158157
printf(_(" --archive-timeout wait timeout for WAL segment archiving\n"));
159158
printf(_(" -S, --slot=SLOTNAME replication slot to use\n"));
@@ -195,7 +194,6 @@ help_restore(void)
195194
printf(_(" --instance=instance_name name of the instance\n"));
196195

197196
printf(_(" -D, --pgdata=pgdata-dir location of the database storage area\n"));
198-
printf(_(" -l, --log store messages in a log file\n"));
199197
printf(_(" -i, --backup-id=backup-id backup to restore\n"));
200198

201199
printf(_(" --progress show progress\n"));
@@ -219,7 +217,6 @@ help_validate(void)
219217
printf(_(" --instance=instance_name name of the instance\n"));
220218
printf(_(" -i, --backup-id=backup-id backup to validate\n"));
221219

222-
printf(_(" -l, --log store messages in a log file\n"));
223220
printf(_(" --progress show progress\n"));
224221
printf(_(" --time=time time stamp up to which recovery will proceed\n"));
225222
printf(_(" --xid=xid transaction ID up to which recovery will proceed\n"));
@@ -250,14 +247,14 @@ help_delete(void)
250247
printf(_(" -i, --backup-id=backup-id backup to delete\n"));
251248
printf(_(" --expired delete backups expired according to current\n"));
252249
printf(_(" retention policy\n"));
253-
printf(_(" -l, --log store messages in a log file\n"));
254250
}
255251

256252
static void
257253
help_set_config(void)
258254
{
259255
printf(_("%s set-config -B backup-dir --instance=instance_name\n"), PROGRAM_NAME);
260-
printf(_(" [--log-level=log-level]\n"));
256+
printf(_(" [--log-level-console=log-level-console]\n"));
257+
printf(_(" [--log-level-file=log-level-file]\n"));
261258
printf(_(" [--log-filename=log-filename]\n"));
262259
printf(_(" [--error-log-filename=error-log-filename]\n"));
263260
printf(_(" [--log-directory=log-directory]\n"));
@@ -276,7 +273,10 @@ help_set_config(void)
276273
printf(_(" --instance=instance_name name of the instance\n"));
277274

278275
printf(_("\n Logging options:\n"));
279-
printf(_(" --log-level=log-level controls which message levels are sent to the log\n"));
276+
printf(_(" --log-level-console=log-level-console\n"
277+
" controls which message levels are sent to the stderr\n"));
278+
printf(_(" --log-level-file=log-level-file\n"
279+
" controls which message levels are sent to a log file\n"));
280280
printf(_(" --log-filename=log-filename file names of the created log files which is treated as as strftime pattern\n"));
281281
printf(_(" --error-log-filename=error-log-filename\n"));
282282
printf(_(" file names of the created log files for error messages\n"));

src/pg_probackup.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ ProbackupSubcmd backup_subcmd;
9393
bool help = false;
9494

9595
static void opt_backup_mode(pgut_option *opt, const char *arg);
96-
static void opt_log_level(pgut_option *opt, const char *arg);
96+
static void opt_log_level_console(pgut_option *opt, const char *arg);
97+
static void opt_log_level_file(pgut_option *opt, const char *arg);
9798
static void opt_compress_alg(pgut_option *opt, const char *arg);
9899

99100
static pgut_option options[] =
@@ -141,13 +142,13 @@ static pgut_option options[] =
141142
{ 'u', 137, "compress-level", &compress_level, SOURCE_CMDLINE },
142143
{ 'b', 138, "compress", &compress_shortcut, SOURCE_CMDLINE },
143144
/* logging options */
144-
{ 'b', 'l', "log", &log_to_file, SOURCE_CMDLINE },
145-
{ 'f', 140, "log-level", opt_log_level, SOURCE_CMDLINE },
146-
{ 's', 141, "log-filename", &log_filename, SOURCE_CMDLINE },
147-
{ 's', 142, "error-log-filename", &error_log_filename, SOURCE_CMDLINE },
148-
{ 's', 143, "log-directory", &log_directory, SOURCE_CMDLINE },
149-
{ 'u', 144, "log-rotation-size", &log_rotation_size, SOURCE_CMDLINE },
150-
{ 'u', 145, "log-rotation-age", &log_rotation_age, SOURCE_CMDLINE },
145+
{ 'f', 140, "log-level-console", opt_log_level_console, SOURCE_CMDLINE },
146+
{ 'f', 141, "log-level-file", opt_log_level_file, SOURCE_CMDLINE },
147+
{ 's', 142, "log-filename", &log_filename, SOURCE_CMDLINE },
148+
{ 's', 143, "error-log-filename", &error_log_filename, SOURCE_CMDLINE },
149+
{ 's', 144, "log-directory", &log_directory, SOURCE_CMDLINE },
150+
{ 'u', 145, "log-rotation-size", &log_rotation_size, SOURCE_CMDLINE },
151+
{ 'u', 146, "log-rotation-age", &log_rotation_age, SOURCE_CMDLINE },
151152
/* connection options */
152153
{ 's', 'd', "pgdatabase", &pgut_dbname, SOURCE_CMDLINE },
153154
{ 's', 'h', "pghost", &host, SOURCE_CMDLINE },
@@ -422,9 +423,15 @@ opt_backup_mode(pgut_option *opt, const char *arg)
422423
}
423424

424425
static void
425-
opt_log_level(pgut_option *opt, const char *arg)
426+
opt_log_level_console(pgut_option *opt, const char *arg)
426427
{
427-
log_level = parse_log_level(arg);
428+
log_level_console = parse_log_level(arg);
429+
}
430+
431+
static void
432+
opt_log_level_file(pgut_option *opt, const char *arg)
433+
{
434+
log_level_file = parse_log_level(arg);
428435
}
429436

430437
CompressAlg

src/pg_probackup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ typedef struct pgBackupConfig
165165
const char *master_user;
166166
int replica_timeout;
167167

168-
int log_to_file;
169-
int log_level;
168+
int log_level_console;
169+
int log_level_file;
170170
char *log_filename;
171171
char *error_log_filename;
172172
char *log_directory;

src/restore.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ restore_backup(pgBackup *backup)
353353
parray_walk(files, pgFileFree);
354354
parray_free(files);
355355

356-
if (LOG_LEVEL <= LOG)
356+
if (LOG_LEVEL_CONSOLE <= LOG || LOG_LEVEL_FILE <= LOG)
357357
{
358358
char *backup_id;
359359

@@ -396,7 +396,7 @@ remove_deleted_files(pgBackup *backup)
396396
if (parray_bsearch(files, file, pgFileComparePathDesc) == NULL)
397397
{
398398
pgFileDelete(file);
399-
if (LOG_LEVEL <= LOG)
399+
if (LOG_LEVEL_CONSOLE <= LOG || LOG_LEVEL_FILE <= LOG)
400400
elog(LOG, "deleted %s", GetRelativePath(file->path, pgdata));
401401
}
402402
}
@@ -579,7 +579,7 @@ check_tablespace_mapping(pgBackup *backup)
579579
pgBackupGetPath(backup, this_backup_path, lengthof(this_backup_path), NULL);
580580
read_tablespace_map(links, this_backup_path);
581581

582-
if (LOG_LEVEL <= LOG)
582+
if (LOG_LEVEL_CONSOLE <= LOG || LOG_LEVEL_FILE <= LOG)
583583
{
584584
char *backup_id;
585585

src/utils/logger.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
/* Logger parameters */
2121

22-
int log_to_file = LOGGER_NONE;
23-
int log_level = LOGGER_NONE;
22+
int log_level_console = LOG_NONE;
23+
int log_level_file = LOG_NONE;
2424

2525
char *log_filename = NULL;
2626
char *error_log_filename = NULL;
@@ -72,7 +72,7 @@ void
7272
init_logger(const char *root_path)
7373
{
7474
/* Set log path */
75-
if (LOG_TO_FILE || error_log_filename)
75+
if (LOG_LEVEL_FILE != LOG_OFF || error_log_filename)
7676
{
7777
if (log_directory)
7878
strcpy(log_path, log_directory);
@@ -132,20 +132,18 @@ elog_internal(int elevel, const char *fmt, va_list args)
132132
time_t log_time = (time_t) time(NULL);
133133
char strfbuf[128];
134134

135-
write_to_file = log_path[0] != '\0' && !logging_to_file &&
136-
(LOG_TO_FILE || error_log_filename);
135+
write_to_file = !logging_to_file && elevel >= LOG_LEVEL_FILE;
136+
write_to_error_log = !logging_to_file &&
137+
elevel >= ERROR && error_log_filename;
138+
write_to_stderr = elevel >= LOG_LEVEL_CONSOLE;
137139

138140
/*
139141
* There is no need to lock if this is elog() from upper elog() and
140142
* logging is not initialized.
141143
*/
142-
if (write_to_file)
144+
if (write_to_file || write_to_error_log)
143145
pthread_mutex_lock(&log_file_mutex);
144146

145-
write_to_error_log =
146-
elevel >= ERROR && error_log_filename && write_to_file;
147-
write_to_stderr = elevel >= ERROR || !LOG_TO_FILE;
148-
149147
/* We need copy args only if we need write to error log file */
150148
if (write_to_error_log)
151149
va_copy(error_args, args);
@@ -156,7 +154,7 @@ elog_internal(int elevel, const char *fmt, va_list args)
156154
if (write_to_stderr && write_to_file)
157155
va_copy(std_args, args);
158156

159-
if (write_to_file)
157+
if (write_to_file || write_to_error_log)
160158
strftime(strfbuf, sizeof(strfbuf), "%Y-%m-%d %H:%M:%S %Z",
161159
localtime(&log_time));
162160

@@ -165,7 +163,7 @@ elog_internal(int elevel, const char *fmt, va_list args)
165163
* Do not write to file if this error was raised during write previous
166164
* message.
167165
*/
168-
if (LOG_TO_FILE && write_to_file)
166+
if (write_to_file)
169167
{
170168
logging_to_file = true;
171169

@@ -229,7 +227,7 @@ elog_internal(int elevel, const char *fmt, va_list args)
229227
va_end(std_args);
230228
}
231229

232-
if (write_to_file)
230+
if (write_to_file || write_to_error_log)
233231
pthread_mutex_unlock(&log_file_mutex);
234232

235233
/* Exit with code if it is an error */
@@ -249,7 +247,7 @@ elog(int elevel, const char *fmt, ...)
249247
* Do not log message if severity level is less than log_level.
250248
* It is the little optimisation to put it here not in elog_internal().
251249
*/
252-
if (elevel < LOG_LEVEL && elevel < ERROR)
250+
if (elevel < LOG_LEVEL_CONSOLE && elevel < LOG_LEVEL_FILE && elevel < ERROR)
253251
return;
254252

255253
va_start(args, fmt);
@@ -290,7 +288,7 @@ pg_log(eLogType type, const char *fmt, ...)
290288
* Do not log message if severity level is less than log_level.
291289
* It is the little optimisation to put it here not in elog_internal().
292290
*/
293-
if (elevel < LOG_LEVEL && elevel < ERROR)
291+
if (elevel < LOG_LEVEL_CONSOLE && elevel < LOG_LEVEL_FILE && elevel < ERROR)
294292
return;
295293

296294
va_start(args, fmt);
@@ -315,7 +313,9 @@ parse_log_level(const char *level)
315313
if (len == 0)
316314
elog(ERROR, "log-level is empty");
317315

318-
if (pg_strncasecmp("verbose", v, len) == 0)
316+
if (pg_strncasecmp("off", v, len) == 0)
317+
return LOG_OFF;
318+
else if (pg_strncasecmp("verbose", v, len) == 0)
319319
return VERBOSE;
320320
else if (pg_strncasecmp("log", v, len) == 0)
321321
return LOG;
@@ -345,6 +345,8 @@ deparse_log_level(int level)
345345
{
346346
switch (level)
347347
{
348+
case LOG_OFF:
349+
return "OFF";
348350
case VERBOSE:
349351
return "VERBOSE";
350352
case LOG:

src/utils/logger.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "postgres_fe.h"
1414

15-
#define LOGGER_NONE (-10)
15+
#define LOG_NONE (-10)
1616

1717
/* Log level */
1818
#define VERBOSE (-5)
@@ -23,11 +23,13 @@
2323
#define ERROR 1
2424
#define FATAL 2
2525
#define PANIC 3
26+
#define LOG_OFF 10
2627

2728
/* Logger parameters */
2829

2930
extern int log_to_file;
30-
extern int log_level;
31+
extern int log_level_console;
32+
extern int log_level_file;
3133

3234
extern char *log_filename;
3335
extern char *error_log_filename;
@@ -37,8 +39,8 @@ extern char log_path[MAXPGPATH];
3739
extern int log_rotation_size;
3840
extern int log_rotation_age;
3941

40-
#define LOG_TO_FILE ((log_to_file == LOGGER_NONE) ? false : (bool) log_to_file)
41-
#define LOG_LEVEL ((log_level == LOGGER_NONE) ? INFO : log_level)
42+
#define LOG_LEVEL_CONSOLE ((log_level_console == LOG_NONE) ? INFO : log_level_console)
43+
#define LOG_LEVEL_FILE ((log_level_file == LOG_NONE) ? LOG_OFF : log_level_file)
4244

4345
#undef elog
4446
extern void elog(int elevel, const char *fmt, ...) pg_attribute_printf(2, 3);

0 commit comments

Comments
 (0)