forked from golang/hotime
optimize log tool
This commit is contained in:
parent
770f0a94c9
commit
740059075a
@ -164,7 +164,8 @@ func (that *Application) Run(router Router) {
|
||||
func (that *Application) SetConnectDB(connect func(err ...*Error) (master, slave *sql.DB)) {
|
||||
|
||||
that.connectDbFunc = connect
|
||||
that.Db.SetConnect(that.connectDbFunc)
|
||||
|
||||
that.Db.SetConnect(that.connectDbFunc, &that.Error)
|
||||
|
||||
}
|
||||
|
||||
@ -197,6 +198,7 @@ func (that *Application) SetCache(cache CacheIns) {
|
||||
func (that *Application) SetConfig(configPath ...string) {
|
||||
|
||||
that.Log = GetLog("", true)
|
||||
that.Error = Error{Logger: that.Log}
|
||||
if len(configPath) != 0 {
|
||||
that.configPath = configPath[0]
|
||||
}
|
||||
@ -223,7 +225,7 @@ func (that *Application) SetConfig(configPath ...string) {
|
||||
}
|
||||
|
||||
that.Log = GetLog(that.Config.GetString("logFile"), true)
|
||||
|
||||
that.Error = Error{Logger: that.Log}
|
||||
if that.Config.GetBool("webConnectLogShow") {
|
||||
that.WebConnectLog = GetLog(that.Config.GetString("webConnectLogFile"), false)
|
||||
}
|
||||
@ -465,6 +467,7 @@ func Init(config string) Application {
|
||||
appIns := Application{}
|
||||
//手动模式,
|
||||
appIns.SetConfig(config)
|
||||
|
||||
SetDB(&appIns)
|
||||
//appIns.SetCache()
|
||||
return appIns
|
||||
@ -483,6 +486,8 @@ func SetDB(appIns *Application) {
|
||||
}
|
||||
}
|
||||
func SetMysqlDB(appIns *Application, config Map) {
|
||||
|
||||
appIns.Db.Type = "mysql"
|
||||
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
|
||||
//master数据库配置
|
||||
query := config.GetString("user") + ":" + config.GetString("password") +
|
||||
@ -507,9 +512,10 @@ func SetMysqlDB(appIns *Application, config Map) {
|
||||
return master, slave
|
||||
//return DB
|
||||
})
|
||||
appIns.Db.Type = "mysql"
|
||||
}
|
||||
func SetSqliteDB(appIns *Application, config Map) {
|
||||
|
||||
appIns.Db.Type = "sqlite"
|
||||
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
|
||||
db, e := sql.Open("sqlite3", config.GetString("path"))
|
||||
if e != nil && len(err) != 0 {
|
||||
@ -519,5 +525,4 @@ func SetSqliteDB(appIns *Application, config Map) {
|
||||
|
||||
return master, slave
|
||||
})
|
||||
appIns.Db.Type = "sqlite"
|
||||
}
|
||||
|
2
cache/cache_db.go
vendored
2
cache/cache_db.go
vendored
@ -10,7 +10,7 @@ import (
|
||||
|
||||
type HoTimeDBInterface interface {
|
||||
Query(query string, args ...interface{}) []Map
|
||||
Exec(query string, args ...interface{}) (sql.Result, Error)
|
||||
Exec(query string, args ...interface{}) (sql.Result, *Error)
|
||||
Get(table string, qu ...interface{}) Map
|
||||
Select(table string, qu ...interface{}) []Map
|
||||
Delete(table string, data map[string]interface{}) int64
|
||||
|
@ -1,7 +1,12 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Error 框架层处理错误
|
||||
type Error struct {
|
||||
*logrus.Logger
|
||||
error
|
||||
}
|
||||
|
||||
@ -13,5 +18,10 @@ func (that *Error) GetError() error {
|
||||
|
||||
func (that *Error) SetError(err error) {
|
||||
that.error = err
|
||||
if that.Logger != nil {
|
||||
//that.Logger=log.GetLog("",false)
|
||||
that.Logger.Warn(err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import (
|
||||
|
||||
type Context struct {
|
||||
*Application
|
||||
ContextBase
|
||||
Resp http.ResponseWriter
|
||||
Req *http.Request
|
||||
RouterString []string
|
||||
|
14
db/db.go
14
db/db.go
@ -6,6 +6,8 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
@ -20,7 +22,7 @@ type HoTimeDB struct {
|
||||
LastQuery string
|
||||
LastData []interface{}
|
||||
ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB)
|
||||
LastErr Error
|
||||
LastErr *Error
|
||||
limit Slice
|
||||
*sql.Tx //事务对象
|
||||
SlaveDB *sql.DB
|
||||
@ -67,11 +69,11 @@ func (that *HoTimeDB) Action(action func(db HoTimeDB) bool) bool {
|
||||
return result
|
||||
}
|
||||
|
||||
func (that *HoTimeDB) InitDb(err ...*Error) Error {
|
||||
func (that *HoTimeDB) InitDb(err ...*Error) *Error {
|
||||
if len(err) != 0 {
|
||||
that.LastErr = *(err[0])
|
||||
that.LastErr = err[0]
|
||||
}
|
||||
that.DB, that.SlaveDB = that.ConnectFunc(&that.LastErr)
|
||||
that.DB, that.SlaveDB = that.ConnectFunc(that.LastErr)
|
||||
if that.DB == nil {
|
||||
return that.LastErr
|
||||
}
|
||||
@ -149,7 +151,7 @@ func (that *HoTimeDB) Row(resl *sql.Rows) []Map {
|
||||
if e != nil {
|
||||
that.LastErr.SetError(e)
|
||||
} else {
|
||||
lis.JsonToMap(string(jlis), &that.LastErr)
|
||||
lis.JsonToMap(string(jlis), that.LastErr)
|
||||
}
|
||||
|
||||
dest = append(dest, lis)
|
||||
@ -342,7 +344,7 @@ func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
|
||||
return that.Row(resl)
|
||||
}
|
||||
|
||||
func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, Error) {
|
||||
func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Error) {
|
||||
|
||||
that.LastQuery = query
|
||||
that.LastData = args
|
||||
|
@ -3,8 +3,6 @@ package main
|
||||
import (
|
||||
"../../hotime"
|
||||
"../../hotime/cache"
|
||||
"../../hotime/common"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"golang.org/x/net/websocket"
|
||||
"time"
|
||||
@ -12,8 +10,8 @@ import (
|
||||
|
||||
func main() {
|
||||
|
||||
appIns := hotime.Application{}
|
||||
|
||||
//appIns := hotime.Application{}
|
||||
appIns := hotime.Init("example/config/config.json")
|
||||
appIns.SetConnectListener(func(context *hotime.Context) bool {
|
||||
//fmt.Println(context.HandlerStr + time.Now().Format(" 2006-01-02 15:04 ") + hotime.Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":")))
|
||||
|
||||
@ -22,7 +20,7 @@ func main() {
|
||||
})
|
||||
|
||||
//手动模式,
|
||||
appIns.SetConfig("example/config/config.json")
|
||||
//appIns.SetConfig("example/config/config.json")
|
||||
//redis缓存接入
|
||||
ca := cache.CacheIns(&cache.CacheRedis{Host: appIns.Config.GetString("redisHost"), Pwd: appIns.Config.GetString("redisPwd"), Time: appIns.Config.GetCeilInt64("cacheLongTime")})
|
||||
ca.Cache("xyzm", "dasdas")
|
||||
@ -38,16 +36,16 @@ func main() {
|
||||
appIns.SetCache(cache.CacheIns(&cache.CacheMemory{}))
|
||||
|
||||
//快捷模式
|
||||
appIns.SetDefault(func(err ...*common.Error) (*sql.DB, *sql.DB) {
|
||||
query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") +
|
||||
"@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8"
|
||||
DB, e := sql.Open("mysql", query)
|
||||
if e != nil && len(err) != 0 {
|
||||
err[0].SetError(e)
|
||||
}
|
||||
return DB, nil
|
||||
})
|
||||
|
||||
//appIns.SetDefault(func(err ...*common.Error) (*sql.DB, *sql.DB) {
|
||||
// query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") +
|
||||
// "@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8"
|
||||
// DB, e := sql.Open("mysql", query)
|
||||
// if e != nil && len(err) != 0 {
|
||||
// err[0].SetError(e)
|
||||
// }
|
||||
// return DB, nil
|
||||
//})
|
||||
//init
|
||||
appIns.Run(hotime.Router{
|
||||
"app": hotime.Proj{
|
||||
"index": hotime.Ctr{
|
||||
|
Loading…
Reference in New Issue
Block a user