Parse the log module and log basic initialization and filtering configuration in Nginx _nginx

Source: Internet
Author: User
Tags data structures flush

In any project, the log is a very important module, whether it is the problem of positioning or day-to-day information management, can not be separated from his

In Nginx, the Ngx_errlog_module module is dedicated to processing Nginx log information and is one of the Nginx core modules

In the main function, the initialization of the log module is done immediately after the initialization of the time.

Log structure:
The main thing that the log module initializes is to initialize the global variable ngx_log and create the ErrLog file

NGX_LOG_S structure
The Ngx_log variable is a ngx_log_s structure, defined in the Core/ngx_log.h file, and is structured as follows:

struct ngx_log_s
//log structure {{{
struct ngx_log_s {
 ngx_uint_t   log_level;  Log level
 ngx_open_file_t  *file;   Log file information
 ngx_atomic_uint_t Connection//reference the number of connections to the Log object
 ngx_log_handler_pt handler;  The callback function that needs to be invoked when the log is output is
 void    *data;   The parameters required by the callback function
 ngx_log_writer_pt writer;
 void    *wdata;
  * * We declare "action" as "char *" because the actions are usually
  * the static strings and in the "U_char *" Case we have to override
  * Their types all the time
 /char    *action;  Before logging, save the currently ongoing action
 ngx_log_t   *next;   Point to next Log Object
}; // }}}

Log level
The Nginx log is divided into 9 levels:

#define NGX_LOG_STDERR   0
#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


ngx_open_file_s Structure
the log's file description structure is as follows:

struct ngx_open_file_s
//Open file structure {{{
struct ngx_open_file_s {
 ngx_fd_t    fd;
 ngx_str_t    name;
 void    (*flush) (ngx_open_file_t *file, ngx_log_t *log);
   callback void *data that is invoked when a file is refreshed     ; parameters used by the callback function
};//}}
All file-related data structures and operations are in core/ngx_conf_file.h and core/ngx_conf_file.c files

Log initialization process
log-related initialization is in the Core/ngx_log.c file, and now we're calling the initialization function:

ngx_log_t *ngx_log_init (U_char *prefix);
 Initializes the NGX_LOG structure, creating the ErrLog file {{ngx_log_t * Ngx_log_init (U_char *prefix) {U_char *p, *name;

 size_t Nlen, Plen;
 Ngx_log.file = &ngx_log_file;

 Ngx_log.log_level = Ngx_log_notice;

 Name = (U_char *) Ngx_error_log_path; /* We use Ngx_strlen () This since BCC warns about * condition is always false and unreachable code */Nlen = NGX

 _strlen (name);
  if (Nlen = = 0) {ngx_log_file.fd = Ngx_stderr;
 Return &ngx_log;

 } p = NULL; Determines whether absolute path #if (NGX_WIN32) if (name[1]!= ': ') {#else if (name[0]!= '/') {#endif if (prefix) {Plen = Ngx_str

  Len (prefix);
   else {#ifdef Ngx_prefix PREFIX = (U_char *) Ngx_prefix;
Plen = Ngx_strlen (prefix);
#else Plen = 0;
   #endif} if (plen) {name = malloc (Plen + Nlen + 2);
   if (name = = NULL) {return null;

   } p = ngx_cpymem (name, prefix, Plen);
   if (!ngx_path_separator (* (p-1))) {*p++ = '/'; } ngx_cpystrn (P, (U_char *) Ngx_erroR_log_path, Nlen + 1);
  p = name; } NGX_LOG_FILE.FD = Ngx_open_file (name, Ngx_file_append, Ngx_file_create_or_open, Ngx_file_default_

 ACCESS);
      if (ngx_log_file.fd = = ngx_invalid_file) {ngx_log_stderr (Ngx_errno, "[Alert] could not open error log file:"
Ngx_open_file_n "\"%s\ "failed", name);  #if (Ngx_win32) ngx_event_log (Ngx_errno, "Could not open error log file:" Ngx_open_file_n "\"%s\ "failed",
name);
 #endif ngx_log_file.fd = Ngx_stderr;
 } if (p) {Ngx_free (P);
} return &ngx_log;

 }
// }}}

This initialization process is very easy to understand, then other log operations are also in this file, we encountered after the detailed explanation

Returns the address of the NGX_LOG structure after the function executes

The NGX_LOG structure takes the following values:

{
 Log_level = 6, 
 file = 0x80e3000 <ngx_log_file>, 
 connection = 0, 
 handler = 0x0, 
 data = 0x0,
   writer = 0x0, 
 wdata = 0x0, 
 action = 0x0, 
 next = 0x0
}

The file field is evaluated:

{
 FD = 3, 
 name = {
 len = 0, 
 data = 0x0
 }, 
 flush = 0x0, 
 data = 0x0
}

Using ngx_log_if does not record a specific log
First step:
Download ngx_log_if address to GitHub first https://github.com/cfsego/ngx_log_if/
Step Two:
Install third party module to Nginx. Third-party module installation can refer to Http://wiki.nginx.org/3rdPartyModules using--add-module to add decompressed ngx_log_if as follows

./configure--add-module=/var/local/ngx_log_if-master

Then compile and install Nginx.
Step Three:
Configure ACCESS_LOG_BYPASS_IF to nginx.conf configuration file

server {
 location/{
  access_log_bypass_if ($status = 404); #不记录404状态的所有日志信息
  access_log_bypass_if ($uri ~* ' Images '); #不记录uri中所有images目录下文件的日志信息
  access_log_bypass_if ($uri = '/index.html '); #不记录uri为/index.html log Information
 Access_ Log_bypass_if ($host ~* ' tonv.cc '); All log information for #不记录host为tonv. CC
 }
}

Restart Nginx to filter out specific logs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.