mirror of
https://github.com/dunglas/frankenphp.git
synced 2025-12-24 13:38:11 +08:00
As discussed in https://github.com/php/frankenphp/discussions/1961, there is no real way to pass a severity/level to any log handler offered by PHP that would make it to the FrankenPHP layer. This new function allows applications embedding FrankenPHP to integrate PHP logging into the application itself, thus offering a more streamlined experience. --------- Co-authored-by: Quentin Burgess <qutn.burgess@gmail.com> Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
38 lines
626 B
Go
38 lines
626 B
Go
package frankenphp_test
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log/slog"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func newTestLogger(t *testing.T) (*slog.Logger, fmt.Stringer) {
|
|
t.Helper()
|
|
|
|
var buf syncBuffer
|
|
|
|
return slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})), &buf
|
|
}
|
|
|
|
// SyncBuffer is a thread-safe buffer for capturing logs in tests.
|
|
type syncBuffer struct {
|
|
b bytes.Buffer
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func (s *syncBuffer) Write(p []byte) (n int, err error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
return s.b.Write(p)
|
|
}
|
|
|
|
func (s *syncBuffer) String() string {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.b.String()
|
|
}
|