log

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 4 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 print logs for various logging levels: - log.Debug(msg string, fields ...zap.Field) - log.Info(msg string, fields ...zap.Field) - log.Warn(msg string, fields ...zap.Field) - log.Error(msg string, fields ...zap.Field) - log.Critical(msg string, fields ...zap.Field) - log.Alert(msg string, fields ...zap.Field) - log.Emergency(msg string, fields ...zap.Field)

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("Starting http server")
	if err := server.ServeAndHandleShutdown(r); err != nil {
		gLog.Error(err)
	}

	gLog.Info("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 Alert

func Alert(ctx context.Context, msg string, fields ...zap.Field)

Alert logs an alert message with additional zap.Field

func Critical

func Critical(ctx context.Context, msg string, fields ...zap.Field)

Critical logs a critical message with additional zap.Field

func Debug

func Debug(ctx context.Context, msg string, fields ...zap.Field)

Debug logs a debug message with additional zap.Field

func Emergency

func Emergency(ctx context.Context, msg string, fields ...zap.Field)

Emergency logs an emergency message with additional zap.Field

func Error

func Error(ctx context.Context, msg string, fields ...zap.Field)

Error logs an error message with additional zap.Field

func FromContext

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

FromContext returns the Logger value stored in Context.

func Info

func Info(ctx context.Context, msg string, fields ...zap.Field)

Info logs an info message with additional zap.Field

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.

func Warn

func Warn(ctx context.Context, msg string, fields ...zap.Field)

Warn logs a warning message with additional zap.Field

Types

This section is empty.

Jump to

Keyboard shortcuts

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