mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-30 10:56:28 +02:00
log context custom writer
This commit is contained in:
parent
d32b8a4d8a
commit
6af7b9a1cd
1 changed files with 11 additions and 1 deletions
|
@ -3,6 +3,7 @@ package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
@ -12,10 +13,15 @@ import (
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type outputContextKey struct{}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logger atomic.Value
|
logger atomic.Value
|
||||||
zapLogger atomic.Value
|
zapLogger atomic.Value
|
||||||
zapLevel zap.AtomicLevel
|
zapLevel zap.AtomicLevel
|
||||||
|
|
||||||
|
// Output marks context.Context to use designated output
|
||||||
|
Output outputContextKey
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -123,6 +129,10 @@ func contextLogger(ctx context.Context) *zerolog.Logger {
|
||||||
if l.GetLevel() == zerolog.Disabled { // no logger associated with context
|
if l.GetLevel() == zerolog.Disabled { // no logger associated with context
|
||||||
return global
|
return global
|
||||||
}
|
}
|
||||||
|
if out, ok := ctx.Value(Output).(io.Writer); ok && out != nil {
|
||||||
|
outLogger := l.Output(out)
|
||||||
|
return &outLogger
|
||||||
|
}
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +147,7 @@ func WithContext(ctx context.Context, update func(c zerolog.Context) zerolog.Con
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
func Error(ctx context.Context) *zerolog.Event {
|
func Error(ctx context.Context) *zerolog.Event {
|
||||||
return Logger().Error()
|
return contextLogger(ctx).Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal starts a new message with fatal level. The os.Exit(1) function
|
// Fatal starts a new message with fatal level. The os.Exit(1) function
|
||||||
|
|
Loading…
Add table
Reference in a new issue