mirror of
https://github.com/dunglas/frankenphp.git
synced 2025-12-24 13:38:11 +08:00
feat: improve PHP logger
This commit is contained in:
@@ -48,6 +48,41 @@ var (
|
||||
logger *zap.Logger
|
||||
)
|
||||
|
||||
type syslogLevel int
|
||||
|
||||
const (
|
||||
emerg syslogLevel = iota /* system is unusable */
|
||||
alert /* action must be taken immediately */
|
||||
crit /* critical conditions */
|
||||
err /* error conditions */
|
||||
warning /* warning conditions */
|
||||
notice /* normal but significant condition */
|
||||
info /* informational */
|
||||
debug /* debug-level messages */
|
||||
|
||||
)
|
||||
|
||||
func (l syslogLevel) String() string {
|
||||
switch l {
|
||||
case emerg:
|
||||
return "emerg"
|
||||
case alert:
|
||||
return "alert"
|
||||
case crit:
|
||||
return "crit"
|
||||
case err:
|
||||
return "err"
|
||||
case warning:
|
||||
return "warning"
|
||||
case notice:
|
||||
return "notice"
|
||||
case debug:
|
||||
return "debug"
|
||||
default:
|
||||
return "info"
|
||||
}
|
||||
}
|
||||
|
||||
// FrankenPHP executes PHP scripts.
|
||||
type FrankenPHPContext struct {
|
||||
// The root directory of the PHP application.
|
||||
@@ -160,15 +195,18 @@ func Init(options ...Option) error {
|
||||
}
|
||||
}
|
||||
|
||||
logger.Debug("FrankenPHP started")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Shutdown() {
|
||||
logger.Debug("FrankenPHP shutting down")
|
||||
stopWorkers()
|
||||
close(requestChan)
|
||||
shutdownWG.Wait()
|
||||
requestChan = nil
|
||||
|
||||
logger.Debug("FrankenPHP shut down")
|
||||
}
|
||||
|
||||
//export go_shutdown
|
||||
@@ -434,19 +472,16 @@ func go_log(message *C.char, level C.int) {
|
||||
m := C.GoString(message)
|
||||
|
||||
switch level {
|
||||
case 0, 1, 2:
|
||||
l.DPanic(m)
|
||||
|
||||
case 3:
|
||||
l.Error(m)
|
||||
case 0, 1, 2, 3:
|
||||
l.Error(m, zap.Stringer("syslog_level", syslogLevel(level)))
|
||||
|
||||
case 4:
|
||||
l.Warn(m)
|
||||
l.Warn(m, zap.Stringer("syslog_level", syslogLevel(level)))
|
||||
|
||||
case 7:
|
||||
l.Debug(m)
|
||||
l.Debug(m, zap.Stringer("syslog_level", syslogLevel(level)))
|
||||
|
||||
default:
|
||||
l.Info(m)
|
||||
l.Info(m, zap.Stringer("syslog_level", syslogLevel(level)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@ import (
|
||||
"github.com/dunglas/frankenphp"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest"
|
||||
"go.uber.org/zap/zaptest/observer"
|
||||
)
|
||||
|
||||
type testOptions struct {
|
||||
@@ -23,6 +26,8 @@ type testOptions struct {
|
||||
nbWorkers int
|
||||
nbParrallelRequests int
|
||||
realServer bool
|
||||
logger *zap.Logger
|
||||
initOpts []frankenphp.Option
|
||||
}
|
||||
|
||||
func runTest(t *testing.T, test func(func(http.ResponseWriter, *http.Request), *httptest.Server, int), opts *testOptions) {
|
||||
@@ -39,10 +44,15 @@ func runTest(t *testing.T, test func(func(http.ResponseWriter, *http.Request), *
|
||||
cwd, _ := os.Getwd()
|
||||
testDataDir := cwd + "/testdata/"
|
||||
|
||||
initOpts := make([]frankenphp.Option, 0, 1)
|
||||
if opts.logger == nil {
|
||||
opts.logger = zaptest.NewLogger(t)
|
||||
}
|
||||
|
||||
initOpts := []frankenphp.Option{frankenphp.WithLogger(opts.logger)}
|
||||
if opts.workerScript != "" {
|
||||
initOpts = append(initOpts, frankenphp.WithWorkers(testDataDir+opts.workerScript, opts.nbWorkers))
|
||||
}
|
||||
initOpts = append(initOpts, opts.initOpts...)
|
||||
|
||||
err := frankenphp.Init(initOpts...)
|
||||
require.Nil(t, err)
|
||||
@@ -332,6 +342,32 @@ my_autoloader`, i), string(body))
|
||||
}, opts)
|
||||
}
|
||||
|
||||
func TestLog_module(t *testing.T) { testLog(t, &testOptions{}) }
|
||||
func TestLog_worker(t *testing.T) {
|
||||
testLog(t, &testOptions{workerScript: "log.php"})
|
||||
}
|
||||
func testLog(t *testing.T, opts *testOptions) {
|
||||
logger, logs := observer.New(zap.InfoLevel)
|
||||
opts.logger = zap.New(logger)
|
||||
|
||||
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
|
||||
req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/log.php?i=%d", i), nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler(w, req)
|
||||
|
||||
var found bool
|
||||
searched := fmt.Sprintf("request %d", i)
|
||||
for _, entry := range logs.All() {
|
||||
if entry.Message == searched {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
assert.True(t, found)
|
||||
}, opts)
|
||||
}
|
||||
|
||||
func ExampleExecuteScript() {
|
||||
if err := frankenphp.Init(); err != nil {
|
||||
panic(err)
|
||||
|
||||
5
go.mod
5
go.mod
@@ -9,12 +9,13 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/benbjohnson/clock v1.3.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/kr/pretty v0.2.1 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
8
go.sum
8
go.sum
@@ -1,3 +1,5 @@
|
||||
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
|
||||
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
@@ -15,20 +17,26 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
|
||||
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
|
||||
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
||||
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
|
||||
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 h1:dbuHpmKjkDzSOMKAWl10QNlgaZUd3V1q99xc81tt2Kc=
|
||||
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
7
testdata/log.php
vendored
Normal file
7
testdata/log.php
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__.'/_executor.php';
|
||||
|
||||
return function () {
|
||||
error_log("request {$_GET['i']}");
|
||||
};
|
||||
Reference in New Issue
Block a user