Skip to content

Commit 3a5dcb1

Browse files
author
Mikhail Galanin
committed
Add URI in FPM logs
1 parent 4a77a1e commit 3a5dcb1

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

sapi/fpm/fpm/zlog.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#include "fpm_config.h"
44

5+
#include "SAPI.h"
6+
#include "php.h"
7+
58
#include <stdio.h>
69
#include <unistd.h>
710
#include <time.h>
@@ -10,10 +13,16 @@
1013
#include <sys/time.h>
1114
#include <errno.h>
1215

16+
#include "zend.h"
17+
#include "zend_extensions.h"
18+
#include "php_ini.h"
19+
#include "php_globals.h"
20+
#include "php_main.h"
1321
#include "php_syslog.h"
1422

1523
#include "zlog.h"
1624
#include "fpm.h"
25+
#include "fastcgi.h"
1726
#include "zend_portability.h"
1827

1928
/* buffer is used for fmt result and it should never be over 2048 */
@@ -134,7 +143,7 @@ static inline size_t zlog_truncate_buf(char *buf, size_t buf_size, size_t space_
134143
/* }}} */
135144

136145
static inline void zlog_external(
137-
int flags, char *buf, size_t buf_size, const char *fmt, va_list args) /* {{{ */
146+
int flags, char *buf, size_t buf_size, char *url, size_t url_len, const char *fmt, va_list args) /* {{{ */
138147
{
139148
va_list ap;
140149
size_t len;
@@ -143,6 +152,10 @@ static inline void zlog_external(
143152
len = vsnprintf(buf, buf_size, fmt, ap);
144153
va_end(ap);
145154

155+
if (url_len > 0 && buf_size > len) {
156+
len += snprintf(buf + len, buf_size - len, "%s", url);
157+
}
158+
146159
if (len >= buf_size) {
147160
len = zlog_truncate_buf(buf, buf_size, 0);
148161
}
@@ -193,13 +206,23 @@ static size_t zlog_buf_prefix(
193206
void vzlog(const char *function, int line, int flags, const char *fmt, va_list args) /* {{{ */
194207
{
195208
char buf[MAX_BUF_LENGTH];
209+
char url[MAX_BUF_LENGTH];
210+
size_t url_len = 0;
196211
size_t buf_size = MAX_BUF_LENGTH;
197212
size_t len = 0;
198213
int truncated = 0;
199214
int saved_errno;
215+
char *server_name, *document_uri;
216+
fcgi_request *request = (fcgi_request*) SG(server_context);
217+
218+
if (request) {
219+
server_name = FCGI_GETENV(request, "SERVER_NAME");
220+
document_uri = FCGI_GETENV(request, "DOCUMENT_URI");
221+
url_len = snprintf(url, sizeof(url), " URI: %s%s", server_name ? server_name : "*unknown*", document_uri ? document_uri : "*unknown_uri*");
222+
}
200223

201224
if (external_logger) {
202-
zlog_external(flags, buf, buf_size, fmt, args);
225+
zlog_external(flags, buf, buf_size, url, url_len, fmt, args);
203226
}
204227

205228
if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
@@ -213,6 +236,11 @@ void vzlog(const char *function, int line, int flags, const char *fmt, va_list a
213236
truncated = 1;
214237
} else {
215238
len += vsnprintf(buf + len, buf_size - len, fmt, args);
239+
240+
if (url_len > 0 && buf_size > len) {
241+
len += snprintf(buf + len, buf_size - len, "%s", url);
242+
}
243+
216244
if (len >= buf_size) {
217245
truncated = 1;
218246
}

sapi/fpm/tests/bug69625-no-script-filename.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ $code = <<<EOT
2424
echo "Test\n";
2525
EOT;
2626

27+
$host = gethostname();
28+
$path = str_replace('.php', '.src.php', realpath(__FILE__));
29+
2730
$tester = new FPM\Tester($cfg, $code);
2831
$tester->start();
2932
$tester->expectLogStartNotices();
3033
$tester
3134
->request('', ['SCRIPT_FILENAME' => null])
3235
->expectHeader('Status', '404 Not Found')
33-
->expectError('Primary script unknown');
36+
->expectError("Primary script unknown URI: $host$path");
3437
$tester->terminate();
3538
$tester->close();
3639

0 commit comments

Comments
 (0)