Skip to content

Commit

Permalink
分级日志
Browse files Browse the repository at this point in the history
  • Loading branch information
bydmm committed Aug 26, 2019
1 parent 10e2229 commit c703169
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ REDIS_ADDR="127.0.0.1:6379"
REDIS_PW=""
REDIS_DB=""
SESSION_SECRE=""
GIN_MODE="debug"
GIN_MODE="debug"
LOG_LEVEL="debug"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
.env
.env
go.sum
3 changes: 2 additions & 1 deletion cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cache

import (
"os"
"singo/util"
"strconv"

"github.com/go-redis/redis"
Expand All @@ -22,7 +23,7 @@ func Redis() {
_, err := client.Ping().Result()

if err != nil {
panic(err)
util.Log().Panic("连接Redis不成功", err)
}

RedisClient = client
Expand Down
8 changes: 6 additions & 2 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package conf

import (
"os"
"singo/cache"
"singo/model"
"os"
"singo/util"

"github.com/joho/godotenv"
)
Expand All @@ -13,9 +14,12 @@ func Init() {
// 从本地读取环境变量
godotenv.Load()

// 设置日志级别
util.BuildLogger(os.Getenv("LOG_LEVEL"))

// 读取翻译文件
if err := LoadLocales("conf/locales/zh-cn.yaml"); err != nil {
panic(err)
util.Log().Panic("翻译文件加载失败", err)
}

// 连接数据库
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module singo
go 1.12

require (
github.com/bydmm/jiode v0.1.0 // indirect
github.com/gin-contrib/cors v1.3.0
github.com/gin-contrib/sessions v0.0.0-20190512062852-3cb4c4f2d615
github.com/gin-gonic/gin v1.4.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff/go.mod h1:+RTT1BOk5P97fT2CiHkbFQwkK3mjsFAP6zCYV2aXtjw=
github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1/go.mod h1:dkChI7Tbtx7H1Tj7TqGSZMOeGpMP5gLHtjroHd4agiI=
github.com/bydmm/jiode v0.1.0/go.mod h1:6mWSrpEwPK5RTEsqLFoQWyF8SZlueViTCkVEs+f4Fwg=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -59,6 +60,7 @@ github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+
github.com/gorilla/sessions v1.1.1/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
github.com/gorilla/sessions v1.1.3 h1:uXoZdcdA5XdXF3QzuSlheVRUvjl+1rKY7zBXL68L9RU=
github.com/gorilla/sessions v1.1.3/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jinzhu/gorm v1.9.10 h1:HvrsqdhCW78xpJF67g1hMxS6eCToo9PZH4LDB8WKPac=
Expand Down Expand Up @@ -169,6 +171,7 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376/go.mod h1:BHKOc1m5wm8WwQkMqYBoo4vNxhmF7xg8+xhG8L+Cy3M=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
Expand Down
3 changes: 2 additions & 1 deletion model/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"singo/util"
"time"

"github.com/jinzhu/gorm"
Expand All @@ -18,7 +19,7 @@ func Database(connString string) {
db.LogMode(true)
// Error
if err != nil {
panic(err)
util.Log().Panic("连接数据库不成功", err)
}
//设置连接池
//空闲
Expand Down
106 changes: 106 additions & 0 deletions util/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package util

import (
"fmt"
"os"
"time"
)

const (
// LevelError 错误
LevelError = iota
// LevelWarning 警告
LevelWarning
// LevelInformational 提示
LevelInformational
// LevelDebug 除错
LevelDebug
)

var logger *Logger

// Logger 日志
type Logger struct {
level int
}

// Println 打印
func (ll *Logger) Println(msg string) {
fmt.Printf("%s %s", time.Now().Format("2006-01-02 15:04:05 -0700"), msg)
}

// Panic 极端错误
func (ll *Logger) Panic(format string, v ...interface{}) {
if LevelError > ll.level {
return
}
msg := fmt.Sprintf("[Panic] "+format, v...)
ll.Println(msg)
os.Exit(0)
}

// Error 错误
func (ll *Logger) Error(format string, v ...interface{}) {
if LevelError > ll.level {
return
}
msg := fmt.Sprintf("[E] "+format, v...)
ll.Println(msg)
}

// Warning 警告
func (ll *Logger) Warning(format string, v ...interface{}) {
if LevelWarning > ll.level {
return
}
msg := fmt.Sprintf("[W] "+format, v...)
ll.Println(msg)
}

// Info 信息
func (ll *Logger) Info(format string, v ...interface{}) {
if LevelInformational > ll.level {
return
}
msg := fmt.Sprintf("[I] "+format, v...)
ll.Println(msg)
}

// Debug 校验
func (ll *Logger) Debug(format string, v ...interface{}) {
if LevelDebug > ll.level {
return
}
msg := fmt.Sprintf("[D] "+format, v...)
ll.Println(msg)
}

// BuildLogger 构建logger
func BuildLogger(level string) {
intLevel := LevelError
switch level {
case "error":
intLevel = LevelError
case "warning":
intLevel = LevelWarning
case "info":
intLevel = LevelInformational
case "debug":
intLevel = LevelDebug
}
l := Logger{
level: intLevel,
}
logger = &l
}

// Log 返回日志对象
func Log() *Logger {
if logger == nil {
l := Logger{
level: LevelDebug,
}
logger = &l
}
return logger
}

0 comments on commit c703169

Please sign in to comment.