Documentation
¶
Overview ¶
Package fxlogging provides a convenient way to create loggers.
Example ¶
package main
import (
"time"
sconfig "github.com/exoscale/stelling/config"
"github.com/exoscale/stelling/fxlogging"
"github.com/exoscale/stelling/fxsentry"
"go.uber.org/fx"
"go.uber.org/zap"
)
type Config struct {
fxlogging.Logging
fxsentry.Sentry
APIKey sconfig.Secret
}
func main() {
conf := &Config{}
args := []string{"logging-test", "--logging.mode", "production", "--api-key", "my-key"}
if err := sconfig.Load(conf, args); err != nil {
panic(err)
}
app := fx.New(fx.Options(
fxlogging.NewModule(
conf,
// these options make the logs determistic so we can test the output
// Normal programs will 90% of the time only need the standard module
// It does however demonstrate how additional zap options can be injected
fxlogging.WithZapOption(zap.WithCaller(false)),
fxlogging.WithZapOption(zap.WithClock(&fixedClock{ts: 1257894000})),
),
fx.Invoke(run),
))
app.Run()
}
func run(sd fx.Shutdowner, logger *zap.Logger) {
logger.Info("Example log")
sd.Shutdown() //nolint:errcheck
}
type fixedClock struct {
ts int64
}
func (c *fixedClock) Now() time.Time {
return time.Unix(c.ts, 0).UTC()
}
func (c *fixedClock) NewTicker(d time.Duration) *time.Ticker {
return time.NewTicker(d)
}
Output: {"level":"info","ts":"2009-11-10T23:00:00.000Z","msg":"Using configuration","conf":{"Mode":"production","Dsn":"","Environment":"prod","Debug":false,"Process":"","APIKey":"*****"}} {"level":"info","ts":"2009-11-10T23:00:00.000Z","msg":"Example log"} {"level":"info","ts":"2009-11-10T23:00:00.000Z","msg":"Final configuration","conf":{"Mode":"production","Dsn":"","Environment":"prod","Debug":false,"Process":"","APIKey":"*****"}}
Index ¶
- Constants
- func ISO8601UTCTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)
- func NewGrpcInjectLoggerInterceptors(logger *zap.Logger, opts ...interceptor.Option) (*fxgrpc.UnaryServerInterceptor, *fxgrpc.StreamServerInterceptor)
- func NewGrpcInjectPeerInterceptors() (*fxgrpc.UnaryClientInterceptor, *fxgrpc.StreamClientInterceptor)
- func NewGrpcLoggingClientInterceptors(logger *zap.Logger, opts ...interceptor.Option) (*fxgrpc.UnaryClientInterceptor, *fxgrpc.StreamClientInterceptor)
- func NewGrpcLoggingServerInterceptors(logger *zap.Logger, opts ...interceptor.Option) (*fxgrpc.UnaryServerInterceptor, *fxgrpc.StreamServerInterceptor)
- func NewLogger(conf LoggingConfig, lc fx.Lifecycle, opts ...zap.Option) (*zap.Logger, error)
- func NewModule(conf LoggingConfig, opts ...Option) fx.Option
- type HttpMiddlewareResult
- type Logging
- type LoggingConfig
- type Option
Examples ¶
Constants ¶
const GrpcInterceptorWeight uint = 50
Variables ¶
This section is empty.
Functions ¶
func ISO8601UTCTimeEncoder ¶
func ISO8601UTCTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)
ISO8601UTCTimeEncoder is like zapcore.ISO8601TimeEncoder but sets the timezone to utc first
func NewGrpcInjectLoggerInterceptors ¶
func NewGrpcInjectLoggerInterceptors(logger *zap.Logger, opts ...interceptor.Option) (*fxgrpc.UnaryServerInterceptor, *fxgrpc.StreamServerInterceptor)
func NewGrpcInjectPeerInterceptors ¶
func NewGrpcInjectPeerInterceptors() (*fxgrpc.UnaryClientInterceptor, *fxgrpc.StreamClientInterceptor)
func NewGrpcLoggingClientInterceptors ¶
func NewGrpcLoggingClientInterceptors(logger *zap.Logger, opts ...interceptor.Option) (*fxgrpc.UnaryClientInterceptor, *fxgrpc.StreamClientInterceptor)
func NewGrpcLoggingServerInterceptors ¶
func NewGrpcLoggingServerInterceptors(logger *zap.Logger, opts ...interceptor.Option) (*fxgrpc.UnaryServerInterceptor, *fxgrpc.StreamServerInterceptor)
Types ¶
type HttpMiddlewareResult ¶
type HttpMiddlewareResult struct {
fx.Out
Middleware *fxhttp.Middleware `group:"http_middleware"`
}
func NewHttpMiddleware ¶
func NewHttpMiddleware(logger *zap.Logger) HttpMiddlewareResult
type Logging ¶
type Logging struct {
// LogMode is the preset logging configuration
Mode string `default:"development" validate:"oneof=production development preproduction"`
}
Logging contains the configuration options for the logging module
func (*Logging) LoggingConfig ¶
func (*Logging) MarshalLogObject ¶
func (l *Logging) MarshalLogObject(enc zapcore.ObjectEncoder) error
type LoggingConfig ¶
type LoggingConfig interface {
LoggingConfig() *Logging
}
type Option ¶
type Option func(*moduleConfig)
func WithFxLoggerOption ¶
WithFxLoggerOption will apply the fxlogger Option to the logger used by the fx machinery Can be given multiple times to supply different options
func WithGrpcClientInterceptorOptions ¶
func WithGrpcClientInterceptorOptions(opt interceptor.Option) Option
func WithGrpcClientInterceptors ¶
WithGrpcClientInterceptors determines if Grpc Client interceptors can be provided by the system This is true by default, but is undesirable in high volume use cases If you app does not use gRPC you can ignore this: fx provides constructors lazily
func WithGrpcServerInterceptorOptions ¶
func WithGrpcServerInterceptorOptions(opt interceptor.Option) Option
func WithZapOption ¶
WithZapOption will apply the given zap.Option to the logger provided by the module Can be given multiple times to supply different options
Directories
¶
| Path | Synopsis |
|---|---|
|
Package grpc provides a logger that is compatible with grpclogV2
|
Package grpc provides a logger that is compatible with grpclogV2 |