Skip to content

Commit

Permalink
[agroal#44] Support for custom log line prefix.
Browse files Browse the repository at this point in the history
This introduces the configuration parameter `log_line_prefix`
that accepts a strftime(3) valid string that is parsed
and stored at the very first log entry in the configuration
structure (`config->log_line_prefix`).
Logging to a file and to the console is done using such
prefix via strftime.

Documentation updated.

Close agroal#44
  • Loading branch information
fluca1978 committed Feb 25, 2022
1 parent eae4fec commit 246f3e3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
3 changes: 2 additions & 1 deletion doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ See a [sample](./etc/pgagroal/pgagroal.conf) configuration for running `pgagroal
| management | 0 | Int | No | The remote management port (disable = 0) |
| log_type | console | String | No | The logging type (console, file, syslog) |
| log_level | info | String | No | The logging level (fatal, error, warn, info, debug1, ..., debug5) |
| log_path | pgagroal.log | String | No | The log file location |
| log_line_prefix | %Y-%m-%d %H:%M:%S | String | No | A prefix to place on every log entry, accepts escape sequences that strftime(3) can parse. No spaces between the string parts are allowed. |
| log_path | pgagroal.log | String | No | The log file location. Can include escape sequences that strftime(3) accepts, e.g., pgagroal-%Y-%m-%d-%H-%M-%S.log |
| log_rotation_age | -1 | String | No | The age that will trigger a log file rotation. If expressed as a positive number, is managed as seconds. Supports suffixes: 'S' (seconds, the default), 'M' (minutes), 'H' (hours), 'D' (days), 'W' (weeks) |
| log_rotation_size | -1 | String | No | The size of the log file that will trigger a log rotation. Supports suffixes: 'B' (bytes), the default if omitted, 'K' (kilobytes), 'M' (megabytes), 'G' (gigabytes). |
| log_mode | append | String | No | Append to or create the log file (append, create). The create option truncates an already existing file. |
Expand Down
1 change: 1 addition & 0 deletions src/include/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern "C" {

#define PGAGROAL_LOGGING_ROTATION_DISABLED -1

#define PGAGROAL_LOGGING_DEFAULT_LOG_LINE_PREFIX "%Y-%m-%d %H:%M:%S"

#define pgagroal_log_trace(...) pgagroal_log_line(PGAGROAL_LOGGING_LEVEL_DEBUG5, __FILE__, __LINE__, __VA_ARGS__)
#define pgagroal_log_debug(...) pgagroal_log_line(PGAGROAL_LOGGING_LEVEL_DEBUG1, __FILE__, __LINE__, __VA_ARGS__)
Expand Down
2 changes: 1 addition & 1 deletion src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ struct configuration
atomic_schar log_lock; /**< The logging lock */
int log_rotation_size; /**< bytes to force log rotation */
int log_rotation_age; /**< minutes for log rotation */
bool log_truncate; /**< Truncate an existing log on rotation */
char log_line_prefix[MISC_LENGTH]; /**< The logging prefix */

bool authquery; /**< Is authentication query enabled */

Expand Down
16 changes: 16 additions & 0 deletions src/libpgagroal/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,22 @@ pgagroal_read_configuration(void* shm, char* filename)
unknown = true;
}

}
else if (!strcmp(key, "log_line_prefix"))
{
if (!strcmp(section, "pgagroal"))
{
max = strlen(value);
if (max > MISC_LENGTH - 1)
max = MISC_LENGTH - 1;

memcpy(config->log_line_prefix, value, max);
}
else
{
unknown = true;
}

}
else if (!strcmp(key, "log_connections"))
{
Expand Down
25 changes: 10 additions & 15 deletions src/libpgagroal/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ log_rotation_enabled(void)
// logging is not to a file
if (config->log_type != PGAGROAL_LOGGING_TYPE_FILE)
{
log_rotation_disable();
return false;
log_rotation_disable();
return false;
}

// log rotation is enabled if either log_rotation_age or
Expand Down Expand Up @@ -203,7 +203,7 @@ pgagroal_init_logging(void)
{
printf("Failed to open log file %s due to %s\n", strlen(config->log_path) > 0 ? config->log_path : "pgagroal.log", strerror(errno));
errno = 0;
log_rotation_disable();
log_rotation_disable();
return 1;
}
}
Expand Down Expand Up @@ -299,15 +299,6 @@ log_file_open(void)
return 1;

log_rotation_set_next_rotation_age();
/* printf("\n\nLog file %s initialized %d, rotation %sable (every %d bytes or %d seconds or so, next estimated at %ld epoch, now is %d)\n\n",
current_log_path,
config->log_level,
log_rotation_enabled() ? "en" : "dis",
config->log_rotation_size,
config->log_rotation_age,
(long) next_log_rotation_age,
(long) htime);
*/
return 0;
}

Expand Down Expand Up @@ -400,11 +391,16 @@ pgagroal_log_line(int level, char *file, int line, char *fmt, ...)
filename = file;
}

if (config->log_line_prefix == NULL || strlen(config->log_line_prefix) == 0)
{
memcpy(config->log_line_prefix,PGAGROAL_LOGGING_DEFAULT_LOG_LINE_PREFIX,strlen(PGAGROAL_LOGGING_DEFAULT_LOG_LINE_PREFIX));
}

va_start(vl, fmt);

if (config->log_type == PGAGROAL_LOGGING_TYPE_CONSOLE)
{
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm)] = '\0';
buf[strftime(buf, sizeof(buf), config->log_line_prefix, tm)] = '\0';
fprintf(stdout, "%s %s%-5s\x1b[0m \x1b[90m%s:%d\x1b[0m ",
buf, colors[level - 1], levels[level - 1],
filename, line);
Expand All @@ -414,7 +410,7 @@ pgagroal_log_line(int level, char *file, int line, char *fmt, ...)
}
else if (config->log_type == PGAGROAL_LOGGING_TYPE_FILE)
{
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm)] = '\0';
buf[strftime(buf, sizeof(buf), config->log_line_prefix, tm)] = '\0';
fprintf(log_file, "%s %-5s %s:%d ",
buf, levels[level - 1], filename, line);
vfprintf(log_file, fmt, vl);
Expand All @@ -423,7 +419,6 @@ pgagroal_log_line(int level, char *file, int line, char *fmt, ...)

if (log_rotation_required())
{
printf("\nLOG ROTATION\n\n");
log_file_rotate();
}
}
Expand Down

0 comments on commit 246f3e3

Please sign in to comment.