log

package
v0.15.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package log provides a global logger for zerolog.

Example

This example uses command-line flags to demonstrate various outputs depending on the chosen log level.

package main

import (
	"context"
	"flag"
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()
	debug := flag.Bool("debug", false, "sets log level to debug")

	flag.Parse()

	// Default level for this example is info, unless debug flag is present
	zerolog.SetGlobalLevel(zerolog.InfoLevel)
	if *debug {
		zerolog.SetGlobalLevel(zerolog.DebugLevel)
	}

	log.Debug(context.Background()).Msg("This message appears only when log level set to Debug")
	log.Info(context.Background()).Msg("This message appears when log level set to Debug or Info")

	if e := log.Debug(context.Background()); e.Enabled() {
		// Compute log output only if enabled.
		value := "bar"
		e.Str("foo", value).Msg("some debug message")
	}

}
Output:

{"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessHandler added in v0.0.5

func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next http.Handler) http.Handler

AccessHandler returns a handler that call f after each request.

func Ctx

func Ctx(ctx context.Context) *zerolog.Logger

Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.

func Debug

func Debug(ctx context.Context) *zerolog.Event

Debug starts a new message with debug level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "debug")

package main

import (
	"context"
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()
	log.Debug(context.Background()).Msg("hello world")

}
Output:

{"level":"debug","time":1199811905,"message":"hello world"}

func DisableDebug added in v0.11.0

func DisableDebug()

DisableDebug tells the logger to use stdout and json output.

func EnableDebug added in v0.11.0

func EnableDebug()

EnableDebug tells the logger to use stdout and pretty print output.

func Error

func Error(ctx context.Context) *zerolog.Event

Error starts a new message with error level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "error")

package main

import (
	"context"
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()
	log.Error(context.Background()).Msg("hello world")

}
Output:

{"level":"error","time":1199811905,"message":"hello world"}

func Fatal

func Fatal() *zerolog.Event

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.

Example

Example of a log at a particular "level" (in this case, "fatal")

package main

import (
	"errors"
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()
	err := errors.New("a repo man spends his life getting into tense situations")
	service := "myservice"

	log.Fatal().
		Err(err).
		Str("service", service).
		Msg("Cannot start")

	// Outputs: {"level":"fatal","time":1199811905,"error":"a repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"}
}
Output:

func FromRequest added in v0.0.2

func FromRequest(r *http.Request) *zerolog.Logger

FromRequest gets the logger in the request's context. This is a shortcut for log.Ctx(r.Context())

func HeadersHandler added in v0.5.1

func HeadersHandler(headers []string) func(next http.Handler) http.Handler

HeadersHandler adds the provided set of header keys to the log context.

https://tools.ietf.org/html/rfc7239 https://en.wikipedia.org/wiki/X-Forwarded-For https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For

func Info

func Info(ctx context.Context) *zerolog.Event

Info starts a new message with info level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "info")

package main

import (
	"context"
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()
	log.Info(context.Background()).Msg("hello world")

}
Output:

{"level":"info","time":1199811905,"message":"hello world"}

func Level

func Level(ctx context.Context, level zerolog.Level) *zerolog.Logger

Level creates a child logger with the minimum accepted level set to level.

func Log

func Log(ctx context.Context) *zerolog.Event

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.

Example

Example of a log with no particular "level"

package main

import (
	"context"
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()
	log.Log(context.Background()).Msg("hello world")

}
Output:

{"time":1199811905,"message":"hello world"}

func Logger

func Logger() *zerolog.Logger

Logger returns the global logger.

func MethodHandler added in v0.0.5

func MethodHandler(fieldKey string) func(next http.Handler) http.Handler

MethodHandler adds the request method as a field to the context's logger using fieldKey as field key.

func NewHandler added in v0.0.5

func NewHandler(getLogger func() *zerolog.Logger) func(http.Handler) http.Handler

NewHandler injects log into requests context.

func Panic

func Panic() *zerolog.Event

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 Print

func Print(v ...interface{})

Print sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Print.

Example

Simple logging example using the Print function in the log package Note that both Print and Printf are at the debug log level by default

package main

import (
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()

	log.Print("hello world")
}
Output:

{"level":"debug","time":1199811905,"message":"hello world"}

func Printf

func Printf(format string, v ...interface{})

Printf sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Printf.

Example

Simple logging example using the Printf function in the log package

package main

import (
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()

	log.Printf("hello %s", "world")
}
Output:

{"level":"debug","time":1199811905,"message":"hello world"}

func RefererHandler added in v0.0.5

func RefererHandler(fieldKey string) func(next http.Handler) http.Handler

RefererHandler adds the request's referer as a field to the context's logger using fieldKey as field key.

func RemoteAddrHandler added in v0.0.5

func RemoteAddrHandler(fieldKey string) func(next http.Handler) http.Handler

RemoteAddrHandler adds the request's remote address as a field to the context's logger using fieldKey as field key.

func RequestHandler added in v0.0.5

func RequestHandler(fieldKey string) func(next http.Handler) http.Handler

RequestHandler adds the request method and URL as a field to the context's logger using fieldKey as field key.

func RequestIDHandler added in v0.0.5

func RequestIDHandler(fieldKey string) func(next http.Handler) http.Handler

RequestIDHandler adds the request's id as a field to the context's logger using fieldKey as field key.

func SetLevel added in v0.0.3

func SetLevel(level string)

SetLevel sets the minimum global log level. Options are 'debug' 'info' 'warn' and 'error'. Defaults to 'debug'

Example
package main

import (
	"context"
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()
	log.SetLevel("info")
	log.Debug(context.Background()).Msg("Debug")
	log.Info(context.Background()).Msg("Debug or Info")
	log.SetLevel("warn")
	log.Debug(context.Background()).Msg("Debug")
	log.Info(context.Background()).Msg("Debug or Info")
	log.Warn(context.Background()).Msg("Debug or Info or Warn")
	log.SetLevel("error")
	log.Debug(context.Background()).Msg("Debug")
	log.Info(context.Background()).Msg("Debug or Info")
	log.Warn(context.Background()).Msg("Debug or Info or Warn")
	log.Error(context.Background()).Msg("Debug or Info or Warn or Error")
	log.SetLevel("default-fall-through")
	log.Debug(context.Background()).Msg("Debug")

}
Output:

{"level":"info","time":1199811905,"message":"Debug or Info"}
{"level":"warn","time":1199811905,"message":"Debug or Info or Warn"}
{"level":"error","time":1199811905,"message":"Debug or Info or Warn or Error"}
{"level":"debug","time":1199811905,"message":"Debug"}

func SetLogger added in v0.11.0

func SetLogger(l *zerolog.Logger)

SetLogger sets zerolog the logger.

func URLHandler added in v0.0.5

func URLHandler(fieldKey string) func(next http.Handler) http.Handler

URLHandler adds the requested URL as a field to the context's logger using fieldKey as field key.

func UserAgentHandler added in v0.0.5

func UserAgentHandler(fieldKey string) func(next http.Handler) http.Handler

UserAgentHandler adds the request's user-agent as a field to the context's logger using fieldKey as field key.

func Warn

func Warn(ctx context.Context) *zerolog.Event

Warn starts a new message with warn level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "warn")

package main

import (
	"context"
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()
	log.Warn(context.Background()).Msg("hello world")

}
Output:

{"level":"warn","time":1199811905,"message":"hello world"}

func With

func With() zerolog.Context

With creates a child logger with the field added to its context.

Example
package main

import (
	"time"

	"github.com/rs/zerolog"

	"github.com/pomerium/pomerium/internal/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC)
	}
	log.DisableDebug()
}

func main() {
	setup()
	sublog := log.With().Str("foo", "bar").Logger()
	sublog.Debug().Msg("hello world")
}
Output:

{"level":"debug","foo":"bar","time":1199811905,"message":"hello world"}

func WithContext added in v0.14.0

func WithContext(ctx context.Context, update func(c zerolog.Context) zerolog.Context) context.Context

WithContext returns a context that has an associated logger and extra fields set via update

func ZapLogger added in v0.11.0

func ZapLogger() *zap.Logger

ZapLogger returns the global zap logger.

Types

type StdLogWrapper added in v0.0.3

type StdLogWrapper struct {
	*zerolog.Logger
}

StdLogWrapper can be used to wrap logs originating from the from std library's ErrorFunction argument in http.Serve and httputil.ReverseProxy.

func (*StdLogWrapper) Write added in v0.0.3

func (l *StdLogWrapper) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL