Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Golang(14)Error Handle Debugging Logging大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Golang(14)Error Handle Debugging Logging

1. Error Handle
func Open(name String) (file *File,err error)

It will return the err if we open file fail.

f,err := os.Open(“filename.ext”)
if err != nil {
log.Fatal(err)
}

Error Type
type error interface {
Error() String
}

There is one method in thE interface error.
implementation of error interface

type errorString struct {
s String
}

func (e *errorString) Error() String {
return e.s
}

fund New(text String) error {
return &errorString{text}
}

We can use New to create one error instance.

For example>
func Sqrt(f float64) (float64,error) {
if f < 0 {
return 0,errors.New(“math: square root of negative number”)
}
…snip...
}

Customer Error Type

type SyntaxError struct {
msg String
Offset int64
}

func (e *SyntaxError) Error() String { return e.msg }

Error Handle
We can deal the error in our router like this>
func viewRecord(w http.ResponseWriter,r *http.request) {
…snip…
if err := datastore.Get(c,key,record); err != nil {
http.Error(w,err.Error(),500)
return
}
if err := viewTemplate.Execute(w,500)
}
}

It works,but we need to put these error handle codes there again and again.

type appHandler func(http.ResponseWriter,*http.request) error

func (fn appHandler) Servehttp(w http.ResponseWriter,r *http.request) {
if err := fn(w,r); err != nil {
http.Error(w,500)
}
}

func init() {
http.Handle(“/view”,appHandler(viewRecord))
}

Then our viewRecord function can be like this.
func viewRecord(w http.ResponseWriter,r *http.request) error {
…snip..
if err := datastore.Get(c,record); err != nil {
return err
}
return viewTemplate.Execute(w,record)
}

We can define the Error Type ourselves.

type appError struct {
Error error
message String
Code int
}

type appHandler func(http.ResponseWriter,*http.request) *appError

func (fn appHandler) Servehttp(w http.ResponseWriter,r *http.request) {
if e := fn(w,r); e != nil {
http.Error(w,e.message,e.CodE)
}
}

If we use our customized Error Type,we can change the viewRecord like this.
func viewRecord(w http.ResponseWriter,r *http.request) *appError {
…snip…
if err := datastore.Get(c,record); err != nil {
return &appError{err,“record not found”,404 }
}
if err := viewTemplate.Execute(w,“Can not display record”,500}
}
return nil
}

2. Use GDB Debugging
…snip...

3. WriTing the Test
…snip…

4. Try seelog

Install the module
>go get -u github.com/cihub/seelog

The Simplest Example works
package main

import log "github.com/cihub/seelog"

func main() {
defer log.Flush()
log.Info("Hello from Seelog!")
}

Create the module logs
I create the directory and file logs/LogCustom.go with codes
package logs

import (
"fmt"
seelog "github.com/cihub/seelog"
)

var Logger seelog.LoggerInterface

func loadAppConfig() {
logger,err := seelog.LoggerFromConfigAsFile("/Users/carl/work/easy/easygo/src/com/sillycat/easygoapp/seelog.xml")
if err != nil {
fmt.Println(err)
return
}
UseLogger(logger)
}

func init() {
DisableLog()
loadAppConfig()
}

// DisableLog disables all library log output
func DisableLog() {
Logger = seelog.Disabled
}

// UseLogger uses a specified seelog.LoggerInterface to output library log.
// Use this func if you are using Seelog logging system in your app.
func UseLogger(newLogger seelog.LoggerInterfacE) {
Logger = newLogger
}

@H_710_2@my logsee.xml will be as follow,we can change more configure based the doc from seelog
<seelog type="adaptive" mininterval="2000000" maxinterval="100000000" critmsgcount="500" minlevel="debug">
<exceptions>
<exception filepattern="test*" minlevel="error"/>
</exceptions>
<outputs formatid="all">
<file path="all.log"/>
<filter levels="info">
<console formatid="fmTinfo"/>
</filter>
<filter levels="error,critical" formatid="fmterror">
<console/>
<file path="errors.log"/>
</filter>
</outputs>
<formats>
<format id="fmTinfo" format="[%Level] [%Time] %Msg%n"/>
<format id="fmterror" format="[%LEVEL] [%Time] [%FuncShort @ %File.%Line] %Msg%n"/>
<format id="all" format="[%Level] [%Time] [@ %File.%Line] %Msg%n"/>
<format id="criticalemail" format="Critical error on our server!\n %Time %Date %relFile %Func %Msg \nSent by Seelog"/>
</formats>
</seelog>

Then we build the customized module like this
>go build src/com/sillycat/easygoapp/logs/LogCustom.go

Then I can use it like this>
package main

import (
"com/sillycat/easygoapp/logs"
)

func main() {
addr := "locahost"
logs.Logger.Info("Start server at:",addr)
err := "Fail to start the server."
logs.Logger.Critical("Server err:",err)
}

This is the right way how we can manage our modules and codes in our own projects.

References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/11.1.md
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/11.2.md

https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/11.3.md

https://github.com/cihub/seelog

http://godoc.org/github.com/cihub/seelog@H_445_616@

大佬总结

以上是大佬教程为你收集整理的Golang(14)Error Handle Debugging Logging全部内容,希望文章能够帮你解决Golang(14)Error Handle Debugging Logging所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。