What PHP programmers must know about enabling access logs and enabling error logs

php是最好的语言
Release: 2023-04-03 13:44:01
Original
4468 people have browsed it

What PHP programmers must know about enabling access logs and enabling error logs

Preface

I originally planned to explain the logs of nginx and apache, but I personally don’t recommend apache (it’s purely a personal hobby), so I won’t introduce the logs of apache here. .

As a programmer, something a little more important than coding is log analysis and query. Common logs and setting methods are listed below.

Configuration file

nginx is divided into two types of logs: access_log and error_log

The settings need to be in nginx.conf. By default, the nginx directory should be compiled and installed through the source code package in

/usr/local/nginx
Copy after login

directory, if you install it through yum or other methods, and you don’t know or don’t know the specific installation directory of nginx, you can use

find / -name nginx.conf
Copy after login

or

nginx -V | grep prefix
-------------
nginx version: nginx/1.13.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
Copy after login

to turn on the access log

If your source code package is installed by default, open the path as follows

vim /usr/local/nginx/nginx.conf
Copy after login
Copy after login

Find the following content

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    
    ...
}
Copy after login

Just open the comments from log_format to access_log. log_format can define the log specifications of nginx.

log_format default specification parameter table

NameAnnotation
$remote_addrClient/user IP address
$time_localAccess time
$requestRequest method Request address
$statusThe request status code is consistent with the HTTP status code
$body_bytes_sentThe requested address size is calculated in bytes format
$http_refererThe request source, where it is accessed from
$http_user_agentUser information (browser information)
$http_x_forwarded_forForwarded IP address

Enable error log

If your source code package is installed by default, open the path as follows

vim /usr/local/nginx/nginx.conf
Copy after login
Copy after login

Find the following content

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
Copy after login

Just delete the annotation. You can store different error types separately. For example,

error_log logs/error.log notice;
Copy after login

notice is the error type. If you don't write it, it will be all.

Acknowledgment

Thank you for reading this. I will write some more articles about log operation and analysis later, I hope they can help you. Thank you

The code changes, but the original intention remains the same

Related articles:

php-cgi.exe Turn on the error log

Teach you how to enable PHP's error_log

Related videos:

PHP development practical tutorial on making a simple calendar

The above is the detailed content of What PHP programmers must know about enabling access logs and enabling error logs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!