Documentation
¶
Overview ¶
Package log
Gapi logging is based on zap.Logger and use its own global logger system to print logs. This global logger can be overridden with your own zap.Logger
## Usage
### Public methods
Gapi logging expose a list of public methods to get logger with various logging levels: - log.Debug(ctx context.Context) - log.Info(ctx context.Context) - log.Warn(ctx context.Context) - log.Error(ctx context.Context) - log.Critical(ctx context.Context) - log.Alert(ctx context.Context) - log.Emergency(ctx context.Context)
import (
"github.com/mwm-io/gapi/handler"
gLog "github.com/mwm-io/gapi/log"
"github.com/mwm-io/gapi/server"
)
func main() {
r := server.NewMux()
server.AddHandler(r, "GET", "/", handler.Func(HelloWorldHandler))
gLog.Info(context.Background()).LogMsg("Starting http server")
if err := server.ServeAndHandleShutdown(r); err != nil {
gLog.Error(context.Background()).LogError(err)
}
gLog.Info(context.Background()).LogMsg("Server stopped")
}
### Global instance
Logger instance can be retrieved using log.Logger().
log := log.Logger()
log.Info("my log")
As mention earlier, you can override gapi logger with log.SetLogger() by passing your custom zap.Logger instance.
myLogger := zap.NewProduction()
log.SetLogger(myLogger)
log.Log("my log")
### Context
Context can also be used to store/get logger: - log.FromContext(ctx) [retrieve gapi logger from context] - log.NewContext(ctx, logger) [store given zap.Logger into context]
Index ¶
- func FromContext(ctx context.Context) (*zap.Logger, bool)
- func LatestLogger(ctx context.Context) *zap.Logger
- func Logger(ctx context.Context) *zap.Logger
- func NewContext(ctx context.Context, l *zap.Logger) context.Context
- func NewRefContext(ctx context.Context, l *zap.Logger) context.Context
- type Log
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶
FromContext returns the Logger value stored in Context.
func LatestLogger ¶ added in v0.2.11
LatestLogger returns the logger from the mutable ref if available, otherwise falls back to the standard context logger. This is used by the Log middleware to get the logger enriched by inner middlewares.
func NewContext ¶
NewContext returns a new Context that carries value Logger. If a logger ref exists in the context (created by NewRefContext), it also updates the ref so that the Log middleware can retrieve the latest enriched logger.
func NewRefContext ¶ added in v0.2.11
NewRefContext creates a mutable logger reference in the context. This should be called by the Log middleware before calling NewContext, so that inner middlewares' calls to NewContext automatically update the ref.
Types ¶
type Log ¶
type Log struct {
// contains filtered or unexported fields
}
Log is a simple client to improve the usability of the zap logger using GAPI