log

package
v0.2.11 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 8 Imported by: 4

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) (*zap.Logger, bool)

FromContext returns the Logger value stored in Context.

func LatestLogger added in v0.2.11

func LatestLogger(ctx context.Context) *zap.Logger

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 Logger

func Logger(ctx context.Context) *zap.Logger

Logger return gapi global logger.

func NewContext

func NewContext(ctx context.Context, l *zap.Logger) context.Context

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

func NewRefContext(ctx context.Context, l *zap.Logger) context.Context

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

func Alert

func Alert(ctx context.Context) *Log

Alert logs an alert message with additional zap.Field

func Critical

func Critical(ctx context.Context) *Log

Critical logs a critical message with additional zap.Field

func Debug

func Debug(ctx context.Context) *Log

Debug logs a debug message with additional zap.Field

func Emergency

func Emergency(ctx context.Context) *Log

Emergency logs an emergency message with additional zap.Field

func Error

func Error(ctx context.Context) *Log

Error logs an error message with additional zap.Field

func Info

func Info(ctx context.Context) *Log

Info logs an info message with additional zap.Field

func Warn

func Warn(ctx context.Context) *Log

Warn logs a warning message with additional zap.Field

func (*Log) LogError added in v0.2.4

func (l *Log) LogError(err error)

LogError take a GAPI error, format error message and log it

func (*Log) LogMsg added in v0.2.4

func (l *Log) LogMsg(format string, a ...any)

LogMsg format and log a message

func (*Log) SetFunc added in v0.2.11

func (l *Log) SetFunc(f func(string, ...zap.Field))

SetFunc sets the log function for this Log instance. This is used when creating a Log from a specific logger (e.g. from LatestLogger).

func (*Log) With added in v0.2.4

func (l *Log) With(fields ...zap.Field) *Log

With returns a new Log with additional zap.Field

Jump to

Keyboard shortcuts

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