log

package module
v0.0.0-...-958ca12 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2023 License: GPL-3.0 Imports: 7 Imported by: 0

README

🪵

github.com/thecodinglab/log is another structured logging library for Golang. It has been inspired by some other awesome logging libraries such as logrus and zap.

I started with this library to combine my personal logging stack into a single library which I can simply import in all of my projects. I am still heavily working on this, so I don't recommend using it in production. However, if you have any feedback or suggestions, feel free to open an issue or pull request.

Usage

go get -u github.com/thecodinglab/log

The logging API uses the golang context package to pass instances of the log.Logger interface through the application. By doing this, each module can append additional meta information to the logger without having to pass a logging instance to every sub-service it uses.

  • Application → initialize logging
    • RESTful API Service → attach HTTP client information
      • Database → attach query statement
      • ...
    • ...
package main

import (
	"context"
	"net"
	"net/http"
	"os"

	"github.com/thecodinglab/log"
	"github.com/thecodinglab/log/level"
	"github.com/thecodinglab/log/capture"
	"github.com/thecodinglab/log/sinks/file"
)

func main() {
	// initialize logger to print to stdout
	logger := log.New(
		file.New(os.Stdout, file.TextFormatter{}, level.Info),
	)
	defer logger.Sync()

	// attach the logger to the global application context
	ctx := log.Attach(context.Background(), logger)

	router := http.NewServeMux()

	server := &http.Server{
		Addr:    "0.0.0.0:8080",
		Handler: loggingMiddleware(router),

		// use the previously created application context as base context for each request
		BaseContext: func(listener net.Listener) context.Context {
			return ctx
		},
	}

	if err := server.ListenAndServe(); err != nil {
		capture.Error(ctx, err)
	}
}

func loggingMiddleware(base http.Handler) http.Handler {
	return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		// attach client information to the logger
		logger := log.With(req.Context(),
			"client", req.RemoteAddr,
			"request_method", req.Method,
			"request_url", req.URL.String(),
		)

		logger.Debug("client ", req.RemoteAddr, " requested resource ", req.URL)

		// pass client information down to the next handler
		ctx := log.Attach(req.Context(), logger)
		base.ServeHTTP(res, req.WithContext(ctx))
	})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attach

func Attach(parent context.Context, logger Logger) context.Context

func Debug

func Debug(ctx context.Context, args ...interface{})

func Debugf

func Debugf(ctx context.Context, format string, args ...interface{})

func Error

func Error(ctx context.Context, args ...interface{})

func Errorf

func Errorf(ctx context.Context, format string, args ...interface{})

func Info

func Info(ctx context.Context, args ...interface{})

func Infof

func Infof(ctx context.Context, format string, args ...interface{})

func SetDefault

func SetDefault(logger Logger)

func Sync

func Sync(ctx context.Context)

func Warn

func Warn(ctx context.Context, args ...interface{})

func Warnf

func Warnf(ctx context.Context, format string, args ...interface{})

Types

type Entry

type Entry struct {
	Level   level.Level `json:"level"`
	Time    time.Time   `json:"time"`
	Message string      `json:"message"`
	Caller  string      `json:"caller"`
	Fields  fields.KV   `json:"fields,omitempty"`
}

type LevelLogger

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

func Level

func Level(ctx context.Context, lvl level.Level) LevelLogger

func (LevelLogger) Print

func (l LevelLogger) Print(args ...interface{})

func (LevelLogger) Printf

func (l LevelLogger) Printf(format string, args ...interface{})

func (LevelLogger) With

func (l LevelLogger) With(kv ...interface{}) LevelLogger

type Logger

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

func Default

func Default() Logger

func Get

func Get(ctx context.Context) Logger

func New

func New(sink Sink) Logger

func With

func With(ctx context.Context, kv ...interface{}) Logger

func (Logger) Debug

func (l Logger) Debug(args ...interface{})

func (Logger) Debugf

func (l Logger) Debugf(format string, args ...interface{})

func (Logger) Error

func (l Logger) Error(args ...interface{})

func (Logger) Errorf

func (l Logger) Errorf(format string, args ...interface{})

func (Logger) Info

func (l Logger) Info(args ...interface{})

func (Logger) Infof

func (l Logger) Infof(format string, args ...interface{})

func (Logger) IsValid

func (l Logger) IsValid() bool

func (Logger) Level

func (l Logger) Level(lvl level.Level) LevelLogger

func (Logger) Sync

func (l Logger) Sync()

func (Logger) Warn

func (l Logger) Warn(args ...interface{})

func (Logger) Warnf

func (l Logger) Warnf(format string, args ...interface{})

func (Logger) With

func (l Logger) With(kv ...interface{}) Logger

type Sink

type Sink interface {
	Write(entry *Entry)
	Sync()
}

Directories

Path Synopsis
sinks

Jump to

Keyboard shortcuts

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