从零开始学golang 给gin框架加上日志logrus

freedbg · · 2739 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

```go package main import ( "fmt" "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" "time" //这里我们导入已经集成的 mysql 驱动,当然也可以导入原版的 import _ "github.com/go-sql-driver/mysql" 一样的 _ "github.com/jinzhu/gorm/dialects/mysql" "github.com/rifflock/lfshook" "github.com/sirupsen/logrus" //"os" ) //step1 获取 http_proxy=192.168.1.2:20080 go get github.com/gin-gonic/gin //http_proxy=192.168.1.2:20080 这部分是代理设置 // //step2 http_proxy=192.168.1.2:20080 go get -u github.com/jinzhu/gorm // //step3 http_proxy=192.168.1.2:20080 go get github.com/go-sql-driver/mysql // //step4 http_proxy=192.168.1.2:20080 go get github.com/sirupsen/logrus // // step4.1 http_proxy=192.168.1.2:20080 go get github.com/rifflock/lfshook 写入到文件吧大兄弟 type User struct { ID uint `gorm:"primary_key` Uname string CreatedAt time.Time } //step4.1 var Log *logrus.Logger func NewLogger() *logrus.Logger { if Log != nil { return Log } pathMap := lfshook.PathMap{ logrus.InfoLevel: "./info.log", logrus.ErrorLevel: "./info.log", } Log = logrus.New() Log.Hooks.Add(lfshook.NewHook( pathMap, &logrus.JSONFormatter{}, )) return Log } func init() { Log = NewLogger() // Log as JSON instead of the default ASCII formatter. //Log.SetFormatter(&logrus.JSONFormatter{}) // Output to stdout instead of the default stderr // Can be any io.Writer, see below for File example //Log.SetOutput(os.Stdout) // Only log the warning severity or above. //Log.SetLevel(log.WarnLevel) } func main() { r := gin.Default() v1 := r.Group("api/v1") { v1.GET("/users", GetUsers) } db, err := gorm.Open("mysql", "golang:Password&123@tcp(127.0.0.1:3306)/golang?charset=utf8&parseTime=True&loc=Local") defer db.Close() if err == nil { fmt.Println("DB connect success") } user := User{Uname: "test1"} fmt.Println(db.NewRecord(user)) // => returns `true` as primary key is blank ret := db.Create(&user) fmt.Println(user) fmt.Println(ret.Error) //step4 // log.Debug("Useful debugging information.") // log.Info("Something noteworthy happened!") // log.Warn("You should probably take a look at this.") // log.Error("Something failed but I'm not quitting.") // log.WithFields(log.Fields{ // "animal": "walrus", // "size": 10, // }).Warn("A group of walrus emerges from the ocean") Log.Warn("You should probably take a look at this.") Log.Error("Something failed but I'm not quitting.") Log.WithFields(logrus.Fields{ "animal": "walrus", "size": 10, }).Error("A group of walrus emerges from the ocean") r.Run(":8080") } func GetUsers(c *gin.Context) { c.JSON(200, "hello world") } ``` git地址 喜欢拉代码的弟兄自己来 https://github.com/godla/golang-gin-web-study.git 一起每天撸一点吧

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2739 次点击  
加入收藏 微博
1 回复  |  直到 2018-01-22 05:02:40
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传