-
Notifications
You must be signed in to change notification settings - Fork 407
feat: add frankenphp_log_message() as a PHP function
#1979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
0f0d182
ee4dbbb
ee7d418
ba25c2c
68345ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -682,6 +682,81 @@ func go_log(threadIndex C.uintptr_t, message *C.char, level C.int) { | |||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // go_log_attrs is a cgo-exported bridge between PHP and the Go slog logger. | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // It is called from C/PHP and must not panic. All errors are reported by | ||||||||||||||||||||||||||||||||||||||||
| // returning a C-allocated error string; on success it returns NULL. | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // Parameters: | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // threadIndex: | ||||||||||||||||||||||||||||||||||||||||
| // - Index into the phpThreads table, used to retrieve the Go context for | ||||||||||||||||||||||||||||||||||||||||
| // the current PHP request/thread. | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // message: | ||||||||||||||||||||||||||||||||||||||||
| // - Pointer to a C string containing the log message bytes. The memory | ||||||||||||||||||||||||||||||||||||||||
| // is owned by the caller and must NOT be freed by Go. | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // len: | ||||||||||||||||||||||||||||||||||||||||
| // - Length of the message, in bytes, as seen from C (not including the | ||||||||||||||||||||||||||||||||||||||||
| // terminating NUL). This is passed to C.GoStringN to build the Go string. | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // level: | ||||||||||||||||||||||||||||||||||||||||
| // - Numeric log level compatible with slog.Level values. It is cast to | ||||||||||||||||||||||||||||||||||||||||
| // slog.Level inside this function. | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // cattrs: | ||||||||||||||||||||||||||||||||||||||||
| // - Pointer to a PHP zval representing an associative array of attributes, | ||||||||||||||||||||||||||||||||||||||||
| // or NULL. When non-NULL, it is converted to map[string]any via GoMap[any] | ||||||||||||||||||||||||||||||||||||||||
| // and then mapped to slog.Attr values (using slog.Any under the hood). | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // Return value: | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // On success: | ||||||||||||||||||||||||||||||||||||||||
| // - Returns NULL and the message is logged (if the logger is enabled at | ||||||||||||||||||||||||||||||||||||||||
| // the given level). | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // On error: | ||||||||||||||||||||||||||||||||||||||||
| // - Returns a non-NULL *C.char pointing to a NUL-terminated error message | ||||||||||||||||||||||||||||||||||||||||
| // allocated with C.CString. The caller is responsible for releasing | ||||||||||||||||||||||||||||||||||||||||
| // this memory. | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
AlliBalliBaba marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||
| //export go_log_attrs | ||||||||||||||||||||||||||||||||||||||||
| func go_log_attrs(threadIndex C.uintptr_t, message *C.char, len C.int, level C.int, cattrs *C.zval) *C.char { | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| func go_log_attrs(threadIndex C.uintptr_t, message *C.char, len C.int, level C.int, cattrs *C.zval) *C.char { | |
| func go_log_attrs(threadIndex C.uintptr_t, message *C.zend_string, level C.zend_long, cattrs *C.zval) *C.char { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AlliBalliBaba marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| m := C.GoStringN(message, len) | |
| lvl := slog.Level(level) | |
| ctx := phpThreads[threadIndex].context() | |
| if globalLogger.Enabled(ctx, lvl) { | |
| globalLogger.LogAttrs(ctx, lvl, m, mapToAttr(attrs)...) | |
| } | |
| return nil | |
| } | |
| ctx := phpThreads[threadIndex].context() | |
| if globalLogger.Enabled(ctx, lvl) { | |
| globalLogger.LogAttrs(ctx, slog.Level(level), GoString(unsafe.Pointer(m)), mapToAttr(attrs)...) | |
| } | |
| return nil | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| // NOTE: use CGO frankenphp_log method. | ||
| // The message and it's optional arguments are expected to be logged by go' std slog system. | ||
| // The log level should be respected out of the box by the std' slog. | ||
| // | ||
| // ac[0] expect the log message as string | ||
| // ac[1] expect the slog.Level, from -8 to +8 | ||
| // ac[2] is an optional php map, which will be converted to a []slog.Attr | ||
|
|
||
| frankenphp_log("some debug message", -4, [ | ||
| "key int" => 1, | ||
| ]); | ||
|
|
||
| frankenphp_log("some info message", 0, [ | ||
| "key string" => "string", | ||
| ]); | ||
|
|
||
| frankenphp_log("some warn message", 4); | ||
|
|
||
| frankenphp_log("some error message", 8, [ | ||
| "err" => ["a", "v"], | ||
| ]); |
Uh oh!
There was an error while loading. Please reload this page.