logger

package
v0.0.0-...-4fe6537 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: MIT Imports: 15 Imported by: 0

README

Summary

This code defines a function called NewLogger that creates a new logger based on the provided configuration. The logger uses the Zap logging library and supports log rotation using the Lumberjack library.

Example Usage

config := NewConfig()
logger := NewLogger(config)
logger.Info("This is an info log")
logger.Error("This is an error log")

Code Analysis

Inputs

  • config (Iconfigs): The configuration object that specifies the logger settings.

Flow

  1. The function first creates an encoder configuration for the logger. If the configuration is for production, it uses the production encoder configuration, otherwise it uses the development encoder configuration.
  2. The function sets the time key and time encoder for the encoder configuration.
  3. The function calls the genarateLogRotater function to create a log rotater based on the provided configuration.
  4. The function creates a new Zap core using the JSON encoder, the log rotater, and the atomic log level.
  5. The function creates a new logger using the created core and returns it.

Outputs

  • ILogger: The created logger object that can be used to log messages.

Documentation

Overview

Package ginzap provides log handling using zap package. Code structure based on ginrus package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomRecoveryWithZap

func CustomRecoveryWithZap(logger ZapLogger, stack bool, recovery gin.RecoveryFunc) gin.HandlerFunc

CustomRecoveryWithZap returns a gin.HandlerFunc (middleware) with a custom recovery handler that recovers from any panics and logs requests using uber-go/zap. All errors are logged using zap.Error(). stack means whether output the stack info. The stack info is easy to find where the error occurs but the stack info is too large.

func Ginzap

func Ginzap(logger ILogger, timeFormat string, utc bool) http.HandlerFunc

Ginzap returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap.

Requests with errors are logged using zap.Error(). Requests without errors are logged using zap.Info().

It receives:

  1. A time package format string (e.g. time.RFC3339).
  2. A boolean stating whether to use UTC time zone or local.

func GinzapWithConfig

func GinzapWithConfig(logger ILogger, conf *Config) http.HandlerFunc

GinzapWithConfig returns a gin.HandlerFunc using configs

func RecoveryWithZap

func RecoveryWithZap(logger ZapLogger, stack bool) gin.HandlerFunc

RecoveryWithZap returns a gin.HandlerFunc (middleware) that recovers from any panics and logs requests using uber-go/zap. All errors are logged using zap.Error(). stack means whether output the stack info. The stack info is easy to find where the error occurs but the stack info is too large.

Types

type Config

type Config struct {
	TimeFormat   string
	UTC          bool
	SkipPaths    []string
	Context      Fn
	DefaultLevel zapcore.Level
}

Config is config setting for Ginzap

type Fn

type Fn func(response http.ResponseWriter, request *http.Request) []zapcore.Field

type IConfigOptionsFunction

type IConfigOptionsFunction func(*configs)

type ILogFieldInput

type ILogFieldInput interface {
	GetFieldName() string
	GetField() interface{}
}

func NewLogFieldInput

func NewLogFieldInput(fieldName string, fields interface{}) ILogFieldInput

type ILogger

type ILogger interface {
	Sync()
	Debug(ctx context.Context, message string, fieldData ...ILogFieldInput)
	Info(ctx context.Context, message string, fieldData ...ILogFieldInput)
	Warn(ctx context.Context, message string, fieldData ...ILogFieldInput)
	Error(ctx context.Context, message string, fieldData ...ILogFieldInput)
	GetIoWriter() io.Writer
	RequestLog(request *http.Request)
	GetGrpcUnaryInterceptor() grpc.UnaryServerInterceptor
}

func NewLogger

func NewLogger(config Iconfigs) ILogger

type Iconfigs

type Iconfigs interface {
	WithFilename(filename string)
	WithMaxBackUp(maxBackUp int)
	WithMaxAge(maxAge int)
	WithIsLocalTime(isLocalTime bool)
	WithIsCompressed(isCompressed bool)
	IsProduction() bool
	FileName() string
	MaxAge() int
	MaxSize() int
	MaxBackups() int
	IslocalTime() bool
	IsCompressed() bool
}

func NewlogConfigOptions

func NewlogConfigOptions(isProduction bool, opts ...IConfigOptionsFunction) Iconfigs

type LogFieldInput

type LogFieldInput struct {
	// contains filtered or unexported fields
}

func (LogFieldInput) GetField

func (i LogFieldInput) GetField() interface{}

func (LogFieldInput) GetFieldName

func (i LogFieldInput) GetFieldName() string

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, message string, fieldData ...ILogFieldInput)

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, message string, fieldData ...ILogFieldInput)

func (*Logger) GetGrpcUnaryInterceptor

func (l *Logger) GetGrpcUnaryInterceptor() grpc.UnaryServerInterceptor

grpc logger

func (*Logger) GetIoWriter

func (l *Logger) GetIoWriter() io.Writer

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, message string, fieldData ...ILogFieldInput)

func (*Logger) RequestLog

func (l *Logger) RequestLog(request *http.Request)

func (*Logger) Sync

func (l *Logger) Sync()

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, message string, fieldData ...ILogFieldInput)

type ZapLogger

type ZapLogger interface {
	Info(msg string, fields ...zap.Field)
	Error(msg string, fields ...zap.Field)
}

ZapLogger is the minimal logger interface compatible with zap.Logger

Jump to

Keyboard shortcuts

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