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