Browse Source

logs

Support for custom log format and custom error log level.
Cristhian Martínez Ochoa 2 years ago
parent
commit
45b8d75e8e
5 changed files with 66 additions and 37 deletions
  1. 9 0
      lib/general
  2. 5 1
      lib/sites
  3. 19 0
      templates/general/conf
  4. 16 5
      templates/nginx/nginx.conf
  5. 17 31
      usr/log

+ 9 - 0
lib/general

@@ -1315,6 +1315,15 @@ check_var() {
 	# FastCGI Cache size
 	# FastCGI Cache size
 	elif [[ $1 == "run-folder-size" ]]; then
 	elif [[ $1 == "run-folder-size" ]]; then
 		[[ -n $(conf_read run-folder-size) && $(conf_read run-folder-size) =~ ^[0-9]+$ && $(conf_read run-folder-size) -gt 10 && $(conf_read run-folder-size) -le "70" ]] && local out=$(conf_read run-folder-size) || local out=25
 		[[ -n $(conf_read run-folder-size) && $(conf_read run-folder-size) =~ ^[0-9]+$ && $(conf_read run-folder-size) -gt 10 && $(conf_read run-folder-size) -le "70" ]] && local out=$(conf_read run-folder-size) || local out=25
+	
+	# Nginx Error Log Level
+	elif [[ $1 == "nginx-error-log-level" ]]; then
+		[[ -n $(conf_read nginx-error-log-level) && $(conf_read nginx-error-log-level) =~ ^(info|notice|warn|error|crit|alert|emerg)$ ]] && local out=$(conf_read nginx-error-log-level) || local out="false"
+	# Nginx Log Format
+	elif [[ $1 == "nginx-log-format" ]]; then
+		local out="we_log" # Basic is fallback!
+		[[ $(conf_read nginx-log-format) == "extended" ]] && local out="we_log_ext"
+		[[ $(conf_read nginx-log-format) == "custom" ]] && local out="we_log_custom"
 		
 		
 	else
 	else
 		local out="false"
 		local out="false"

+ 5 - 1
lib/sites

@@ -788,7 +788,11 @@ createsite() {
 	fi
 	fi
 	
 	
 	# Check if only-error log is enabled
 	# Check if only-error log is enabled
-	[[ $(conf_read global-access-log-off) == "true" && $domain != $(conf_read tools-port) ]] && sudo log $domain -only-error=on > /dev/null 2>&1
+	if [[ $(conf_read global-access-log-off) == "true" && $domain != $(conf_read tools-port) ]]; then
+		sudo log $domain -only-error=on > /dev/null 2>&1
+	else
+		sudo log $domain -only-error=off > /dev/null 2>&1
+	fi
 	
 	
 	sudo chown -R www-data:www-data /var/www
 	sudo chown -R www-data:www-data /var/www
 	[[ $(conf_read login-www-data) == "true" ]] && sudo chown root:root /var/www
 	[[ $(conf_read login-www-data) == "true" ]] && sudo chown root:root /var/www

+ 19 - 0
templates/general/conf

@@ -113,6 +113,25 @@
 #global-access-log-off:true
 #global-access-log-off:true
 
 
 
 
+# Nginx Log Format
+# Values: basic | extended | custom
+# Default: basic
+# With server reset only the global setting is applied.
+# Note: This setting will be applied to new sites, or for existing sites 'access log' needs to be reenabled (same for global).
+# Note: https://webinoly.com/documentation/logs/#format
+###########################
+#nginx-log-format:basic
+
+
+# Nginx Error Log Level
+# Values: info | notice | warn | error | crit | alert | emerg
+# Default: error
+# With server reset only the global setting is applied.
+# Note: This setting will be applied to new sites, or for existing sites 'access log' needs to be reenabled (same for global).
+###########################
+#nginx-error-log-level:error
+
+
 # Nginx Default Response
 # Nginx Default Response
 # Values: default | blackhole | <example.com>
 # Values: default | blackhole | <example.com>
 # Default: default (Nginx Welcome Page)
 # Default: default (Nginx Welcome Page)

+ 16 - 5
templates/nginx/nginx.conf

@@ -73,15 +73,26 @@ http {
 	##
 	##
 	# Logging Settings
 	# Logging Settings
 	##
 	##
+	
+	log_format we_log '$remote_addr $upstream_response_time $upstream_cache_status [$time_local] '
+		'$http_host "$request" $status $body_bytes_sent $request_time '
+		'"$http_referer" "$http_user_agent"';
 
 
-	access_log /var/log/nginx/access.log;
-	error_log /var/log/nginx/error.log;
+	log_format we_log_ext '$remote_addr - $remote_user [$time_local] "$request" '
+		'$status $body_bytes_sent "$http_referer" '
+		'"$http_user_agent" "$http_x_forwarded_for" '
+		'"$host" sn="$server_name" '
+		'rt=$request_time '
+		'ua="$upstream_addr" us="$upstream_status" '
+		'ut="$upstream_response_time" ul="$upstream_response_length" '
+		'cs=$upstream_cache_status' ;
 	
 	
+	include /etc/nginx/conf.d/*.conf.log;
 	open_log_file_cache max=1000 inactive=30s valid=1m;
 	open_log_file_cache max=1000 inactive=30s valid=1m;
 	
 	
-	log_format we_log '$remote_addr $upstream_response_time $upstream_cache_status [$time_local] '
-	'$http_host "$request" $status $body_bytes_sent $request_time '
-	'"$http_referer" "$http_user_agent"';
+	access_log /var/log/nginx/access.log we_log;
+	error_log /var/log/nginx/error.log;
+	
 
 
 	##
 	##
 	# Gzip Settings
 	# Gzip Settings

+ 17 - 31
usr/log

@@ -219,28 +219,20 @@ elif [[ -n $only_error ]]; then
 	
 	
 	# Global-Conf
 	# Global-Conf
 	if [[ -z $domain ]]; then
 	if [[ -z $domain ]]; then
-		[[ $(conf_read nginx) == "true" ]] && islog=$( grep -F "access_log off;" /etc/nginx/nginx.conf )
 		if [[ $only_error == "on" ]]; then
 		if [[ $only_error == "on" ]]; then
 			api-events_update lg1
 			api-events_update lg1
-			if [[ -z $islog ]]; then
-				[[ $(conf_read nginx) == "true" ]] && sudo sed -i '/access_log/c \	access_log off;' /etc/nginx/nginx.conf
-				conf_write global-access-log-off true
-				sudo systemctl reload nginx
-				echo "${gre}Only-Error Log was successfully enabled (global)!${end}"
-			else	
-				echo "${gre}Only-Error Log is already enabled (global)!${end}"
-			fi
+			[[ $(conf_read nginx) == "true" ]] && sudo sed -i '/access_log /c \	access_log off;' /etc/nginx/nginx.conf
+			conf_write global-access-log-off true
+			echo "${gre}Only-Error Log was successfully enabled (global)!${end}"
 		elif [[ $only_error == "off" ]]; then
 		elif [[ $only_error == "off" ]]; then
 			api-events_update lg2
 			api-events_update lg2
-			if [[ -n $islog || ( -z $islog && $(conf_read nginx) != "true" ) ]]; then
-				[[ $(conf_read nginx) == "true" ]] && sudo sed -i "/access_log/c \	access_log \/var\/log\/nginx\/access.log;" /etc/nginx/nginx.conf
-				conf_write global-access-log-off false
-				sudo systemctl reload nginx
-				echo "${gre}Only-Error Log was successfully disabled (global)!${end}"
-			else	
-				echo "${gre}Only-Error Log is already disabled (global)!${end}"
-			fi
+			[[ $(conf_read nginx) == "true" ]] && sudo sed -i "/access_log /c \	access_log \/var\/log\/nginx\/access.log $(check_var nginx-log-format);" /etc/nginx/nginx.conf
+			conf_write global-access-log-off false
+			echo "${gre}Only-Error Log was successfully disabled (global)!${end}"
 		fi
 		fi
+		
+		[[ $(check_var nginx-error-log-level) != "false" ]] && sudo sed -i "/error_log /c \	error_log \/var\/log\/nginx\/error.log $(check_var nginx-error-log-level);" /etc/nginx/nginx.conf
+		sudo systemctl reload nginx
 	fi
 	fi
 	
 	
 	# Per site
 	# Per site
@@ -253,23 +245,17 @@ elif [[ -n $only_error ]]; then
 		
 		
 		if [[ $only_error == "on" ]]; then
 		if [[ $only_error == "on" ]]; then
 			api-events_update lg3
 			api-events_update lg3
-			if [[ $(is_log $domain) == "true" ]]; then
-				sudo sed -i '/access_log/c \	access_log off;' /etc/nginx/sites-available/$domain
-				sudo systemctl reload nginx
-				echo "${gre}Only-Error Log was successfully enabled for ${blu}${domain} ${gre}site!${end}"
-			else	
-				echo "${gre}Access Log is already enabled for ${blu}${domain} ${gre}site!${end}"
-			fi
+			sudo sed -i '/access_log /c \	access_log off;' /etc/nginx/sites-available/$domain
+			echo "${gre}Only-Error Log was successfully enabled for ${blu}${domain} ${gre}site!${end}"
 		elif [[ $only_error == "off" ]]; then
 		elif [[ $only_error == "off" ]]; then
 			api-events_update lg4
 			api-events_update lg4
-			if [[ $(is_log $domain) != "true" ]]; then
-				sudo sed -i "/access_log/c \	access_log \/var\/log\/nginx\/${domain}.access.log we_log;" /etc/nginx/sites-available/$domain
-				sudo systemctl reload nginx
-				echo "${gre}Only-Error Log was successfully disabled for ${blu}${domain} ${gre}site!${end}"
-			else	
-				echo "${gre}Access Log is already disabled for ${blu}${domain} ${gre}site!${end}"
-			fi
+			sudo sed -i "/access_log /c \	access_log \/var\/log\/nginx\/${domain}.access.log $(check_var nginx-log-format);" /etc/nginx/sites-available/$domain
+			echo "${gre}Only-Error Log was successfully disabled for ${blu}${domain} ${gre}site!${end}"
 		fi
 		fi
+		
+		[[ $(check_var nginx-error-log-level) != "false" ]] && sudo sed -i "/error_log /c \	error_log \/var\/log\/nginx\/${domain}.error.log $(check_var nginx-error-log-level);" /etc/nginx/sites-available/$domain
+		sudo systemctl reload nginx
+		
 	elif [[ -n $domain && ! -f /etc/nginx/sites-available/$domain ]]; then
 	elif [[ -n $domain && ! -f /etc/nginx/sites-available/$domain ]]; then
 		echo "${red}[ERROR] Domain not found!${end}"
 		echo "${red}[ERROR] Domain not found!${end}"
 		exit 1
 		exit 1