Skip to content

Commit 7f3e43e

Browse files
committed
fix(logging): fix my mess following main rebase
Attempt to follow 8341cc9 design decisions.
1 parent c658d1e commit 7f3e43e

File tree

6 files changed

+105
-49
lines changed

6 files changed

+105
-49
lines changed

frankenphp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ PHP_FUNCTION(frankenphp_log_message) {
560560
Z_PARAM_LONG(level)
561561
ZEND_PARSE_PARAMETERS_END();
562562

563-
go_log_n(message, message_len, (int)level);
563+
go_log_n(thread_index, message, message_len, (int)level);
564564
}
565565

566566
PHP_FUNCTION(frankenphp_log) {
@@ -576,7 +576,7 @@ PHP_FUNCTION(frankenphp_log) {
576576
Z_PARAM_ARRAY(context)
577577
ZEND_PARSE_PARAMETERS_END();
578578

579-
go_log_attrs(message, message_len, (int)level, context);
579+
go_log_attrs(thread_index, message, message_len, (int)level, context);
580580
}
581581

582582
PHP_MINIT_FUNCTION(frankenphp) {

frankenphp.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -628,19 +628,18 @@ func go_read_cookies(threadIndex C.uintptr_t) *C.char {
628628

629629
//export go_log
630630
func go_log(threadIndex C.uintptr_t, message *C.char, level C.int) {
631-
ctx := phpThreads[threadIndex].context()
632631
m := C.GoString(message)
633-
phpLog(m, level)
632+
phpLog(phpThreads[threadIndex].context(), m, level)
634633
}
635634

636635
//export go_log_n
637-
func go_log_n(message *C.char, len C.int, level C.int) {
636+
func go_log_n(threadIndex C.uintptr_t, message *C.char, len C.int, level C.int) {
638637
m := C.GoStringN(message, len)
639-
phpLog(m, level)
638+
phpLog(phpThreads[threadIndex].context(), m, level)
640639
}
641640

642641
//export go_log_attrs
643-
func go_log_attrs(message *C.char, len C.int, level C.int, ctx *C.zval) {
642+
func go_log_attrs(threadIndex C.uintptr_t, message *C.char, len C.int, level C.int, cattrs *C.zval) {
644643
m := C.GoStringN(message, len)
645644

646645
var lvl slog.Level
@@ -650,12 +649,17 @@ func go_log_attrs(message *C.char, len C.int, level C.int, ctx *C.zval) {
650649
lvl = slog.Level(level)
651650
}
652651

653-
attrs, err := GoMap[any](unsafe.Pointer(ctx))
652+
attrs, err := GoMap[any](unsafe.Pointer(cattrs))
654653
if err != nil {
655654
attrs = nil
656655
}
657656

658-
logger.LogAttrs(context.Background(), lvl, m, mapToAttr(attrs)...)
657+
ctx := phpThreads[threadIndex].context()
658+
659+
// NOTE: no need to check for logger enabled levels.
660+
// Check's already done by the logger itself.
661+
// See: https://cs.opensource.google/go/go/+/refs/tags/go1.25.4:src/log/slog/logger.go;l=264
662+
globalLogger.LogAttrs(ctx, lvl, m, mapToAttr(attrs)...)
659663
}
660664

661665
func mapToAttr(input map[string]any) []slog.Attr {
@@ -717,7 +721,7 @@ func mapToAttr(input map[string]any) []slog.Attr {
717721
return out
718722
}
719723

720-
func phpLog(m string, level C.int) {
724+
func phpLog(ctx context.Context, m string, level C.int) {
721725
var le syslogLevel
722726
if level < C.int(syslogLevelEmerg) || level > C.int(syslogLevelDebug) {
723727
le = syslogLevelInfo

frankenphp_test.go

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,29 +1018,62 @@ func FuzzRequest(f *testing.F) {
10181018
}
10191019

10201020
func TestFrankenPHPLog(t *testing.T) {
1021-
var buf bytes.Buffer
1022-
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
1023-
logger := slog.New(handler)
1021+
type tcase struct {
1022+
name, file string
1023+
cases map[string]string
1024+
}
10241025

1025-
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
1026-
body, _ := testGet("http://example.com/logging.php", handler, t)
1027-
assert.Empty(t, body)
1028-
}, &testOptions{
1029-
logger: logger,
1030-
nbParallelRequests: 1,
1031-
nbWorkers: 1,
1032-
})
1026+
test := func(t *testing.T, tc tcase) {
1027+
t.Helper()
1028+
t.Run(tc.name, func(t *testing.T) {
1029+
t.Helper()
1030+
1031+
var buf bytes.Buffer
1032+
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
1033+
logger := slog.New(handler)
1034+
1035+
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
1036+
body, _ := testGet("http://example.com/"+tc.file, handler, t)
1037+
assert.Empty(t, body)
1038+
}, &testOptions{
1039+
logger: logger,
1040+
nbParallelRequests: 1,
1041+
nbWorkers: 1,
1042+
})
10331043

1034-
logOutput := buf.String()
1044+
logOutput := buf.String()
10351045

1036-
t.Logf("captured logs: %s", logOutput)
1046+
t.Logf("captured log output: %s", logOutput)
10371047

1038-
for level, needle := range map[string]string{
1039-
"debug attrs": `level=DEBUG msg="some debug message" "key int"=1`,
1040-
"info attrs": `level=INFO msg="some info message" "key string"=string`,
1041-
"warn attrs": `level=WARN msg="some warn message"`,
1042-
"error attrs": `level=ERROR msg="some error message" err="[a v]"`,
1043-
} {
1044-
assert.Containsf(t, logOutput, needle, "should contains %q log", level)
1048+
for level, needle := range tc.cases {
1049+
assert.Containsf(t, logOutput, needle, "should contains %q log", level)
1050+
}
1051+
})
10451052
}
1053+
1054+
test(t, tcase{
1055+
name: "syslog to slog",
1056+
file: "log_syslog_to_slog.php",
1057+
cases: map[string]string{
1058+
"emerg": "level=ERROR msg=\"testing emerg as ERROR\" syslog_level=emerg\n",
1059+
"alert": "level=ERROR msg=\"testing alert as ERROR\" syslog_level=alert\n",
1060+
"crit": "level=ERROR msg=\"testing crit as ERROR\" syslog_level=crit\n",
1061+
"error": "level=ERROR msg=\"testing error as ERROR\" syslog_level=err\n",
1062+
"warn": "level=WARN msg=\"testing warning as WARN\" syslog_level=warning\n",
1063+
"notice": "level=INFO msg=\"testing notice as INFO\" syslog_level=notice\n",
1064+
"info": "level=INFO msg=\"testing info as INFO\" syslog_level=info\n",
1065+
"debug": "level=DEBUG msg=\"testing debug as DEBUG\" syslog_level=debug\n",
1066+
},
1067+
})
1068+
1069+
test(t, tcase{
1070+
name: "log to slog attrs",
1071+
file: "log_to_slog.php",
1072+
cases: map[string]string{
1073+
"debug attrs": `level=DEBUG msg="some debug message" "key int"=1`,
1074+
"info attrs": `level=INFO msg="some info message" "key string"=string`,
1075+
"warn attrs": `level=WARN msg="some warn message"`,
1076+
"error attrs": `level=ERROR msg="some error message" err="[a v]"`,
1077+
},
1078+
})
10461079
}

testdata/log_syslog_to_slog.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
// NOTE: use CGO frankenpgp_log_meaaage method.
4+
//
5+
// The message is expected to be logged by go' slog system.
6+
// The syslog level should be translated to go' std slog one.
7+
8+
frankenphp_log_message("testing emerg as ERROR", LOG_EMERG);
9+
frankenphp_log_message("testing alert as ERROR", LOG_ALERT);
10+
frankenphp_log_message("testing crit as ERROR", LOG_CRIT);
11+
frankenphp_log_message("testing error as ERROR", LOG_ERR);
12+
frankenphp_log_message("testing warning as WARN", LOG_WARNING);
13+
frankenphp_log_message("testing notice as INFO", LOG_NOTICE);
14+
frankenphp_log_message("testing info as INFO", LOG_INFO);
15+
frankenphp_log_message("testing debug as DEBUG", LOG_DEBUG);

testdata/log_to_slog.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
// NOTE: use CGO frankenphp_log method.
4+
// The message and it's optional arguments are expected to be logged by go' std slog system.
5+
// The log level should be respected out of the box by the std' slog.
6+
//
7+
// ac[0] expect the log message as string
8+
// ac[1] expect the slog.Level, from -8 to +8
9+
// ac[2] is an optional php map, which will be converted to a []slog.Attr
10+
11+
frankenphp_log("some debug message", -4, [
12+
"key int" => 1,
13+
]);
14+
15+
frankenphp_log("some info message", 0, [
16+
"key string" => "string",
17+
]);
18+
19+
frankenphp_log("some warn message", 4);
20+
21+
frankenphp_log("some error message", 8, [
22+
"err" => ["a", "v"],
23+
]);

testdata/logging.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)