增加log文件存取,以及增加LOG函数

This commit is contained in:
2020-02-22 00:13:41 +08:00
parent ab294ca6f0
commit bb2311148e
10 changed files with 148 additions and 123 deletions
+15 -26
View File
@@ -4,17 +4,13 @@ import (
"bytes"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type Application struct {
@@ -106,8 +102,9 @@ func (this *Application) Run(router Router) {
//异常处理
defer func() {
if err := recover(); err != nil {
this.SetError(errors.New(fmt.Sprint(err)), LOG_FMT)
//log.Println(err)
//this.SetError(errors.New(fmt.Sprint(err)), LOG_FMT)
logFmt(err, 2, LOG_ERROR)
this.Run(router)
}
}()
@@ -126,7 +123,7 @@ func (this *Application) Run(router Router) {
//启动服务
this.Server.Addr = ":" + this.Port
err := this.Server.ListenAndServe()
log.Println(err)
logFmt(err, 2)
ch <- 1
}()
@@ -138,19 +135,19 @@ func (this *Application) Run(router Router) {
//启动服务
this.Server.Addr = ":" + this.TLSPort
err := this.Server.ListenAndServeTLS(this.Config.GetString("tlsCert"), this.Config.GetString("tlsKey"))
log.Println(err)
logFmt(err, 2)
ch <- 2
}()
} else {
log.Println("没有端口启用")
logFmt("没有端口启用", 2, LOG_INFO)
return
}
value := <-ch
log.Println("启动服务失败 : ", value)
logFmt("启动服务失败 : "+ObjToStr(value), 2, LOG_ERROR)
}
//启动实例
@@ -217,11 +214,11 @@ func (this *Application) SetConfig(configPath ...string) {
cmap.JsonToMap(string(btes), &this.Error)
for k, v := range cmap {
this.Config.Put(k, v) //程序配置
Config.Put(k, v) //系统配置
this.Config[k] = v //程序配置
Config[k] = v //系统配置
}
} else {
log.Println("配置文件不存在,或者配置出错,使用缺省默认配置")
logFmt("配置文件不存在,或者配置出错,使用缺省默认配置", 2)
}
@@ -238,21 +235,13 @@ func (this *Application) SetConfig(configPath ...string) {
//写入配置说明
var configNoteByte bytes.Buffer
json.Indent(&configNoteByte, []byte(ConfigNote.ToJsonString()), "", "\t")
ioutil.WriteFile(filepath.Dir(this.configPath)+"/confignote.json", configNoteByte.Bytes(), os.ModeAppend)
//写入配置
os.MkdirAll(filepath.Dir(this.configPath), os.ModeDir)
err = ioutil.WriteFile(this.configPath, configByte.Bytes(), os.ModeAppend)
if err != nil {
os.MkdirAll(filepath.Dir(this.configPath), os.ModeDir)
os.Create(this.configPath)
err = ioutil.WriteFile(this.configPath, configByte.Bytes(), os.ModeAppend)
if err != nil {
this.Error.SetError(err)
}
//写入配置说明
os.Create(filepath.Dir(this.configPath) + "/confignote.json")
ioutil.WriteFile(filepath.Dir(this.configPath)+"/confignote.json", configNoteByte.Bytes(), os.ModeAppend)
this.Error.SetError(err)
}
ioutil.WriteFile(filepath.Dir(this.configPath)+"/confignote.json", configNoteByte.Bytes(), os.ModeAppend)
}
@@ -338,7 +327,7 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
this.crossDomain(&context)
//是否展示日志
if this.Config.GetInt("connectLogShow") != 0 {
log.Println(context.HandlerStr + time.Now().Format(" 2006-01-02 15:04 ") + Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":")))
logFmt(Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":"))+" "+context.HandlerStr, 0, LOG_INFO)
}
//访问拦截true继续false暂停