ilog

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: Apache-2.0 Imports: 13 Imported by: 2

README

ilog.go

license pkg goreportcard workflow workflow codecov sourcegraph

ilog.go is a simple logging interface library for Go. By defining only the logger interface ilog.Logger, users can easily swap out the underlying logging implementation without changing their application code.

Features

  • Flexibility: Allows users to choose or switch between different logging implementations.
  • Simplicity: Only defines the logging interface, leaving the choice of logging implementation to the user.

Interface

ilog.Logger interface is defined here:

Reference Implementations

We provide reference implementations for a custom logger and some of the popular logging libraries:

Usage

First, go get ilog.go in your Go application:

go get -u github.com/kunitsucom/ilog.go

Then, define a variable of the ilog.Logger interface type and initialize it with your chosen logger implementation.

For example, if using the default implementation:

import (
    "github.com/kunitsucom/ilog.go"
)

func main() {
    l := ilog.NewBuilder(ilog.DebugLevel, os.Stdout).Build()
}

if zap:

go get -u github.com/kunitsucom/ilog.go/implementations/zap
import (
    "github.com/kunitsucom/ilog.go"
    "go.uber.org/zap"
    ilogzap "github.com/kunitsucom/ilog.go/implementations/zap"
)

func main() {
    l := ilogzap.New(ilog.DebugLevel, zap.NewProduction())
}

if zerolog:

go get -u github.com/kunitsucom/ilog.go/implementations/zerolog
import (
    "github.com/kunitsucom/ilog.go"
    ilogzerolog "github.com/kunitsucom/ilog.go/implementations/zerolog"
    "github.com/rs/zerolog"
)

func main() {
    l := ilogzerolog.New(ilog.DebugLevel, zerolog.New(os.Stdout))
}

Now, you can use the l as ilog.Logger for logging in your application:

func main() {
    l := ... // your chosen logger implementation

    l.String("key", "value").Infof("This is an info message")
    l.Err(err).Errorf("This is an error message")
}

If you wish to switch to another logger, simply change the initialization of the l variable.

Implementing a Custom Logger

If the provided reference implementations do not meet your requirements, you can easily implement the Logger interface with your desired logging package. Ensure that your custom logger adheres to the methods defined in the ilog.go interface.

License

here.

Documentation

Overview

ilog is a simple logging interface library package for Go. By defining only the logger interface `ilog.Logger`, users can easily swap out the underlying logging implementation without changing their application code.

Index

Constants

This section is empty.

Variables

View Source
var ErrLogEntryIsNotWritten = errors.New("ilog: log entry not written")

ErrLogEntryIsNotWritten is the error returned when a log entry is not written.

Functions

func NewBuilder

func NewBuilder(level Level, w io.Writer) implLoggerConfig

NewBuilder returns a new Builder of ilog.Logger with the specified level and writer.

func SetGlobal

func SetGlobal(logger Logger) (rollback func())

func SetStdLogger

func SetStdLogger(l Logger) (rollback func())

func WithContext

func WithContext(ctx context.Context, logger Logger) context.Context

Types

type Level

type Level int8

Level is the type for logging level.

const (
	DebugLevel Level = -8
	InfoLevel  Level = 0
	WarnLevel  Level = 8
	ErrorLevel Level = 16
)

type LogEntry

type LogEntry interface {

	// Logger returns a new logger with the same fields of the log entry.
	Logger() (copied Logger)

	// error: for considering undispatched LogEntry as error so that they can be detected by Go static analysis.
	error
	// contains filtered or unexported methods
}

LogEntry is the interface that has the logging methods for a single log entry.

type Logger

type Logger interface {
	// Level returns the current logging level of the logger.
	Level() (currentLoggerLevel Level)
	// SetLevel sets the logging level of the logger.
	SetLevel(level Level) (copied Logger)
	// AddCallerSkip adds the number of stack frames to skip to the logger.
	AddCallerSkip(skip int) (copied Logger)
	// Copy returns a copy of the logger.
	Copy() (copied Logger)
	// contains filtered or unexported methods
}

Logger is the interface that has the basic logging methods.

func FromContext

func FromContext(ctx context.Context) (logger Logger)

func Global

func Global() Logger

func L

func L() Logger

Directories

Path Synopsis
implementations
zap Module
zerolog Module

Jump to

Keyboard shortcuts

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