nginx-log
Logging
nginx provides two kinds of logs, ‘error’ log and debug log, you can use several APIs(macro) to log info to console or file, let’s explain how to use these APIs in detail.
support formats
1 | /* src/core/ngx_string.c |
usage
user can configure log level like this
1 | error /tmp/log debug; |
level can be:
- emerg
- alert
- crit
- error
- warn
- notice
- info
- debug -> debug all
specific debug component
- debug_core
- debug_alloc
- debug_mutex
- debug_event
- debug_http
- debug_mail
- debug_stream
error log
there are only two high level APIs user should use
- ngx_conf_log_error(level, ): log error during conf
- ngx_log_error(level,): log error during runtime
level can be
- #define NGX_LOG_EMERG 1
- #define NGX_LOG_ALERT 2
- #define NGX_LOG_CRIT 3
- #define NGX_LOG_ERR 4
- #define NGX_LOG_WARN 5
- #define NGX_LOG_NOTICE 6
- #define NGX_LOG_INFO 7
- #define NGX_LOG_DEBUG 8
1 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
1 | error_log /tmp/log info; |
Note: ngx_log_error is always complied into source code, logging or not depends on what level user configured
debug log
there are several high level APIs(macros) user can use
- ngx_log_debug0
- ngx_log_debug1
- ngx_log_debug2
- ngx_log_debug3
- ngx_log_debug4
- ngx_log_debug5
- ngx_log_debug6
- ngx_log_debug7
- ngx_log_debug8
level only limited to these below, any others like above will not be printed(like NGX_LOG_DEBUG, not printed if use ngx_log_debugx API)
- #define NGX_LOG_DEBUG_CORE 0x010
- #define NGX_LOG_DEBUG_ALLOC 0x020
- #define NGX_LOG_DEBUG_MUTEX 0x040
- #define NGX_LOG_DEBUG_EVENT 0x080
- #define NGX_LOG_DEBUG_HTTP 0x100
- #define NGX_LOG_DEBUG_MAIL 0x200
- #define NGX_LOG_DEBUG_STREAM 0x400
1 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log,...); |
Note:
- when call ngx_log_debugxxx, should never pass non-debug level to these APIs, even it’s permitted.
- ngx_log_debugxxx is complied into source code, only when
--with-debug
is configured - logging or not depends on what level user configured
1 | error_log /tmp/log debug; |
inside these logging APIS
high level APIS call low level APIs with some checks, but user should never call these low level APIs
1 | // log_level is user setting |
For debug log(NGX_LOG_DEBUG_HTTP), it’s logged only when
- –with-debug is configured by user
- user set log level(log->log_level) which has that bit
Note: when error_log set debug log->log_level = NGX_LOG_DEBUG_ALL(0x7ffffff0)