优化日志及config

This commit is contained in:
hoteas 2022-07-11 11:07:17 +08:00
parent 1ab9027c93
commit f37b9aee70
8 changed files with 541 additions and 8 deletions

View File

@ -615,6 +615,8 @@ func SetMysqlDB(appIns *Application, config Map) {
appIns.Db.Type = "mysql"
appIns.Db.DBName = config.GetString("name")
appIns.Db.Prefix = config.GetString("prefix")
appIns.Db.Log = appIns.Log
appIns.Db.Mode = appIns.Config.GetCeilInt("mode")
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
//master数据库配置
query := config.GetString("user") + ":" + config.GetString("password") +
@ -644,6 +646,8 @@ func SetSqliteDB(appIns *Application, config Map) {
appIns.Db.Type = "sqlite"
appIns.Db.Prefix = config.GetString("prefix")
appIns.Db.Mode = appIns.Config.GetCeilInt("mode")
appIns.Db.Log = appIns.Log
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
db, e := sql.Open("sqlite3", config.GetString("path"))
if e != nil && len(err) != 0 {

View File

@ -8,6 +8,7 @@ import (
"errors"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
"github.com/sirupsen/logrus"
"os"
"reflect"
"sort"
@ -19,6 +20,7 @@ type HoTimeDB struct {
ContextBase
DBName string
*cache.HoTimeCache
Log *logrus.Logger
Type string
Prefix string
LastQuery string
@ -28,6 +30,7 @@ type HoTimeDB struct {
limit Slice
*sql.Tx //事务对象
SlaveDB *sql.DB
Mode int //mode为0生产模式,1、为测试模式、2为开发模式
}
// SetConnect 设置数据库配置连接
@ -306,7 +309,11 @@ func (that *HoTimeDB) md5(query string, args ...interface{}) string {
}
func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
defer func() {
if that.Mode == 2 {
that.Log.Info("SQL:"+that.LastQuery, " DATA:", that.LastData, " ERROR:", that.LastErr.GetError())
}
}()
//fmt.Println(query)
var err error
var resl *sql.Rows
@ -348,7 +355,11 @@ func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
}
func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Error) {
defer func() {
if that.Mode == 2 {
that.Log.Info("SQL: "+that.LastQuery, " DATA: ", that.LastData, " ERROR: ", that.LastErr.GetError())
}
}()
that.LastQuery = query
that.LastData = args
var e error
@ -480,7 +491,7 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
temp, resWhere := that.where(where)
query += temp
query += temp + ";"
qs = append(qs, resWhere...)
md5 := that.md5(query, qs...)
@ -982,7 +993,7 @@ func (that *HoTimeDB) Update(table string, data Map, where Map) int64 {
temp, resWhere := that.where(where)
//fmt.Println(resWhere)
query += temp
query += temp + ";"
qs = append(qs, resWhere...)
res, err := that.Exec(query, qs...)
@ -1007,7 +1018,7 @@ func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
query := "DELETE FROM " + that.Prefix + table + " "
temp, resWhere := that.where(data)
query += temp
query += temp + ";"
res, err := that.Exec(query, resWhere...)
rows := int64(0)

View File

@ -0,0 +1,38 @@
{
"cache": {
"memory": {
"db": true,
"session": true,
"timeout": 7200
}
},
"codeConfig": [
{
"config": "config/app.json",
"mode": 0,
"name": "",
"rule": "config/rule.json",
"table": "admin"
}
],
"db": {
"sqlite": {
"path": "config/data.db"
}
},
"defFile": [
"index.html",
"index.htm"
],
"error": {
"1": "内部系统异常",
"2": "访问权限异常",
"3": "请求参数异常",
"4": "数据处理异常",
"5": "数据结果异常"
},
"mode": 2,
"port": "80",
"sessionName": "HOTIME",
"tpt": "tpt"
}

View File

@ -0,0 +1,78 @@
{
"cache": {
"db": {
"db": "默认false非必须缓存数据库启用后能减少数据库的读写压力",
"session": "默认true非必须缓存web session同时缓存session保持的用户缓存",
"timeout": "默认60 * 60 * 24 * 30非必须过期时间超时自动删除"
},
"memory": {
"db": "默认true非必须缓存数据库启用后能减少数据库的读写压力",
"session": "默认true非必须缓存web session同时缓存session保持的用户缓存",
"timeout": "默认60 * 60 * 2非必须过期时间超时自动删除"
},
"redis": {
"db": "默认true非必须缓存数据库启用后能减少数据库的读写压力",
"host": "默认服务ip127.0.0.1必须如果需要使用redis服务时配置",
"password": "默认密码空必须如果需要使用redis服务时配置默认密码空",
"port": "默认服务端口6379必须如果需要使用redis服务时配置",
"session": "默认true非必须缓存web session同时缓存session保持的用户缓存",
"timeout": "默认60 * 60 * 24 * 15非必须过期时间超时自动删除"
},
"注释": "可配置memorydbredis默认启用memory默认优先级为memory\u003eredis\u003edb,memory与数据库缓存设置项一致缓存数据填充会自动反方向反哺加入memory缓存过期将自动从redis更新但memory永远不会更新redis如果是集群建议不要开启memory配置即启用"
},
"codeConfig": [
"注释:配置即启用,非必须,默认无",
{
"config": "默认config/app.json必须接口描述配置文件",
"mode": "默认0非必须0为内嵌代码模式1为生成代码模式在开发模式下会显示更多的数据用于开发测试并能够辅助研发自动生成配置文件、代码等功能,",
"name": "默认无非必须有则生成代码到此目录无则采用缺省模式使用表名如设置为admin将在admin目录生成包名为admin的代码",
"rule": "默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成",
"table": "默认admin必须根据数据库内当前表名做为用户生成数据"
}
],
"crossDomain": "默认空 非必须空字符串为不开启如果需要跨域设置auto为智能开启所有网站允许跨域http://www.baidu.com为指定域允许跨域",
"db": {
"mysql": {
"host": "默认127.0.0.1必须数据库ip地址",
"name": "默认test必须数据库名称",
"password": "默认root必须数据库密码",
"port": "默认3306必须数据库端口",
"prefix": "默认空,非必须,数据表前缀",
"slave": {
"host": "默认127.0.0.1必须数据库ip地址",
"name": "默认test必须数据库名称",
"password": "默认root必须数据库密码",
"port": "默认3306必须数据库端口",
"user": "默认root必须数据库用户名",
"注释": "从数据库配置mysql里配置slave项即启用主从读写减少数据库压力"
},
"user": "默认root必须数据库用户名",
"注释": "除prefix及主从数据库slave项其他全部必须"
},
"sqlite": {
"path": "默认config/data.db必须数据库位置"
},
"注释": "配置即启用非必须默认使用sqlite数据库"
},
"defFile": "默认访问index.html或者index.htm文件必须默认访问文件类型",
"error": {
"1": "内部系统异常,在环境配置,文件访问权限等基础运行环境条件不足造成严重错误时使用",
"2": "访问权限异常,没有登录或者登录异常等时候使用",
"3": "请求参数异常request参数不满足要求比如参数不足参数类型错误参数不满足要求等时候使用",
"4": "数据处理异常,数据库操作或者三方请求返回的结果非正常结果,比如数据库突然中断等时候使用",
"5": "数据结果异常一般用于无法给出response要求的格式要求下使用比如response需要的是string格式但你只能提供int数据时",
"注释": "web服务内置错误提示自定义异常建议10开始"
},
"logFile": "无默认,非必须,如果需要存储日志文件时使用,保存格式为:a/b/c/20060102150405.txt,将生成a/b/c/年月日时分秒.txt按需设置",
"logLevel": "默认0必须0关闭1打印日志等级",
"mode": "默认0,非必须0生产模式1测试模式2开发模式web无缓存数据库不启用缓存",
"modeRouterStrict": "默认false,必须路由严格模式false,为大小写忽略必须匹配true必须大小写匹配",
"port": "默认80必须web服务开启Http端口0为不启用http服务,默认80",
"sessionName": "默认HOTIME必须设置session的cookie名",
"tlsCert": "默认空非必须https证书",
"tlsKey": "默认空非必须https密钥",
"tlsPort": "默认空非必须web服务https端口0为不启用https服务",
"tpt": "默认tpt必须web静态文件目录默认为程序目录下tpt目录",
"webConnectLogFile": "无默认非必须webConnectLogShow开启之后才能使用如果需要存储日志文件时使用保存格式为:a/b/c/20060102150405.txt,将生成a/b/c/年月日时分秒.txt按需设置",
"webConnectLogShow": "默认true非必须访问日志如果需要web访问链接、访问ip、访问时间打印false为关闭true开启此功能"
}

BIN
example/config/data.db Normal file

Binary file not shown.

362
example/config/rule.json Normal file
View File

@ -0,0 +1,362 @@
[
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "idcard",
"strict": false,
"type": ""
},
{
"edit": false,
"info": true,
"list": true,
"must": false,
"name": "id",
"strict": true,
"type": ""
},
{
"edit": true,
"info": true,
"list": true,
"must": false,
"name": "parent_id",
"strict": true,
"type": ""
},
{
"edit": true,
"info": true,
"list": true,
"must": false,
"name": "amount",
"strict": true,
"type": "money"
},
{
"edit": false,
"info": false,
"list": false,
"must": false,
"name": "content",
"strict": false,
"type": "textArea"
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "info",
"strict": false,
"type": "textArea"
},
{
"edit": true,
"info": true,
"list": true,
"must": false,
"name": "status",
"strict": false,
"type": "select"
},
{
"edit": true,
"info": true,
"list": true,
"must": false,
"name": "state",
"strict": false,
"type": "select"
},
{
"edit": true,
"info": true,
"list": true,
"must": false,
"name": "sex",
"strict": false,
"type": "select"
},
{
"edit": false,
"info": false,
"list": false,
"must": false,
"name": "delete",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "lat",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "lng",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "latitude",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "longitude",
"strict": false,
"type": ""
},
{
"edit": false,
"info": false,
"list": false,
"must": false,
"name": "index",
"strict": false,
"type": "index"
},
{
"edit": true,
"info": false,
"list": false,
"must": false,
"name": "password",
"strict": false,
"type": "password"
},
{
"edit": true,
"info": false,
"list": false,
"must": false,
"name": "pwd",
"strict": false,
"type": "password"
},
{
"edit": false,
"info": false,
"list": false,
"must": false,
"name": "version",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "seq",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "sort",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "note",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "description",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "abstract",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "content",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": true,
"must": false,
"name": "address",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "full_name",
"strict": false,
"type": ""
},
{
"edit": false,
"info": true,
"list": false,
"must": false,
"name": "create_time",
"strict": true,
"type": "time"
},
{
"edit": false,
"info": true,
"list": true,
"must": false,
"name": "modify_time",
"strict": true,
"type": "time"
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "image",
"strict": false,
"type": "image"
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "img",
"strict": false,
"type": "image"
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "icon",
"strict": false,
"type": "image"
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "avatar",
"strict": false,
"type": "image"
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "file",
"strict": false,
"type": "file"
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "age",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": false,
"must": false,
"name": "email",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": true,
"must": false,
"name": "time",
"strict": false,
"type": "time"
},
{
"edit": false,
"info": true,
"list": false,
"must": false,
"name": "level",
"strict": false,
"type": ""
},
{
"edit": true,
"info": true,
"list": true,
"must": false,
"name": "rule",
"strict": false,
"type": "form"
},
{
"edit": false,
"info": true,
"list": true,
"must": false,
"name": "table",
"strict": false,
"type": "table"
},
{
"edit": false,
"info": true,
"list": true,
"must": false,
"name": "table_id",
"strict": false,
"type": "table_id"
}
]

27
example/main.go Normal file
View File

@ -0,0 +1,27 @@
package main
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
"fmt"
)
func main() {
appIns := Init("config/config.json")
appIns.Run(Router{"app": Proj{
"user": Ctr{
"test": func(that *Context) {
fmt.Println("dasdasd")
id := that.Db.Insert("user", Map{"name": "test"})
ok := that.Db.Update("user", Map{"name": "test1"}, Map{"name": "test"})
ps := that.Db.Select("user", "*")
p := that.Db.Get("user", "*")
row := that.Db.Delete("user", Map{"id": id})
that.Display(0, Slice{id, ok, ps, p, row})
},
},
},
})
}

View File

@ -82,10 +82,23 @@ func findCaller(skip int) string {
for i := 0; i < 10; i++ {
file, line = getCaller(skip + i)
if !strings.HasPrefix(file, "logrus") {
if file == "common/error.go" {
file, line = getCaller(skip + i + 1)
j := 0
for true {
j++
if file == "common/error.go" {
file, line = getCaller(skip + i + j)
}
if file == "db/hotimedb.go" {
file, line = getCaller(skip + i + j)
}
//if file == "code/makecode.go" {
// file, line = getCaller(skip + i + j)
//}
if j == 5 {
break
}
}
break
}
}