Files
frankenphp/log_test.go
Raphael Coeffic 91c553f3d9 feat: add support for structured logging with the frankenphp_log() PHP function (#1979)
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>
2025-12-15 16:10:35 +01:00

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()
}