Nginx日志详解

1. 相关配置

Nginx日志系统由两条指令开启error_log、access_log

1
2
3
4
5
6
7
8
error_log  logs/error.log debug;

http {
...
access_log logs/access.log main;
...
}

具体配置可以参考error_logaccess_log

2. 源码解析

首先来看一下关于日志的几个宏定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
filename:log.h
#if (NGX_HAVE_C99_VARIADIC_MACROS)

#define NGX_HAVE_VARIADIC_MACROS 1

#define ngx_log_error(level, log, ...) \
if ((log)->log_level >= level) ngx_log_error_core(level, log, __VA_ARGS__)

void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, ...);

#define ngx_log_debug(level, log, ...) \
if ((log)->log_level & level) \
ngx_log_error_core(NGX_LOG_DEBUG, log, __VA_ARGS__)

/*********************************/

#elif (NGX_HAVE_GCC_VARIADIC_MACROS)

#define NGX_HAVE_VARIADIC_MACROS 1

#define ngx_log_error(level, log, args...) \
if ((log)->log_level >= level) ngx_log_error_core(level, log, args)

void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, ...);

#define ngx_log_debug(level, log, args...) \
if ((log)->log_level & level) \
ngx_log_error_core(NGX_LOG_DEBUG, log, args)

/*********************************/

#else /* no variadic macros */

#define NGX_HAVE_VARIADIC_MACROS 0

void ngx_cdecl ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, ...);
void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, va_list args);
void ngx_cdecl ngx_log_debug_core(ngx_log_t *log, ngx_err_t err,
const char *fmt, ...);


#endif /* variadic macros */

从上面的定义可以看出,实现日志功能的核心在于ngx_log_error_core函数,接下来我们就来看看这个函数的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#if (NGX_HAVE_VARIADIC_MACROS)

void
ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, ...)

#else

void
ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, va_list args)

#endif
{
#if (NGX_HAVE_VARIADIC_MACROS)
va_list args;
#endif
u_char *p, *last, *msg;
ssize_t n;
ngx_uint_t wrote_stderr, debug_connection;
u_char errstr[NGX_MAX_ERROR_STR];

last = errstr + NGX_MAX_ERROR_STR;

p = ngx_cpymem(errstr, ngx_cached_err_log_time.data,
ngx_cached_err_log_time.len);

p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]);

/* pid#tid */
p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ",
ngx_log_pid, ngx_log_tid);

if (log->connection) {
p = ngx_slprintf(p, last, "*%uA ", log->connection);
}

msg = p;

#if (NGX_HAVE_VARIADIC_MACROS)

va_start(args, fmt);
p = ngx_vslprintf(p, last, fmt, args);
va_end(args);

#else

p = ngx_vslprintf(p, last, fmt, args);

#endif

if (err) {
p = ngx_log_errno(p, last, err);
}

if (level != NGX_LOG_DEBUG && log->handler) {
p = log->handler(log, p, last - p);
}

if (p > last - NGX_LINEFEED_SIZE) {
p = last - NGX_LINEFEED_SIZE;
}

ngx_linefeed(p);

wrote_stderr = 0;
debug_connection = (log->log_level & NGX_LOG_DEBUG_CONNECTION) != 0;

while (log) {

if (log->log_level < level && !debug_connection) {
break;
}

if (log->writer) {
log->writer(log, level, errstr, p - errstr);
goto next;
}

if (ngx_time() == log->disk_full_time) {

/*
* on FreeBSD writing to a full filesystem with enabled softupdates
* may block process for much longer time than writing to non-full
* filesystem, so we skip writing to a log for one second
*/

goto next;
}

n = ngx_write_fd(log->file->fd, errstr, p - errstr);

if (n == -1 && ngx_errno == NGX_ENOSPC) {
log->disk_full_time = ngx_time();
}

if (log->file->fd == ngx_stderr) {
wrote_stderr = 1;
}

next:

log = log->next;
}

if (!ngx_use_stderr
|| level > NGX_LOG_WARN
|| wrote_stderr)
{
return;
}

msg -= (7 + err_levels[level].len + 3);

(void) ngx_sprintf(msg, "nginx: [%V] ", &err_levels[level]);

(void) ngx_write_console(ngx_stderr, msg, p - msg);
}
  1. 从代码中可以看出,nginx的作者对于变参处理还是很细心的,把各种情况都考虑到了,在处理好变参定义之后,就开始初始化字符串格式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

p = ngx_cpymem(errstr, ngx_cached_err_log_time.data,
ngx_cached_err_log_time.len);

p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]);

/* pid#tid */
p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ",
ngx_log_pid, ngx_log_tid);

if (log->connection) {
p = ngx_slprintf(p, last, "*%uA ", log->connection);
}

  1. 处理变参参数,将变参的值复制到日志串中:
1
2
3
4
5
6
7
8
9
10
11
#if (NGX_HAVE_VARIADIC_MACROS)

va_start(args, fmt);
p = ngx_vslprintf(p, last, fmt, args);
va_end(args);

#else

p = ngx_vslprintf(p, last, fmt, args);

#endif
  1. 处理错误号跟换行:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (err) {
p = ngx_log_errno(p, last, err);
}

if (level != NGX_LOG_DEBUG && log->handler) {
p = log->handler(log, p, last - p);
}

if (p > last - NGX_LINEFEED_SIZE) {
p = last - NGX_LINEFEED_SIZE;
}
ngx_linefeed(p);

  1. 最后就是将日志串输出到文件或者标准错误流

3. 总结