mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-02 11:56:02 +02:00
- POMERIUM_DEBUG no longer sets log level. - LOG_LEVEL now responsible for setting global log level.
142 lines
3.8 KiB
Go
142 lines
3.8 KiB
Go
// Package log provides a global logger for zerolog.
|
|
package log // import "github.com/pomerium/pomerium/internal/log"
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
// Logger is the global logger.
|
|
var Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
|
|
|
|
// SetDebugMode tells the logger to use standard out and pretty print output.
|
|
func SetDebugMode() {
|
|
Logger = Logger.Output(zerolog.ConsoleWriter{Out: os.Stdout})
|
|
}
|
|
|
|
// SetLevel sets the minimum global log level. Options are 'debu' 'info' 'warn' and 'error'.
|
|
// Defaults to 'debug'
|
|
func SetLevel(level string) {
|
|
switch level {
|
|
case "info":
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
case "warn":
|
|
zerolog.SetGlobalLevel(zerolog.WarnLevel)
|
|
case "error":
|
|
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
|
|
default:
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
}
|
|
}
|
|
|
|
// With creates a child logger with the field added to its context.
|
|
func With() zerolog.Context {
|
|
return Logger.With()
|
|
}
|
|
|
|
// Level creates a child logger with the minimum accepted level set to level.
|
|
func Level(level zerolog.Level) zerolog.Logger {
|
|
return Logger.Level(level)
|
|
}
|
|
|
|
// Debug starts a new message with debug level.
|
|
//
|
|
// You must call Msg on the returned event in order to send the event.
|
|
func Debug() *zerolog.Event {
|
|
return Logger.Debug()
|
|
}
|
|
|
|
// Info starts a new message with info level.
|
|
//
|
|
// You must call Msg on the returned event in order to send the event.
|
|
func Info() *zerolog.Event {
|
|
return Logger.Info()
|
|
}
|
|
|
|
// Warn starts a new message with warn level.
|
|
//
|
|
// You must call Msg on the returned event in order to send the event.
|
|
func Warn() *zerolog.Event {
|
|
return Logger.Warn()
|
|
}
|
|
|
|
// Error starts a new message with error level.
|
|
//
|
|
// You must call Msg on the returned event in order to send the event.
|
|
func Error() *zerolog.Event {
|
|
return Logger.Error()
|
|
}
|
|
|
|
// Fatal starts a new message with fatal level. The os.Exit(1) function
|
|
// is called by the Msg method.
|
|
//
|
|
// You must call Msg on the returned event in order to send the event.
|
|
func Fatal() *zerolog.Event {
|
|
return Logger.Fatal()
|
|
}
|
|
|
|
// Panic starts a new message with panic level. The message is also sent
|
|
// to the panic function.
|
|
//
|
|
// You must call Msg on the returned event in order to send the event.
|
|
func Panic() *zerolog.Event {
|
|
return Logger.Panic()
|
|
}
|
|
|
|
// WithLevel starts a new message with level.
|
|
//
|
|
// You must call Msg on the returned event in order to send the event.
|
|
func WithLevel(level zerolog.Level) *zerolog.Event {
|
|
return Logger.WithLevel(level)
|
|
}
|
|
|
|
// Log starts a new message with no level. Setting zerolog.GlobalLevel to
|
|
// zerolog.Disabled will still disable events produced by this method.
|
|
//
|
|
// You must call Msg on the returned event in order to send the event.
|
|
func Log() *zerolog.Event {
|
|
return Logger.Log()
|
|
}
|
|
|
|
// Print sends a log event using debug level and no extra field.
|
|
// Arguments are handled in the manner of fmt.Print.
|
|
func Print(v ...interface{}) {
|
|
Logger.Print(v...)
|
|
}
|
|
|
|
// Printf sends a log event using debug level and no extra field.
|
|
// Arguments are handled in the manner of fmt.Printf.
|
|
func Printf(format string, v ...interface{}) {
|
|
Logger.Printf(format, v...)
|
|
}
|
|
|
|
// Ctx returns the Logger associated with the ctx. If no logger
|
|
// is associated, a disabled logger is returned.
|
|
func Ctx(ctx context.Context) *zerolog.Logger {
|
|
return zerolog.Ctx(ctx)
|
|
}
|
|
|
|
// FromRequest gets the logger in the request's context.
|
|
// This is a shortcut for log.Ctx(r.Context())
|
|
func FromRequest(r *http.Request) *zerolog.Logger {
|
|
return Ctx(r.Context())
|
|
}
|
|
|
|
// StdLogWrapper can be used to wrap logs originating from the from std
|
|
// library's ErrorFunction argument in http.Serve and httputil.ReverseProxy.
|
|
type StdLogWrapper struct {
|
|
*zerolog.Logger
|
|
}
|
|
|
|
func (l *StdLogWrapper) Write(p []byte) (n int, err error) {
|
|
n = len(p)
|
|
if n > 0 && p[n-1] == '\n' {
|
|
// Trim CR added by stdlog.
|
|
p = p[0 : n-1]
|
|
}
|
|
l.Error().Msg(string(p))
|
|
return len(p), nil
|
|
}
|