Skip to content

Commit eeb1816

Browse files
committed
optimize: now ngx.log is much faster when the log level argument is lower than the actual error_log level specified in nginx.conf. thanks Matthieu Tourne for providing the patch.
1 parent 02b1db9 commit eeb1816

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/ngx_http_lua_log.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ static int ngx_http_lua_print(lua_State *L);
1010
static int ngx_http_lua_ngx_log(lua_State *L);
1111

1212

13-
static int log_wrapper(ngx_http_request_t *r, const char *ident, int level,
14-
lua_State *L);
13+
static int log_wrapper(ngx_http_request_t *r, const char *ident,
14+
ngx_uint_t level, lua_State *L);
15+
1516
static void ngx_http_lua_inject_log_consts(lua_State *L);
1617

1718

@@ -37,7 +38,7 @@ ngx_http_lua_ngx_log(lua_State *L)
3738
/* remove log-level param from stack */
3839
lua_remove(L, 1);
3940

40-
return log_wrapper(r, "", level, L);
41+
return log_wrapper(r, "", (ngx_uint_t) level, L);
4142
}
4243

4344
dd("(lua-log) can't output log due to invalid logging context!");
@@ -75,7 +76,8 @@ ngx_http_lua_print(lua_State *L)
7576

7677

7778
static int
78-
log_wrapper(ngx_http_request_t *r, const char *ident, int level, lua_State *L)
79+
log_wrapper(ngx_http_request_t *r, const char *ident, ngx_uint_t level,
80+
lua_State *L)
7981
{
8082
u_char *buf;
8183
u_char *p;
@@ -85,6 +87,10 @@ log_wrapper(ngx_http_request_t *r, const char *ident, int level, lua_State *L)
8587
int type;
8688
const char *msg;
8789

90+
if (level > r->connection->log->log_level) {
91+
return 0;
92+
}
93+
8894
nargs = lua_gettop(L);
8995
if (nargs == 0) {
9096
buf = NULL;
@@ -185,7 +191,7 @@ log_wrapper(ngx_http_request_t *r, const char *ident, int level, lua_State *L)
185191
*p++ = '\0';
186192

187193
done:
188-
ngx_log_error((ngx_uint_t) level, r->connection->log, 0,
194+
ngx_log_error(level, r->connection->log, 0,
189195
"%s%s", ident, (buf == NULL) ? (u_char *) "(null)" : buf);
190196
return 0;
191197
}

0 commit comments

Comments
 (0)