log

package
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 7 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 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.

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) 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