ce

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

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

Go to latest
Published: Jun 24, 2023 License: MIT Imports: 10 Imported by: 0

README

ce

A logging tool that wraps zap

usage
package main

import (
	"github.com/hqsdevnow/ce"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"gopkg.in/natefinch/lumberjack.v2"
	"os"
	"time"
)

func main() {
	defer func() {
		ce.Sync()
	}()

	defer ce.Recover(true)
	ce.Debug("test1", zap.String("k1", "v1"), zap.Int32("k2", 2))
	ce.Info("test2")
	ce.Warn("test3")
	ce.Error("test4")

	var enablePanic bool
	if enablePanic {
		ce.Panic("test5")
	}

	var enableFatalTest bool
	if enableFatalTest {
		ce.Fatal("test6")
	}

	var enableLumberjackTest bool
	if enableLumberjackTest {
		ce.DefaultLogger = ce.NewLogger(
			ce.DefaultAtomicLevel,
			zap.DPanicLevel,
			zapcore.AddSync(os.Stderr),
			zapcore.AddSync(&lumberjack.Logger{
				Filename:   "/tmp/foobar.txt",
				MaxSize:    1,  // mb
				MaxAge:     30, // days
				MaxBackups: 7,  // count of log file
				LocalTime:  false,
				Compress:   false,
			}),
		)
		for i := 0; i < 0xfff; i++ {
			ce.Info("bar", zap.String("time", time.Now().String()))
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultAtomicLevel zap.AtomicLevel
View Source
var DefaultLogger *zap.Logger
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", zap.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 {
				DefaultLogger.WithOptions(zap.AddCallerSkip(1)).Error("recover", zap.Any("recoverObj", recoverObj), zap.Stack("callStack"))
			} else {
				DefaultLogger.WithOptions(zap.AddCallerSkip(1)).Error("recover", zap.Any("recoverObj", recoverObj))
			}
		}

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

Functions

func CheckError

func CheckError(err error, fields ...zap.Field)

func Debug

func Debug(msg string, fields ...zap.Field)

func Error

func Error(msg string, fields ...zap.Field)

func Fatal

func Fatal(msg string, fields ...zap.Field)

func Info

func Info(msg string, fields ...zap.Field)

func NewLogger

func NewLogger(level, levelByStacktrace zapcore.LevelEnabler, writes ...zapcore.WriteSyncer) *zap.Logger

func OpenPPROF

func OpenPPROF(addr string)

func Panic

func Panic(msg string, fields ...zap.Field)

func Print

func Print(args ...interface{})

func Printf

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

func Sync

func Sync()

func Warn

func Warn(msg string, fields ...zap.Field)

func WrapObjToErr

func WrapObjToErr(obj interface{}) 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