Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 19dbc34

Browse files
committed
Add a hook for processing messages due to be sent to the server log.
Use-cases for this include custom log filtering rules and custom log message transmission mechanisms (for instance, lossy log message collection, which has been discussed several times recently). As is our common practice for hooks, there's no regression test nor user-facing documentation for this, though the author did exhibit a sample module using the hook. Martin Pihlak, reviewed by Marti Raudsepp
1 parent bc97c38 commit 19dbc34

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/backend/utils/error/elog.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ sigjmp_buf *PG_exception_stack = NULL;
9595

9696
extern bool redirection_done;
9797

98+
/*
99+
* Hook for intercepting messages before they are sent to the server log.
100+
* Note that the hook will not get called for messages that are suppressed
101+
* by log_min_messages. Also note that logging hooks implemented in preload
102+
* libraries will miss any log messages that are generated before the
103+
* library is loaded.
104+
*/
105+
emit_log_hook_type emit_log_hook = NULL;
106+
98107
/* GUC parameters */
99108
int Log_error_verbosity = PGERROR_VERBOSE;
100109
char *Log_line_prefix = NULL; /* format for extra log line info */
@@ -1276,6 +1285,23 @@ EmitErrorReport(void)
12761285
CHECK_STACK_DEPTH();
12771286
oldcontext = MemoryContextSwitchTo(ErrorContext);
12781287

1288+
/*
1289+
* Call hook before sending message to log. The hook function is allowed
1290+
* to turn off edata->output_to_server, so we must recheck that afterward.
1291+
* Making any other change in the content of edata is not considered
1292+
* supported.
1293+
*
1294+
* Note: the reason why the hook can only turn off output_to_server, and
1295+
* not turn it on, is that it'd be unreliable: we will never get here at
1296+
* all if errstart() deems the message uninteresting. A hook that could
1297+
* make decisions in that direction would have to hook into errstart(),
1298+
* where it would have much less information available. emit_log_hook is
1299+
* intended for custom log filtering and custom log message transmission
1300+
* mechanisms.
1301+
*/
1302+
if (edata->output_to_server && emit_log_hook)
1303+
(*emit_log_hook) (edata);
1304+
12791305
/* Send to server log, if enabled */
12801306
if (edata->output_to_server)
12811307
send_message_to_server_log(edata);

src/include/utils/elog.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ extern void FlushErrorState(void);
334334
extern void ReThrowError(ErrorData *edata);
335335
extern void pg_re_throw(void) __attribute__((noreturn));
336336

337+
/* Hook for intercepting messages before they are sent to the server log */
338+
typedef void (*emit_log_hook_type) (ErrorData *edata);
339+
extern PGDLLIMPORT emit_log_hook_type emit_log_hook;
340+
337341

338342
/* GUC-configurable parameters */
339343

0 commit comments

Comments
 (0)