others- how to print nginx logs to multiple files or print logs to stdout/console in addition to files?

1. Purpose

In this post, I would demo how to print nginx logs to multiple files or print logs to stdout/console in addition to files.

By default, nginx print logs to files:


But we want to change the log as follows, In addition to print logs to files, we also want to print logs to stdout, for debug purpose.


2. The solution

nginx support multiple lines of access_log and error_log directives, so we can add multiple nginx log directives to the nginx.conf:

http {
	...
	  access_log /var/log/nginx/access.log; # original file
    access_log /dev/stdout; # print access log to stdout
    error_log /var/log/nginx/error.log; # original file
    error_log /dev/stdout info; # print info level error log to stdout
	...
}

The following line is responsible to print access logs to stdout:

    access_log /dev/stdout; # print access log to stdout

The following line is responsible to print error logs to stdout:

error_log /dev/stdout info; # print info level error log to stdout

You can see that we have appended a info suffix to the above command, it’s a log level in nginx, there are 8 levels in nginx:

debug、info、notice、warn、error、crit、alert、emerg


Now we can start nginx in foreground for debug purpose:

nginx -g 'daemon off; master_process off;'


Now we can test the logs:

Let’s visit a normal url:

root@launch-advisor-20191120:/etc/nginx# curl http://localhost:81/
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Hello, Nginx!</title>
</head>
<body>
    <h1>Hello,my Nginx!</h1>
    <p>We have just configured our Nginx web server on Ubuntu Server!</p>
</body>
</html>

Catch the log in console:

::1 - - [11/Jan/2022:16:47:35 +0800] "GET / HTTP/1.1" 200 224 "-" "curl/7.58.0"
2022/01/11 16:47:35 [info] 5690#5690: *2 client ::1 closed keepalive connection

And the logs also exist in /var/log/nginx/access.log:

::1 - - [11/Jan/2022:16:47:35 +0800] "GET / HTTP/1.1" 200 224 "-" "curl/7.58.0"

Then we can test the nginx error log, let’s navigate to a non-exist url:

root@launch-advisor-20191120:/etc/nginx# curl http://localhost:81/adfa
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>

Catch the error log in console:

::1 - - [11/Jan/2022:16:47:28 +0800] "GET /adfa HTTP/1.1" 404 178 "-" "curl/7.58.0"
2022/01/11 16:47:28 [info] 5690#5690: *1 client ::1 closed keepalive connection

And the error log also exist in /var/log/nginx/access.log:

::1 - - [11/Jan/2022:16:47:28 +0800] "GET /adfa HTTP/1.1" 404 178 "-" "curl/7.58.0"


3. Summary

In this post, I demonstrated how to print nginx logs to multiple targets including stdout/console, the key point is that you can write multiple access_log and error_log directives in nginx.conf, or you can just override the directive in your server blocks. That’s it, thanks for your reading.