首页 > 技术文章 > nginx如何写日志

szprg 2015-06-19 15:36 原文

写日志函数为ngx_log_error_core,位于src/core/ngx_log.c:89行
核心代码如下:
while (log) {

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

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

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

        log = log->next;
    }

其中ngx_write_fd为
static ngx_inline ssize_t
ngx_write_fd(ngx_fd_t fd, void *buf, size_t n)
{
    return write(fd, buf, n);
}

即nginx中写日志是没有作什么特别的优化处理,完全依赖操作系统提供的异步写来保证性能

推荐阅读