codecamp

GoFrame 日志组件-堆栈打印

错误日志信息支持​Stack​特性,该特性可以自动打印出当前调用日志组件方法的堆栈信息,该堆栈信息可以通过​Notice*/Warning*/Error*/Critical*/Panic*/Fatal*​等错误日志输出方法触发,也可以通过​GetStack/PrintStack​获取/打印。错误信息的​stack​信息对于调试来说相当有用。

示例1,通过Error方法触发

package main

import (
	"context"

	"github.com/gogf/gf/v2/os/glog"
)

func Test(ctx context.Context) {
	glog.Error(ctx, "This is error!")
}

func main() {
	ctx := context.TODO()
	Test(ctx)
}

打印出的结果如下:

2022-01-05 15:08:54.998 [ERRO] This is error! 
Stack:
1.  main.Test
    C:/hailaz/test/main.go:10
2.  main.main
    C:/hailaz/test/main.go:15

示例2,通过Stack方法打印

package main

import (
	"context"
	"fmt"

	"github.com/gogf/gf/v2/os/glog"
)

func main() {
	ctx := context.TODO()
	glog.PrintStack(ctx)
	glog.New().PrintStack(ctx)

	fmt.Println(glog.GetStack())
	fmt.Println(glog.New().GetStack())
}

执行后,输出结果为:

2019-07-12 22:20:28.070 Stack:
1. main.main
   /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_stack.go:11

2019-07-12 22:20:28.070 Stack:
1. main.main
   /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_stack.go:12

1. main.main
   /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_stack.go:14

1. main.main
   /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_stack.go:15

示例3,打印gerror.Error

glog​日志模块支持对标准错误以及​gerror​错误的堆栈打印支持。

package main

import (
	"context"
	"errors"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/glog"
)

func MakeError() error {
	return errors.New("connection closed with normal error")
}

func MakeGError() error {
	return gerror.New("connection closed with gerror")
}

func TestGError(ctx context.Context) {
	err1 := MakeError()
	err2 := MakeGError()
	glog.Error(ctx, err1)
	glog.Error(ctx, err2)
}

func main() {
	ctx := context.TODO()
	TestGError(ctx)
}

执行后,终端输出:

2019-07-12 22:23:11.467 [ERRO] connection closed with normal error
Stack:
1. main.TestGError
   /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:20
2. main.main
   /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:25

2019-07-12 22:23:11.467 [ERRO] connection closed with gerror
1. connection closed with gerror
    1). main.MakeGError
        /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:14
    2). main.TestGError
        /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:19
    3). main.main
        /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:25
Stack:
1. main.TestGError
   /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:21
2. main.main
   /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:25


GoFrame 日志组件-异步输出
GoFrame 日志组件-调试信息
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

GoFrame 核心组件

GoFrame 核心组件-数据库ORM

GoFrame 模块列表

GoFrame 模块列表-单元测试

GoFrame 模块列表-功能调试

GoFrame WEB服务开发

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }