errlog

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

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

Go to latest
Published: Mar 25, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Example
package main

import (
	"errors"

	"github.com/streamwest-1629/errlog"
)

func StacktraceTestA() error {
	return errlog.Stamp(errlog.Container{
		Type:    errlog.LogError,
		Message: "call in stack",
		With:    []errlog.Pair{errlog.QuotedString("func", "StacktraceTestA")},
	})
}

func StacktraceTestB() error {

	if err := StacktraceTestA(); err != nil {
		return errlog.Stamp(errlog.Container{
			Type:     errlog.LogError,
			Message:  "call in stack",
			With:     []errlog.Pair{errlog.QuotedString("func", "StacktraceTestB")},
			Internal: err,
		})
	}

	return nil
}

func main() {
	errlog.Default.Message(errlog.LogInfo, nil, "hello, world errlog!")
	errlog.Default.Log(errlog.Stamp(
		errlog.Container{
			Type:    errlog.LogInfo,
			Message: "errlog was created with consideration for its compatibility with errors in Go language",
			Description: "the objects issued for each logging can be treated as error types." +
				" Therefore, you can choose whether to output the created logs as \"output as 'logged message'\"" +
				" or \"delegate the processing to the caller as an 'error'\".",
		}))

	errlog.Default.Message(errlog.LogInfo, nil, "it supports errors.Unwrap(), errors.Is() function in errors package")
	errlog.Default.Message(errlog.LogInfo, errors.New("example error"), "of course, it also supports the normal errors.New() function")
	errlog.Default.Message(errlog.LogFixed, StacktraceTestB(), "it supports stacktrace")
}
Output:

Index

Examples

Constants

View Source
const (
	LogDebug LogType = 1 << iota
	LogInfo
	LogFixed
	LogWarn
	LogError
	LogFatal
	UnknownLog     LogType = 0
	LogDebugExpr           = "DEBUG"
	LogInfoExpr            = "INFO"
	LogFixedExpr           = "FIXED"
	LogWarnExpr            = "WARN"
	LogErrorExpr           = "ERROR"
	LogFatalExpr           = "FATAL"
	LogUnknownExpr         = "UNKNOWN"
)

Variables

View Source
var Default interface {
	Message(level LogType, err error, msg string, with ...Pair)
	Log(content Container)
} = DefaultLoggers.CommandLine
View Source
var DefaultLoggers = struct {
	CommandLine CommandLineLogger
}{
	CommandLine: CommandLineLogger{
		Color: true,
		Dest:  os.Stdout,
	},
}
View Source
var DefaultStamp = LogStampConfig{
	EnableTimestamp:       true,
	TimestampFormat:       time.RFC3339,
	EnableStackTraceTypes: LogFixed | LogError | LogFatal,
	StackTraceFactory: func(programCounters []uintptr, dest []string) int {

		dest = dest[:0]
		frames := runtime.CallersFrames(programCounters)

		for {
			frame, more := frames.Next()
			funcName := filepath.Base(frame.Func.Name())
			funcName = funcName[strings.IndexByte(funcName, '.')+1:]
			dest = append(dest, frame.File+":"+strconv.Itoa(frame.Line)+" "+funcName)

			if !more {
				return len(dest)
			}
		}
	},
}
View Source
var LoggingTypes = LogWarn | LogError | LogFatal

Functions

This section is empty.

Types

type CommandLineLogger

type CommandLineLogger struct {
	Color        bool
	Dest         io.Writer
	DefaultStamp *LogStampConfig
}

func (CommandLineLogger) Log

func (cl CommandLineLogger) Log(content Container)

func (CommandLineLogger) Message

func (cl CommandLineLogger) Message(level LogType, err error, msg string, with ...Pair)

type Container

type Container struct {
	Type        LogType
	Message     string
	Description string
	Internal    error
	With        []Pair
	// contains filtered or unexported fields
}

func Stamp

func Stamp(container Container) Container

func StampDetail

func StampDetail(container Container, traceSkip int) Container

func (Container) Error

func (lc Container) Error() string

func (Container) Is

func (lc Container) Is(compare error) bool

func (Container) StackTrace

func (lc Container) StackTrace(dest []string) int

func (Container) Unwrap

func (lc Container) Unwrap() error

func (Container) WalkErrorstack

func (lc Container) WalkErrorstack(fn func(container Container, wrapped error))

type LogStampConfig

type LogStampConfig struct {
	EnableTimestamp       bool
	TimestampFormat       string
	EnableStackTraceTypes LogType
	StackTraceFactory     func(programCounters []uintptr, dest []string) int
}

func (*LogStampConfig) Stamp

func (conf *LogStampConfig) Stamp(container Container) Container

func (*LogStampConfig) StampDetail

func (conf *LogStampConfig) StampDetail(container Container, traceSkip int) Container

type LogType

type LogType int

type Pair

type Pair struct {
	Key string
	// contains filtered or unexported fields
}

func Bool

func Bool(key string, boolean bool) Pair

func Int

func Int[T constraints.Signed](key string, signed T) Pair

func QuotedString

func QuotedString(key, str string) Pair

func TypeOf

func TypeOf(key string, value interface{}) Pair

func Uint

func Uint[T constraints.Unsigned](key string, unsigned T) Pair

Jump to

Keyboard shortcuts

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