crash

package
v1.16.1 Latest Latest
Warning

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

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

README

Manual

捕获运行时 panic

API

Crash
type Crash struct {
    Logger  *logrus.Logger `json:"logger"`
    Message string         `json:"message"`
}
RecoverFunc
type RecoverFunc func(*Crash)
捕获 panic 并打印日志
func Recover(message string, fns ...RecoverFunc)

示例

package main

import (
	"github.com/sirupsen/logrus"

	"github.com/o8x/jk/crash"
	"github.com/o8x/jk/signal"
)

func foo() {
	panic("panic in foo")
}

func main() {
	go func() {
		defer crash.Recover("recover by main")

		foo()
	}()

	go func() {
		l := logrus.New()
		l.SetLevel(logrus.FatalLevel)
		defer crash.Recover("crash", crash.Logger(l))

		foo()
	}()

	signal.Wait()
}

运行它

第二次注入的 logger 是 fatal 级别,所以 Error 级别的日志不会被打印

> go run .
ERRO[0000] recover by main       recover="panic in foo" stack="goroutine .. [running]:....."

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Logger added in v1.9.2

func Logger(log *logrus.Logger) func(c *Crash)

func Recover

func Recover(message string, fns ...RecoverFunc)

Types

type Crash added in v1.9.2

type Crash struct {
	Logger  *logrus.Logger `json:"logger"`
	Message string         `json:"message"`
}

type RecoverFunc added in v1.9.2

type RecoverFunc func(*Crash)

Jump to

Keyboard shortcuts

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