Skip to content

Commit c47ae88

Browse files
fix: include FrankenPHP version in startup logs
1 parent b1ccb0e commit c47ae88

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

frankenphp.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package frankenphp
1818
// #include <php_variables.h>
1919
// #include <zend_llist.h>
2020
// #include <SAPI.h>
21+
// static inline const char *frankenphp_get_build_version() {
22+
// return TOSTRING(FRANKENPHP_VERSION);
23+
// }
2124
import "C"
2225
import (
2326
"bytes"
@@ -145,6 +148,10 @@ func Version() PHPVersion {
145148
}
146149
}
147150

151+
func frankenPHPVersion() string {
152+
return C.GoString(C.frankenphp_get_build_version())
153+
}
154+
148155
func Config() PHPConfig {
149156
cConfig := C.frankenphp_get_config()
150157

@@ -342,7 +349,7 @@ func Init(options ...Option) error {
342349
initAutoScaling(mainThread)
343350

344351
if globalLogger.Enabled(globalCtx, slog.LevelInfo) {
345-
globalLogger.LogAttrs(globalCtx, slog.LevelInfo, "FrankenPHP started 🐘", slog.String("php_version", Version().Version), slog.Int("num_threads", mainThread.numThreads), slog.Int("max_threads", mainThread.maxThreads), slog.Int("max_requests", maxRequestsPerThread))
352+
globalLogger.LogAttrs(globalCtx, slog.LevelInfo, startupLogMessage, startupLogAttrs(Version().Version, mainThread.numThreads, mainThread.maxThreads, maxRequestsPerThread)...)
346353

347354
if EmbeddedAppPath != "" {
348355
globalLogger.LogAttrs(globalCtx, slog.LevelInfo, "embedded PHP app 📦", slog.String("path", EmbeddedAppPath))

startup_log.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package frankenphp
2+
3+
import "log/slog"
4+
5+
const (
6+
startupLogMessage = "FrankenPHP started 🐘"
7+
startupLogAttrVersion = "version"
8+
startupLogAttrPHPVersion = "php_version"
9+
startupLogAttrNumThreads = "num_threads"
10+
startupLogAttrMaxThreads = "max_threads"
11+
startupLogAttrMaxRequests = "max_requests"
12+
startupLogAttrCapacity = 5
13+
)
14+
15+
func startupLogAttrs(phpVersion string, numThreads int, maxThreads int, maxRequests int) []slog.Attr {
16+
attrs := make([]slog.Attr, 0, startupLogAttrCapacity)
17+
if version := frankenPHPVersion(); version != "" {
18+
attrs = append(attrs, slog.String(startupLogAttrVersion, version))
19+
}
20+
21+
return append(attrs,
22+
slog.String(startupLogAttrPHPVersion, phpVersion),
23+
slog.Int(startupLogAttrNumThreads, numThreads),
24+
slog.Int(startupLogAttrMaxThreads, maxThreads),
25+
slog.Int(startupLogAttrMaxRequests, maxRequests),
26+
)
27+
}

startup_log_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package frankenphp
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
const startupLogTestExpectedVersionEnv = "FRANKENPHP_EXPECT_VERSION"
9+
10+
func TestStartupLogAttrsIncludeFrankenPHPVersion(t *testing.T) {
11+
const (
12+
testPHPVersion = "8.2.31"
13+
testNumThreads = 4
14+
testMaxThreads = 8
15+
testMaxRequests = 0
16+
)
17+
18+
expectedFrankenPHPVersion := os.Getenv(startupLogTestExpectedVersionEnv)
19+
if expectedFrankenPHPVersion == "" {
20+
expectedFrankenPHPVersion = frankenPHPVersion()
21+
}
22+
23+
attrs := startupLogAttrs(testPHPVersion, testNumThreads, testMaxThreads, testMaxRequests)
24+
if len(attrs) == 0 {
25+
t.Fatal("expected startup log attrs")
26+
}
27+
if attrs[0].Key != startupLogAttrVersion {
28+
t.Fatalf("expected first startup log attr key %q, got %q", startupLogAttrVersion, attrs[0].Key)
29+
}
30+
if got := attrs[0].Value.String(); got != expectedFrankenPHPVersion {
31+
t.Fatalf("expected startup log version %q, got %q", expectedFrankenPHPVersion, got)
32+
}
33+
}

0 commit comments

Comments
 (0)