- char *
- ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)
- {
- ngx_uint_t i, n, d, found;
- ngx_str_t *value;
- value = cf->args->elts;
- for (i = 2; i < cf->args->nelts; i++) {
- found = 0;
- for (n = 1; n <= NGX_LOG_DEBUG; n++) {
- if (ngx_strcmp(value[i].data, err_levels[n].data) == 0) {
- if (log->log_level != 0) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "duplicate log level \\"%V\\"",
- &value[i]);
- return NGX_CONF_ERROR;
- }
- log->log_level = n;
- found = 1;
- break;
- }
- }
- for (n = 0, d = NGX_LOG_DEBUG_FIRST; d <= NGX_LOG_DEBUG_LAST; d <<= 1) {
- if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) {
- //若已设置err_levels,则不允许设置debug level。
- //即error_log logs/error.log DEBUG NGX_LOG_DEBUG;将报错。
- if (log->log_level & ~NGX_LOG_DEBUG_ALL) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "invalid log level \\"%V\\"",
- &value[i]);
- return NGX_CONF_ERROR;
- }
- log->log_level |= d;
- found = 1;
- break;
- }
- }
- if (!found) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "invalid log level \\"%V\\"", &value[i]);
- return NGX_CONF_ERROR;
- }
- }
- // 如果error_log logs/error.log NGX_LOG_DEBUG;相当于设置了全部的debug level.
- if (log->log_level == NGX_LOG_DEBUG) {
- log->log_level = NGX_LOG_DEBUG_ALL;
- }
- return NGX_CONF_OK;
- }
- static char *
- ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
- {
- ngx_str_t *value, name;
- if (cf->cycle->new_log.file) {
- return "is duplicate";
- }
- value = cf->args->elts;
- if (ngx_strcmp(value[1].data, "stderr") == 0) {
- ngx_str_null(&name);
- } else {
- name = value[1];
- }
- cf->cycle->new_log.file = ngx_conf_open_file(cf->cycle, &name);
- if (cf->cycle->new_log.file == NULL) {
- return NULL;
- }
- if (cf->args->nelts == 2) {
- cf->cycle->new_log.log_level = NGX_LOG_ERR;
- return NGX_CONF_OK;
- }
- cf->cycle->new_log.log_level = 0;
- return ngx_log_set_levels(cf, &cf->cycle->new_log);
- }
- //该片段来自于http://www.codesnippet.cn/detail/051220137756.html
来源: http://www.codesnippet.cn/detail/051220137756.html