feature: applied the intercept_error_log patch to the nginx core to provide 3rd-party modules a hook to intercept nginx error log data without touching files.

3rd-party modules can register a custom interception hook to ngx_http_core_main_conf_t.intercept_log_handler.

Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
This commit is contained in:
Yuansheng 2017-04-06 22:28:10 +08:00 committed by Yichun Zhang (agentzh)
parent e767256038
commit 7a7576319e
2 changed files with 73 additions and 3 deletions

View File

@ -0,0 +1,60 @@
diff --git a/src/core/ngx_cycle.h b/src/core/ngx_cycle.h
index c51b7ff..4c335b9 100644
--- a/src/core/ngx_cycle.h
+++ b/src/core/ngx_cycle.h
@@ -22,9 +22,14 @@
#define NGX_DEBUG_POINTS_ABORT 2
+#define HAVE_INTERCEPT_ERROR_LOG_PATCH
+
+
typedef struct ngx_shm_zone_s ngx_shm_zone_t;
typedef ngx_int_t (*ngx_shm_zone_init_pt) (ngx_shm_zone_t *zone, void *data);
+typedef ngx_int_t (*ngx_log_intercept_pt) (ngx_log_t *log, ngx_uint_t level,
+ u_char *buf, size_t len);
struct ngx_shm_zone_s {
void *data;
@@ -75,6 +80,10 @@ struct ngx_cycle_s {
ngx_str_t prefix;
ngx_str_t lock_file;
ngx_str_t hostname;
+
+ ngx_log_intercept_pt intercept_error_log_handler;
+ void *intercept_error_log_data;
+ unsigned entered_logger; /* :1 */
};
diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c
index 8e9408d..ed9b11b 100644
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -112,6 +112,8 @@ ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
ngx_uint_t wrote_stderr, debug_connection;
u_char errstr[NGX_MAX_ERROR_STR];
+ ngx_log_intercept_pt log_intercept = NULL;
+
last = errstr + NGX_MAX_ERROR_STR;
p = ngx_cpymem(errstr, ngx_cached_err_log_time.data,
@@ -153,6 +155,16 @@ ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
p = last - NGX_LINEFEED_SIZE;
}
+ if (ngx_cycle) {
+ log_intercept = ngx_cycle->intercept_error_log_handler;
+ }
+
+ if (log_intercept && !ngx_cycle->entered_logger) {
+ ngx_cycle->entered_logger = 1;
+ log_intercept(log, level, errstr, p - errstr);
+ ngx_cycle->entered_logger = 0;
+ }
+
ngx_linefeed(p);
wrote_stderr = 0;

View File

@ -55,9 +55,19 @@ else
fi
fi
echo "$info_txt applying the privileged-agent-process patch"
patch -p1 < $root/patches/nginx-$main_ver-privileged_agent_process.patch || exit 1
echo
answer=`$root/util/ver-ge "$main_ver" 1.11.2`
if [ "$answer" = "Y" ]; then
echo "$info_txt applying the privileged-agent-process patch"
patch -p1 < $root/patches/nginx-$main_ver-privileged_agent_process.patch || exit 1
echo
fi
answer=`$root/util/ver-ge "$main_ver" 1.11.2`
if [ "$answer" = "Y" ]; then
echo "$info_txt applying the intercept-error-log patch"
patch -p1 < $root/patches/nginx-$main_ver-intercept_error_log.patch || exit 1
echo
fi
echo "$info_txt applying the upstream-pipelining patch for nginx"
patch -p1 < $root/patches/nginx-$main_ver-upstream_pipelining.patch || exit 1