You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2020/11/25 05:10:17 UTC

[apisix] branch master updated: feat: enable access log or not (#2769)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 1d0c719  feat: enable access log or not (#2769)
1d0c719 is described below

commit 1d0c719a7bb8e38cf7cc158b33158daaff12bf41
Author: HelloBug0 <73...@users.noreply.github.com>
AuthorDate: Wed Nov 25 13:10:08 2020 +0800

    feat: enable access log or not (#2769)
    
    Fix #2713.
---
 .travis/apisix_cli_test.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++--
 apisix/cli/ngx_tpl.lua     |  4 +++
 conf/config-default.yaml   |  1 +
 3 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/.travis/apisix_cli_test.sh b/.travis/apisix_cli_test.sh
index 2e142fe..299647d 100755
--- a/.travis/apisix_cli_test.sh
+++ b/.travis/apisix_cli_test.sh
@@ -422,12 +422,86 @@ if [ ! $? -eq 0 ]; then
     exit 1
 fi
 
-git checkout conf/config.yaml
+echo "passed: access_log_format in nginx.conf is ok"
 
-echo "passed: worker_processes number is configurable"
+# check enable access log
+
+echo '
+nginx_config:
+  http:
+    enable_access_log: true
+    access_log_format: "$remote_addr - $remote_user [$time_local] $http_host test_enable_access_log_true"
+' > conf/config.yaml
+
+make init
+
+count_test_access_log=`grep -c "test_enable_access_log_true" conf/nginx.conf || true`
+if [ $count_test_access_log -eq 0 ]; then
+    echo "failed: nginx.conf file doesn't find access_log_format when enable access log"
+    exit 1
+fi
+
+count_access_log_off=`grep -c "access_log off;" conf/nginx.conf || true`
+if [ $count_access_log_off -eq 2 ]; then
+    echo "failed: nginx.conf file find access_log off; when enable access log"
+    exit 1
+fi
+
+make run
+sleep 0.1
+curl http://127.0.0.1:9080/hi
+sleep 4
+tail -n 1 logs/access.log > output.log
+
+count_grep=`grep -c "test_enable_access_log_true" output.log || true`
+if [ $count_grep -eq 0 ]; then
+    echo "failed: not found test_enable_access_log in access.log "
+    exit 1
+fi
+
+make stop
+
+echo '
+nginx_config:
+  http:
+    enable_access_log: false
+    access_log_format: "$remote_addr - $remote_user [$time_local] $http_host test_enable_access_log_false"
+' > conf/config.yaml
+
+make init
+
+count_test_access_log=`grep -c "test_enable_access_log_false" conf/nginx.conf || true`
+if [ $count_test_access_log -eq 1 ]; then
+    echo "failed: nginx.conf file find access_log_format when disable access log"
+    exit 1
+fi
+
+count_access_log_off=`grep -c "access_log off;" conf/nginx.conf || true`
+if [ $count_access_log_off -ne 2 ]; then
+    echo "failed: nginx.conf file doesn't find access_log off; when disable access log"
+    exit 1
+fi
+
+make run
+sleep 0.1
+curl http://127.0.0.1:9080/hi
+sleep 4
+tail -n 1 logs/access.log > output.log
+
+count_grep=`grep -c "test_enable_access_log_false" output.log || true`
+if [ $count_grep -eq 1 ]; then
+    echo "failed: found test_enable_access_log in access.log "
+    exit 1
+fi
+
+make stop
+
+echo "passed: enable_access_log is ok"
 
 # missing admin key, allow any IP to access admin api
 
+git checkout conf/config.yaml
+
 echo '
 apisix:
   allow_admin: ~
diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index 9e17f4c..3d0440a 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -171,9 +171,13 @@ http {
     lua_regex_match_limit 100000;
     lua_regex_cache_max_entries 8192;
 
+    {% if http.enable_access_log == false then %}
+    access_log off;
+    {% else %}
     log_format main escape={* http.access_log_format_escape *} '{* http.access_log_format *}';
 
     access_log {* http.access_log *} main buffer=16384 flush=3;
+    {% end %}
     open_file_cache  max=1000 inactive=60;
     client_max_body_size {* http.client_max_body_size *};
     keepalive_timeout {* http.keepalive_timeout *};
diff --git a/conf/config-default.yaml b/conf/config-default.yaml
index ca947c4..2acc4db 100644
--- a/conf/config-default.yaml
+++ b/conf/config-default.yaml
@@ -131,6 +131,7 @@ nginx_config:                     # config for render the template to genarate n
   #envs:                            # allow to get a list of environment variables
   #  - TEST_ENV
   http:
+    enable_access_log: true        # enable access log or not, default true
     access_log: "logs/access.log"
     access_log_format: "$remote_addr - $remote_user [$time_local] $http_host \"$request\" $status $body_bytes_sent $request_time \"$http_referer\" \"$http_user_agent\" $upstream_addr $upstream_status $upstream_response_time \"$upstream_scheme://$upstream_host$upstream_uri\""
     access_log_format_escape: default       # allows setting json or default characters escaping in variables