-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
371 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
APP_ENV=local | ||
APP_DEBUG=true | ||
APP_NAME=test | ||
APP_PORT=8080 | ||
|
||
DB_CONNECTION=mysql | ||
DB_HOST=192.168.10.10 | ||
DB_PORT=3306 | ||
DB_DATABASE=homestead | ||
DB_PARAMS="parseTime=true&loc=Local" | ||
DB_USERNAME=homestead | ||
DB_PASSWORD=secret | ||
|
||
JWT_SecretKey=test | ||
JWT_EXP=480 | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"github.com/sirupsen/logrus" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type MineFormatter struct{} | ||
|
||
const TimeFormat = "2006-01-02 15:04:05" | ||
|
||
func (s *MineFormatter) Format(entry *logrus.Entry) ([]byte, error) { | ||
|
||
msg := fmt.Sprintf("[%s] [%s] %s\n", time.Now().Local().Format(TimeFormat), strings.ToUpper(entry.Level.String()), entry.Message) | ||
|
||
return []byte(msg), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package config | ||
|
||
import ( | ||
"IrisFramework/app/Models" | ||
"errors" | ||
"fmt" | ||
"github.com/jinzhu/gorm" | ||
_ "github.com/jinzhu/gorm" | ||
log "github.com/sirupsen/logrus" | ||
"os" | ||
) | ||
|
||
type GormLogger struct{} | ||
var Gorm *gorm.DB | ||
|
||
func initDB() { | ||
once.Do(func() { | ||
dbDriver := os.Getenv("DB_CONNECTION") | ||
switch dbDriver { | ||
case "mysql": | ||
initMysql() | ||
break | ||
default: | ||
panic(errors.New("only supper mysql")) | ||
break | ||
} | ||
}) | ||
|
||
} | ||
|
||
/** | ||
初始化mysql | ||
*/ | ||
func initMysql() { | ||
db, err := gorm.Open("mysql", "homestead:secret@(192.168.10.10:3306)/homestead?charset=utf8&parseTime=True&loc=Local") | ||
if err != nil { | ||
panic("failed to connect database") | ||
} | ||
defer db.Close() | ||
|
||
db.SetLogger(&GormLogger{}) | ||
db.LogMode(true) | ||
db.SingularTable(true) | ||
|
||
// test | ||
Gorm = db | ||
|
||
var userModel Models.User | ||
Gorm.Where("username like ?","%风%").Find(&userModel) | ||
} | ||
|
||
|
||
/** | ||
打印日志 | ||
*/ | ||
func (*GormLogger) Print(v ...interface{}) { | ||
|
||
switch v[0] { | ||
case "sql": | ||
// [gorm] [sql] SELECT * FROM `user` WHERE `user`.`deleted_at` IS NULL AND ((username like ?)) []interface {}{"%萧风%"} [1.880301ms] | ||
msg := fmt.Sprintf("[%s] [%s] [%s] %s %#v ", "gorm", v[0], v[2], v[3], v[4]) | ||
Log.Info(msg) | ||
case "log": | ||
log.WithFields(log.Fields{"module": "gorm", "type": "log"}).Print(v[2]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package config | ||
|
||
func init() { | ||
// 初始化日志 | ||
InitLog() | ||
// 初始化 数据库 | ||
initDB() | ||
|
||
//initXorm() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package config | ||
|
||
import ( | ||
config "IrisFramework/config/Log" | ||
rotatelogs "github.com/lestrrat/go-file-rotatelogs" | ||
"github.com/rifflock/lfshook" | ||
"github.com/sirupsen/logrus" | ||
"os" | ||
"path" | ||
"time" | ||
) | ||
|
||
const LogPath = "./storage/logs" | ||
const FileSuffix = ".log" | ||
|
||
var Log = logrus.New() | ||
|
||
func InitLog() { | ||
Log.Out = os.Stdout | ||
var loglevel logrus.Level | ||
err := loglevel.UnmarshalText([]byte("info")) | ||
if err != nil { | ||
Log.Panicf("设置log级别失败:%v", err) | ||
} | ||
|
||
Log.SetLevel(loglevel) | ||
|
||
NewSimpleLogger(Log, LogPath, 8) | ||
} | ||
|
||
/** | ||
文件日志 | ||
*/ | ||
func NewSimpleLogger(log *logrus.Logger, logPath string, save uint) { | ||
|
||
lfHook := lfshook.NewHook(lfshook.WriterMap{ | ||
logrus.DebugLevel: writer(logPath, "debug", save), // 为不同级别设置不同的输出目的 | ||
logrus.InfoLevel: writer(logPath, "info", save), | ||
logrus.WarnLevel: writer(logPath, "warn", save), | ||
logrus.ErrorLevel: writer(logPath, "error", save), | ||
logrus.FatalLevel: writer(logPath, "fatal", save), | ||
logrus.PanicLevel: writer(logPath, "panic", save), | ||
}, &config.MineFormatter{}) | ||
|
||
log.AddHook(lfHook) | ||
} | ||
|
||
/** | ||
文件设置 | ||
*/ | ||
func writer(logPath string, level string, save uint) *rotatelogs.RotateLogs { | ||
logFullPath := path.Join(logPath, level) | ||
var cstSh, _ = time.LoadLocation("Asia/Shanghai") //上海 | ||
fileSuffix := time.Now().In(cstSh).Format("2006-01-02") + FileSuffix | ||
|
||
logier, err := rotatelogs.New( | ||
logFullPath+"-"+fileSuffix, | ||
rotatelogs.WithLinkName(logFullPath), // 生成软链,指向最新日志文件 | ||
rotatelogs.WithRotationCount(int(save)), // 文件最大保存份数 | ||
rotatelogs.WithRotationTime(time.Hour*24), // 日志切割时间间隔 | ||
) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
return logier | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.