[njs] Added additional log routines for different nginx log levels.

Dmitry Volyntsev xeioex at nginx.com
Thu Mar 29 13:38:14 UTC 2018


details:   http://hg.nginx.org/njs/rev/36ef67f13052
branches:  
changeset: 472:36ef67f13052
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Mar 29 16:38:02 2018 +0300
description:
Added additional log routines for different nginx log levels.

warn(), error().

diffstat:

 nginx/ngx_http_js_module.c   |  82 +++++++++++++++++++++++++++++++++++++------
 nginx/ngx_stream_js_module.c |  67 +++++++++++++++++++++++++++++++++--
 2 files changed, 131 insertions(+), 18 deletions(-)

diffs (235 lines):

diff -r ecf693afd698 -r 36ef67f13052 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Tue Mar 27 19:11:04 2018 +0300
+++ b/nginx/ngx_http_js_module.c	Thu Mar 29 16:38:02 2018 +0300
@@ -89,8 +89,16 @@ static njs_ret_t ngx_http_js_ext_send(nj
     nxt_uint_t nargs, njs_index_t unused);
 static njs_ret_t ngx_http_js_ext_finish(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused);
+
 static njs_ret_t ngx_http_js_ext_log(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused);
+static njs_ret_t ngx_http_js_ext_warn(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused);
+static njs_ret_t ngx_http_js_ext_error(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused);
+static njs_ret_t ngx_http_js_ext_log_core(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, ngx_uint_t level);
+
 static njs_ret_t ngx_http_js_ext_get_http_version(njs_vm_t *vm,
     njs_value_t *value, void *obj, uintptr_t data);
 static njs_ret_t ngx_http_js_ext_get_remote_address(njs_vm_t *vm,
@@ -287,18 +295,6 @@ static njs_external_t  ngx_http_js_ext_r
 
 static njs_external_t  ngx_http_js_ext_request[] = {
 
-    { nxt_string("log"),
-      NJS_EXTERN_METHOD,
-      NULL,
-      0,
-      NULL,
-      NULL,
-      NULL,
-      NULL,
-      NULL,
-      ngx_http_js_ext_log,
-      0 },
-
     { nxt_string("uri"),
       NJS_EXTERN_PROPERTY,
       NULL,
@@ -406,6 +402,42 @@ static njs_external_t  ngx_http_js_ext_r
       NULL,
       ngx_http_js_ext_subrequest,
       0 },
+
+    { nxt_string("log"),
+      NJS_EXTERN_METHOD,
+      NULL,
+      0,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      ngx_http_js_ext_log,
+      0 },
+
+    { nxt_string("warn"),
+      NJS_EXTERN_METHOD,
+      NULL,
+      0,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      ngx_http_js_ext_warn,
+      0 },
+
+    { nxt_string("error"),
+      NJS_EXTERN_METHOD,
+      NULL,
+      0,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      ngx_http_js_ext_error,
+      0 },
 };
 
 
@@ -1226,6 +1258,30 @@ static njs_ret_t
 ngx_http_js_ext_log(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t unused)
 {
+    return ngx_http_js_ext_log_core(vm, args, nargs, NGX_LOG_INFO);
+}
+
+
+static njs_ret_t
+ngx_http_js_ext_warn(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    return ngx_http_js_ext_log_core(vm, args, nargs, NGX_LOG_WARN);
+}
+
+
+static njs_ret_t
+ngx_http_js_ext_error(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    return ngx_http_js_ext_log_core(vm, args, nargs, NGX_LOG_ERR);
+}
+
+
+static njs_ret_t
+ngx_http_js_ext_log_core(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    ngx_uint_t level)
+{
     nxt_str_t            msg;
     ngx_connection_t    *c;
     ngx_log_handler_pt   handler;
@@ -1243,7 +1299,7 @@ ngx_http_js_ext_log(njs_vm_t *vm, njs_va
     handler = c->log->handler;
     c->log->handler = NULL;
 
-    ngx_log_error(NGX_LOG_INFO, c->log, 0, "js: %*s", msg.length, msg.start);
+    ngx_log_error(level, c->log, 0, "js: %*s", msg.length, msg.start);
 
     c->log->handler = handler;
 
diff -r ecf693afd698 -r 36ef67f13052 nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Tue Mar 27 19:11:04 2018 +0300
+++ b/nginx/ngx_stream_js_module.c	Thu Mar 29 16:38:02 2018 +0300
@@ -63,8 +63,16 @@ static njs_ret_t ngx_stream_js_ext_get_b
     void *obj, uintptr_t data);
 static njs_ret_t ngx_stream_js_ext_set_buffer(njs_vm_t *vm, void *obj,
     uintptr_t data, nxt_str_t *value);
- static njs_ret_t ngx_stream_js_ext_log(njs_vm_t *vm, njs_value_t *args,
+
+static njs_ret_t ngx_stream_js_ext_log(njs_vm_t *vm, njs_value_t *args,
+     nxt_uint_t nargs, njs_index_t unused);
+static njs_ret_t ngx_stream_js_ext_warn(njs_vm_t *vm, njs_value_t *args,
      nxt_uint_t nargs, njs_index_t unused);
+static njs_ret_t ngx_stream_js_ext_error(njs_vm_t *vm, njs_value_t *args,
+     nxt_uint_t nargs, njs_index_t unused);
+static njs_ret_t ngx_stream_js_ext_log_core(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, ngx_uint_t level);
+
 static njs_ret_t ngx_stream_js_ext_get_variable(njs_vm_t *vm,
     njs_value_t *value, void *obj, uintptr_t data);
 static njs_ret_t ngx_stream_js_ext_get_code(njs_vm_t *vm,
@@ -199,6 +207,19 @@ static njs_external_t  ngx_stream_js_ext
       NULL,
       0 },
 
+
+    { nxt_string("variables"),
+      NJS_EXTERN_OBJECT,
+      NULL,
+      0,
+      ngx_stream_js_ext_get_variable,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      0 },
+
     { nxt_string("log"),
       NJS_EXTERN_METHOD,
       NULL,
@@ -211,16 +232,28 @@ static njs_external_t  ngx_stream_js_ext
       ngx_stream_js_ext_log,
       0 },
 
-    { nxt_string("variables"),
-      NJS_EXTERN_OBJECT,
+    { nxt_string("warn"),
+      NJS_EXTERN_METHOD,
       NULL,
       0,
-      ngx_stream_js_ext_get_variable,
       NULL,
       NULL,
       NULL,
       NULL,
       NULL,
+      ngx_stream_js_ext_warn,
+      0 },
+
+    { nxt_string("error"),
+      NJS_EXTERN_METHOD,
+      NULL,
+      0,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      ngx_stream_js_ext_error,
       0 },
 
     { nxt_string("OK"),
@@ -825,6 +858,30 @@ static njs_ret_t
 ngx_stream_js_ext_log(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t unused)
 {
+    return ngx_stream_js_ext_log_core(vm, args, nargs, NGX_LOG_INFO);
+}
+
+
+static njs_ret_t
+ngx_stream_js_ext_warn(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    return ngx_stream_js_ext_log_core(vm, args, nargs, NGX_LOG_WARN);
+}
+
+
+static njs_ret_t
+ngx_stream_js_ext_error(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    return ngx_stream_js_ext_log_core(vm, args, nargs, NGX_LOG_ERR);
+}
+
+
+static njs_ret_t
+ngx_stream_js_ext_log_core(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    ngx_uint_t level)
+{
     nxt_str_t              msg;
     ngx_connection_t      *c;
     ngx_log_handler_pt     handler;
@@ -842,7 +899,7 @@ ngx_stream_js_ext_log(njs_vm_t *vm, njs_
     handler = c->log->handler;
     c->log->handler = NULL;
 
-    ngx_log_error(NGX_LOG_INFO, c->log, 0, "js: %*s", msg.length, msg.start);
+    ngx_log_error(level, c->log, 0, "js: %*s", msg.length, msg.start);
 
     c->log->handler = handler;
 


More information about the nginx-devel mailing list