ce

package module
v0.0.0-...-e43077a Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 16 Imported by: 0

README

ce

Simple wrapper for standard library log/slog

usage
package main

import (
	"github.com/themoment198/ce"
	"gopkg.in/natefinch/lumberjack.v2"
	"io"
	"log/slog"
	"os"
	"path/filepath"
)

func main() {
	log := slog.New(
		slog.NewJSONHandler(
			io.MultiWriter(
				os.Stderr,
				&lumberjack.Logger{
					Filename:   "/tmp/1/foobar.txt",
					MaxSize:    1, // mb
					MaxAge:     7, // days
					MaxBackups: 0, // count of log file, 0 means default(retain all old log files)
					LocalTime:  false,
					Compress:   false,
				},
			),

			&slog.HandlerOptions{
				AddSource: true,
				Level:     &ce.DefaultLevel,
				ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
					if a.Key == slog.SourceKey {
						source := a.Value.Any().(*slog.Source)
						source.File = filepath.Base(source.File)
					}
					return a
				},
			},
		),
	)

	ce.SetDefault(log)

	// case 1
	func() {
		ce.Debug("test1", slog.String("k1", "v1"), slog.Int("k2", 2))
		ce.Info("test2")
		ce.Warn("test3")
		ce.Error("test4")
		ce.Print("msg5", "msg6", "msg7", "msg8")
		ce.Printf("%s - %s", "msg9", "msg10")
		ce.DefaultLevel.Set(slog.LevelInfo)
		ce.Debug("test7")
	}()

	// case 2
	func() {
		defer ce.Recover(false)
		ce.CheckError(io.EOF, "k1", "v1")
	}()

	// case 3
	func() {
		defer ce.Recover(true)
		ce.CheckError(io.EOF, "k1", "v1")
	}()

	// case 4
	func() {
		defer ce.Recover(false)
		panic(io.EOF)
	}()

	// case 5
	func() {
		defer ce.Recover(true)
		panic(io.EOF)
	}()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLevel slog.LevelVar
View Source
var Notify = func(callbacks ...func()) {
	notifyObjChan := make(chan os.Signal, 1)
	signal.Notify(notifyObjChan, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGHUP)

forByNotify:
	for {
		s := <-notifyObjChan
		Info("notify", slog.Any("notifyObj", s))
		switch s {
		case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM:
			break forByNotify

		case syscall.SIGHUP:
		default:
			break forByNotify
		}
	}

	for _, callback := range callbacks {
		callback()
	}
}
View Source
var Recover = func(showStack bool, defers ...func(recoverObj interface{})) {
	if recoverObj := recover(); recoverObj != nil {
		_, ok := recoverObj.(*panicByCheckError)
		if ok == false {
			if showStack {
				callStackBin := debug.Stack()
				Default().log(nil, slog.LevelError, 4, "recover", "recoverObj", recoverObj, "callStack", *(*string)(unsafe.Pointer(&callStackBin)))
			} else {
				Default().log(nil, slog.LevelError, 4, "recover", "recoverObj", recoverObj)
			}
		}

		for _, v := range defers {
			v(recoverObj)
		}
	}
}

Functions

func CheckError

func CheckError(err error, args ...any)

func Debug

func Debug(msg string, args ...any)

func Default

func Default() *slogWrapper

func Error

func Error(msg string, args ...any)

func Info

func Info(msg string, args ...any)

func OpenPProf

func OpenPProf(addr string)

func Print

func Print(args ...any)

func Printf

func Printf(format string, args ...any)

func SetDefault

func SetDefault(l *slog.Logger)

func Warn

func Warn(msg string, args ...any)

func WrapToErr

func WrapToErr(obj any) error

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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