Try to abstract the main part.
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eyebluecn/tank/code/logger"
|
"github.com/eyebluecn/tank/code/tool/inter"
|
||||||
"github.com/eyebluecn/tank/code/tool/util"
|
"github.com/eyebluecn/tank/code/tool/util"
|
||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -65,7 +65,7 @@ type ConfigItem struct {
|
|||||||
func (this *ConfigItem) validate() bool {
|
func (this *ConfigItem) validate() bool {
|
||||||
|
|
||||||
if this.ServerPort == 0 {
|
if this.ServerPort == 0 {
|
||||||
logger.LOGGER.Error("ServerPort 未配置")
|
inter.LOGGER.Error("ServerPort 未配置")
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
//只要配置文件中有配置端口,就使用。
|
//只要配置文件中有配置端口,就使用。
|
||||||
@ -73,27 +73,27 @@ func (this *ConfigItem) validate() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlUsername == "" {
|
if this.MysqlUsername == "" {
|
||||||
logger.LOGGER.Error("MysqlUsername 未配置")
|
inter.LOGGER.Error("MysqlUsername 未配置")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlPassword == "" {
|
if this.MysqlPassword == "" {
|
||||||
logger.LOGGER.Error("MysqlPassword 未配置")
|
inter.LOGGER.Error("MysqlPassword 未配置")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlHost == "" {
|
if this.MysqlHost == "" {
|
||||||
logger.LOGGER.Error("MysqlHost 未配置")
|
inter.LOGGER.Error("MysqlHost 未配置")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlPort == 0 {
|
if this.MysqlPort == 0 {
|
||||||
logger.LOGGER.Error("MysqlPort 未配置")
|
inter.LOGGER.Error("MysqlPort 未配置")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlSchema == "" {
|
if this.MysqlSchema == "" {
|
||||||
logger.LOGGER.Error("MysqlSchema 未配置")
|
inter.LOGGER.Error("MysqlSchema 未配置")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,14 +135,14 @@ func (this *Config) ReadFromConfigFile() {
|
|||||||
filePath := util.GetConfPath() + "/tank.json"
|
filePath := util.GetConfPath() + "/tank.json"
|
||||||
content, err := ioutil.ReadFile(filePath)
|
content, err := ioutil.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LOGGER.Warn("无法找到配置文件:%s 即将进入安装过程!", filePath)
|
inter.LOGGER.Warn("无法找到配置文件:%s 即将进入安装过程!", filePath)
|
||||||
this.Installed = false
|
this.Installed = false
|
||||||
} else {
|
} else {
|
||||||
this.Item = &ConfigItem{}
|
this.Item = &ConfigItem{}
|
||||||
logger.LOGGER.Warn("读取配置文件:%s", filePath)
|
inter.LOGGER.Warn("读取配置文件:%s", filePath)
|
||||||
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(content, this.Item)
|
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(content, this.Item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LOGGER.Error("配置文件格式错误! 即将进入安装过程!")
|
inter.LOGGER.Error("配置文件格式错误! 即将进入安装过程!")
|
||||||
this.Installed = false
|
this.Installed = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -150,7 +150,7 @@ func (this *Config) ReadFromConfigFile() {
|
|||||||
//验证项是否齐全
|
//验证项是否齐全
|
||||||
itemValidate := this.Item.validate()
|
itemValidate := this.Item.validate()
|
||||||
if !itemValidate {
|
if !itemValidate {
|
||||||
logger.LOGGER.Error("配置文件信息不齐全! 即将进入安装过程!")
|
inter.LOGGER.Error("配置文件信息不齐全! 即将进入安装过程!")
|
||||||
this.Installed = false
|
this.Installed = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -171,8 +171,8 @@ func (this *Config) ReadFromConfigFile() {
|
|||||||
this.MysqlUrl = util.GetMysqlUrl(this.Item.MysqlPort, this.Item.MysqlHost, this.Item.MysqlSchema, this.Item.MysqlUsername, this.Item.MysqlPassword)
|
this.MysqlUrl = util.GetMysqlUrl(this.Item.MysqlPort, this.Item.MysqlHost, this.Item.MysqlSchema, this.Item.MysqlUsername, this.Item.MysqlPassword)
|
||||||
this.Installed = true
|
this.Installed = true
|
||||||
|
|
||||||
logger.LOGGER.Info("使用配置文件:%s", filePath)
|
inter.LOGGER.Info("使用配置文件:%s", filePath)
|
||||||
logger.LOGGER.Info("上传文件存放路径:%s", this.MatterPath)
|
inter.LOGGER.Info("上传文件存放路径:%s", this.MatterPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ func (this *AlienService) PreviewOrDownload(
|
|||||||
}
|
}
|
||||||
|
|
||||||
//文件下载次数加一,为了加快访问速度,异步进行
|
//文件下载次数加一,为了加快访问速度,异步进行
|
||||||
go util.SafeMethod(func() {
|
go util.RunWithRecovery(func() {
|
||||||
this.matterDao.TimesIncrement(uuid)
|
this.matterDao.TimesIncrement(uuid)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ package rest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eyebluecn/tank/code/config"
|
"github.com/eyebluecn/tank/code/config"
|
||||||
"github.com/eyebluecn/tank/code/logger"
|
"github.com/eyebluecn/tank/code/tool/inter"
|
||||||
"github.com/eyebluecn/tank/code/tool/result"
|
"github.com/eyebluecn/tank/code/tool/result"
|
||||||
"github.com/eyebluecn/tank/code/tool/util"
|
"github.com/eyebluecn/tank/code/tool/util"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -20,11 +20,11 @@ type IBean interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Bean struct {
|
type Bean struct {
|
||||||
logger *logger.Logger
|
logger inter.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Bean) Init() {
|
func (this *Bean) Init() {
|
||||||
this.logger = logger.LOGGER
|
this.logger = inter.LOGGER
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Bean) Bootstrap() {
|
func (this *Bean) Bootstrap() {
|
||||||
@ -59,6 +59,7 @@ func (this *Bean) findUser(writer http.ResponseWriter, request *http.Request) *U
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cacheItem == nil || cacheItem.Data() == nil {
|
if cacheItem == nil || cacheItem.Data() == nil {
|
||||||
|
|
||||||
this.logger.Warn("cache item中已经不存在了 ")
|
this.logger.Warn("cache item中已经不存在了 ")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,8 @@ package rest
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/eyebluecn/tank/code/config"
|
"github.com/eyebluecn/tank/code/config"
|
||||||
"github.com/eyebluecn/tank/code/logger"
|
|
||||||
cache2 "github.com/eyebluecn/tank/code/tool/cache"
|
cache2 "github.com/eyebluecn/tank/code/tool/cache"
|
||||||
|
"github.com/eyebluecn/tank/code/tool/inter"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
@ -56,7 +56,7 @@ func (this *Context) OpenDb() {
|
|||||||
this.DB, err = gorm.Open("mysql", config.CONFIG.MysqlUrl)
|
this.DB, err = gorm.Open("mysql", config.CONFIG.MysqlUrl)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LOGGER.Panic("failed to connect mysql database")
|
inter.LOGGER.Panic("failed to connect mysql database")
|
||||||
}
|
}
|
||||||
|
|
||||||
//是否打开sql日志(在调试阶段可以打开,以方便查看执行的SQL)
|
//是否打开sql日志(在调试阶段可以打开,以方便查看执行的SQL)
|
||||||
@ -68,7 +68,7 @@ func (this *Context) CloseDb() {
|
|||||||
if this.DB != nil {
|
if this.DB != nil {
|
||||||
err := this.DB.Close()
|
err := this.DB.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("关闭数据库连接出错", err)
|
inter.LOGGER.Error("关闭数据库连接出错 %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ func (this *Context) registerBean(bean IBean) {
|
|||||||
|
|
||||||
err := fmt.Sprintf("【%s】已经被注册了,跳过。", typeName)
|
err := fmt.Sprintf("【%s】已经被注册了,跳过。", typeName)
|
||||||
if _, ok := this.BeanMap[typeName]; ok {
|
if _, ok := this.BeanMap[typeName]; ok {
|
||||||
logger.LOGGER.Error(fmt.Sprintf(err))
|
inter.LOGGER.Error(fmt.Sprintf(err))
|
||||||
} else {
|
} else {
|
||||||
this.BeanMap[typeName] = element
|
this.BeanMap[typeName] = element
|
||||||
|
|
||||||
@ -95,7 +95,7 @@ func (this *Context) registerBean(bean IBean) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
logger.LOGGER.Panic("注册的【%s】不是Bean类型。", typeName)
|
inter.LOGGER.Panic("注册的【%s】不是Bean类型。", typeName)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -165,7 +165,7 @@ func (this *Context) GetBean(bean IBean) IBean {
|
|||||||
if val, ok := this.BeanMap[typeName]; ok {
|
if val, ok := this.BeanMap[typeName]; ok {
|
||||||
return val
|
return val
|
||||||
} else {
|
} else {
|
||||||
logger.LOGGER.Panic("【%s】没有注册。", typeName)
|
inter.LOGGER.Panic("【%s】没有注册。", typeName)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ func (this *DashboardService) Bootstrap() {
|
|||||||
this.logger.Info("[cron job] 每日00:05清洗离线数据")
|
this.logger.Info("[cron job] 每日00:05清洗离线数据")
|
||||||
|
|
||||||
//立即执行一次数据清洗任务
|
//立即执行一次数据清洗任务
|
||||||
go util.SafeMethod(this.etl)
|
go util.RunWithRecovery(this.etl)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ func (this *FootprintService) Bootstrap() {
|
|||||||
this.logger.Info("[cron job] 每日00:10 删除8日之前的访问数据")
|
this.logger.Info("[cron job] 每日00:10 删除8日之前的访问数据")
|
||||||
|
|
||||||
//立即执行一次数据清洗任务
|
//立即执行一次数据清洗任务
|
||||||
go util.SafeMethod(this.cleanOldData)
|
go util.RunWithRecovery(this.cleanOldData)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ package rest
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/eyebluecn/tank/code/config"
|
"github.com/eyebluecn/tank/code/config"
|
||||||
"github.com/eyebluecn/tank/code/logger"
|
"github.com/eyebluecn/tank/code/tool/inter"
|
||||||
"github.com/eyebluecn/tank/code/tool/result"
|
"github.com/eyebluecn/tank/code/tool/result"
|
||||||
"github.com/eyebluecn/tank/code/tool/util"
|
"github.com/eyebluecn/tank/code/tool/util"
|
||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
@ -72,7 +72,7 @@ func NewRouter() *Router {
|
|||||||
func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http.Request, startTime time.Time) {
|
func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http.Request, startTime time.Time) {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
|
|
||||||
logger.LOGGER.Error("错误: %v", err)
|
inter.LOGGER.Error("错误: %v", err)
|
||||||
|
|
||||||
var webResult *result.WebResult = nil
|
var webResult *result.WebResult = nil
|
||||||
if value, ok := err.(string); ok {
|
if value, ok := err.(string); ok {
|
||||||
@ -108,7 +108,7 @@ func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http
|
|||||||
}
|
}
|
||||||
|
|
||||||
//错误情况记录。
|
//错误情况记录。
|
||||||
go util.SafeMethod(func() {
|
go util.RunWithRecovery(func() {
|
||||||
this.footprintService.Trace(writer, request, time.Now().Sub(startTime), false)
|
this.footprintService.Trace(writer, request, time.Now().Sub(startTime), false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -156,7 +156,7 @@ func (this *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//正常的访问记录会落到这里。
|
//正常的访问记录会落到这里。
|
||||||
go util.SafeMethod(func() {
|
go util.RunWithRecovery(func() {
|
||||||
this.footprintService.Trace(writer, request, time.Now().Sub(startTime), true)
|
this.footprintService.Trace(writer, request, time.Now().Sub(startTime), true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package logger
|
package support
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/eyebluecn/tank/code/tool/util"
|
"github.com/eyebluecn/tank/code/tool/util"
|
||||||
|
"github.com/robfig/cron"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -10,12 +11,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//日志系统必须高保
|
|
||||||
//全局唯一的日志对象(在main函数中初始化)
|
|
||||||
var LOGGER = &Logger{}
|
|
||||||
|
|
||||||
//在Logger的基础上包装一个全新的Logger.
|
//在Logger的基础上包装一个全新的Logger.
|
||||||
type Logger struct {
|
type TankLogger struct {
|
||||||
//加锁,在维护日志期间,禁止写入日志。
|
//加锁,在维护日志期间,禁止写入日志。
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
|
|
||||||
@ -23,12 +20,28 @@ type Logger struct {
|
|||||||
goLogger *log.Logger
|
goLogger *log.Logger
|
||||||
//日志记录所在的文件
|
//日志记录所在的文件
|
||||||
file *os.File
|
file *os.File
|
||||||
//每天凌晨定时整理器
|
}
|
||||||
maintainTimer *time.Timer
|
|
||||||
|
func (this *TankLogger) Init() {
|
||||||
|
|
||||||
|
this.openFile()
|
||||||
|
|
||||||
|
//每日00:00整理日志。
|
||||||
|
expression := "0 0 0 * * ?"
|
||||||
|
cronJob := cron.New()
|
||||||
|
err := cronJob.AddFunc(expression, this.maintain)
|
||||||
|
util.PanicError(err)
|
||||||
|
cronJob.Start()
|
||||||
|
this.Info("[cron job] 每日00:00维护日志")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *TankLogger) Destroy() {
|
||||||
|
this.closeFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
//处理日志的统一方法。
|
//处理日志的统一方法。
|
||||||
func (this *Logger) log(prefix string, format string, v ...interface{}) {
|
func (this *TankLogger) Log(prefix string, format string, v ...interface{}) {
|
||||||
|
|
||||||
content := fmt.Sprintf(format+"\r\n", v...)
|
content := fmt.Sprintf(format+"\r\n", v...)
|
||||||
|
|
||||||
@ -52,44 +65,29 @@ func (this *Logger) log(prefix string, format string, v ...interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//处理日志的统一方法。
|
//处理日志的统一方法。
|
||||||
func (this *Logger) Debug(format string, v ...interface{}) {
|
func (this *TankLogger) Debug(format string, v ...interface{}) {
|
||||||
this.log("[DEBUG]", format, v...)
|
this.Log("[DEBUG]", format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Logger) Info(format string, v ...interface{}) {
|
func (this *TankLogger) Info(format string, v ...interface{}) {
|
||||||
this.log("[INFO ]", format, v...)
|
this.Log("[INFO ]", format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Logger) Warn(format string, v ...interface{}) {
|
func (this *TankLogger) Warn(format string, v ...interface{}) {
|
||||||
this.log("[WARN ]", format, v...)
|
this.Log("[WARN ]", format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Logger) Error(format string, v ...interface{}) {
|
func (this *TankLogger) Error(format string, v ...interface{}) {
|
||||||
this.log("[ERROR]", format, v...)
|
this.Log("[ERROR]", format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Logger) Panic(format string, v ...interface{}) {
|
func (this *TankLogger) Panic(format string, v ...interface{}) {
|
||||||
this.log("[PANIC]", format, v...)
|
this.Log("[PANIC]", format, v...)
|
||||||
panic(fmt.Sprintf(format, v...))
|
panic(fmt.Sprintf(format, v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Logger) Init() {
|
|
||||||
|
|
||||||
this.openFile()
|
|
||||||
|
|
||||||
//日志需要自我备份,自我维护。明天第一秒触发
|
|
||||||
nextTime := util.FirstSecondOfDay(util.Tomorrow())
|
|
||||||
duration := nextTime.Sub(time.Now())
|
|
||||||
|
|
||||||
this.Info("下一次日志维护时间%s 距当前 %ds ", util.ConvertTimeToDateTimeString(nextTime), duration/time.Second)
|
|
||||||
this.maintainTimer = time.AfterFunc(duration, func() {
|
|
||||||
go util.SafeMethod(this.maintain)
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//将日志写入到今天的日期中(该方法内必须使用异步方法记录日志,否则会引发死锁)
|
//将日志写入到今天的日期中(该方法内必须使用异步方法记录日志,否则会引发死锁)
|
||||||
func (this *Logger) maintain() {
|
func (this *TankLogger) maintain() {
|
||||||
|
|
||||||
this.Info("每日维护日志")
|
this.Info("每日维护日志")
|
||||||
|
|
||||||
@ -100,7 +98,7 @@ func (this *Logger) maintain() {
|
|||||||
this.closeFile()
|
this.closeFile()
|
||||||
|
|
||||||
//日志归类到昨天
|
//日志归类到昨天
|
||||||
destPath := util.GetLogPath() + "/tank-" + util.Yesterday().Local().Format("2006-01-02") + ".log"
|
destPath := util.GetLogPath() + "/tank-" + util.ConvertTimeToDateString(util.Yesterday()) + ".log"
|
||||||
|
|
||||||
//直接重命名文件
|
//直接重命名文件
|
||||||
err := os.Rename(this.fileName(), destPath)
|
err := os.Rename(this.fileName(), destPath)
|
||||||
@ -111,23 +109,33 @@ func (this *Logger) maintain() {
|
|||||||
//再次打开文件
|
//再次打开文件
|
||||||
this.openFile()
|
this.openFile()
|
||||||
|
|
||||||
//准备好下次维护日志的时间。
|
//删除一个月之前的日志文件。
|
||||||
now := time.Now()
|
monthAgo := time.Now()
|
||||||
nextTime := util.FirstSecondOfDay(util.Tomorrow())
|
monthAgo = monthAgo.AddDate(0, -1, 0)
|
||||||
duration := nextTime.Sub(now)
|
oldDestPath := util.GetLogPath() + "/tank-" + util.ConvertTimeToDateString(monthAgo) + ".log"
|
||||||
this.Info("下次维护时间:%s ", util.ConvertTimeToDateTimeString(nextTime))
|
this.Log("删除日志文件 %s", oldDestPath)
|
||||||
this.maintainTimer = time.AfterFunc(duration, func() {
|
|
||||||
go util.SafeMethod(this.maintain)
|
//删除文件
|
||||||
})
|
exists, err := util.PathExists(oldDestPath)
|
||||||
|
util.PanicError(err)
|
||||||
|
if exists {
|
||||||
|
err = os.Remove(oldDestPath)
|
||||||
|
if err != nil {
|
||||||
|
this.Error("删除磁盘上的文件%s 出错 %s", oldDestPath, err.Error())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.Error("日志文件 %s 不存在,无需删除", oldDestPath)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//日志名称
|
//日志名称
|
||||||
func (this *Logger) fileName() string {
|
func (this *TankLogger) fileName() string {
|
||||||
return util.GetLogPath() + "/tank.log"
|
return util.GetLogPath() + "/tank.log"
|
||||||
}
|
}
|
||||||
|
|
||||||
//打开日志文件
|
//打开日志文件
|
||||||
func (this *Logger) openFile() {
|
func (this *TankLogger) openFile() {
|
||||||
//日志输出到文件中 文件打开后暂时不关闭
|
//日志输出到文件中 文件打开后暂时不关闭
|
||||||
fmt.Printf("使用日志文件 %s\r\n", this.fileName())
|
fmt.Printf("使用日志文件 %s\r\n", this.fileName())
|
||||||
f, err := os.OpenFile(this.fileName(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
f, err := os.OpenFile(this.fileName(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
@ -137,11 +145,15 @@ func (this *Logger) openFile() {
|
|||||||
|
|
||||||
this.goLogger = log.New(f, "", log.Ltime|log.Lshortfile)
|
this.goLogger = log.New(f, "", log.Ltime|log.Lshortfile)
|
||||||
|
|
||||||
|
if this.goLogger == nil {
|
||||||
|
fmt.Printf("Error: cannot create goLogger \r\n")
|
||||||
|
}
|
||||||
|
|
||||||
this.file = f
|
this.file = f
|
||||||
}
|
}
|
||||||
|
|
||||||
//关闭日志文件
|
//关闭日志文件
|
||||||
func (this *Logger) closeFile() {
|
func (this *TankLogger) closeFile() {
|
||||||
if this.file != nil {
|
if this.file != nil {
|
||||||
err := this.file.Close()
|
err := this.file.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -149,13 +161,3 @@ func (this *Logger) closeFile() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Logger) Destroy() {
|
|
||||||
|
|
||||||
this.closeFile()
|
|
||||||
|
|
||||||
if this.maintainTimer != nil {
|
|
||||||
this.maintainTimer.Stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -26,6 +26,9 @@ func TestCron(t *testing.T) {
|
|||||||
err := c.AddFunc(spec, func() {
|
err := c.AddFunc(spec, func() {
|
||||||
i++
|
i++
|
||||||
log.Println("cron running:", i)
|
log.Println("cron running:", i)
|
||||||
|
if i == 2 {
|
||||||
|
panic("intent to panic.")
|
||||||
|
}
|
||||||
})
|
})
|
||||||
util.PanicError(err)
|
util.PanicError(err)
|
||||||
|
|
||||||
|
2
code/tool/cache/cache.go
vendored
2
code/tool/cache/cache.go
vendored
@ -197,7 +197,7 @@ func (table *Table) checkExpire() {
|
|||||||
table.cleanupInterval = smallestDuration
|
table.cleanupInterval = smallestDuration
|
||||||
if smallestDuration > 0 {
|
if smallestDuration > 0 {
|
||||||
table.cleanupTimer = time.AfterFunc(smallestDuration, func() {
|
table.cleanupTimer = time.AfterFunc(smallestDuration, func() {
|
||||||
go util.SafeMethod(table.checkExpire)
|
go util.RunWithRecovery(table.checkExpire)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
table.Unlock()
|
table.Unlock()
|
||||||
|
17
code/tool/inter/logger.go
Normal file
17
code/tool/inter/logger.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package inter
|
||||||
|
|
||||||
|
//日志系统必须高保
|
||||||
|
//全局唯一的日志对象(在main函数中初始化)
|
||||||
|
var LOGGER Logger
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
//处理日志的统一方法。
|
||||||
|
Log(prefix string, format string, v ...interface{})
|
||||||
|
|
||||||
|
//不同级别的日志处理
|
||||||
|
Debug(format string, v ...interface{})
|
||||||
|
Info(format string, v ...interface{})
|
||||||
|
Warn(format string, v ...interface{})
|
||||||
|
Error(format string, v ...interface{})
|
||||||
|
Panic(format string, v ...interface{})
|
||||||
|
}
|
@ -40,8 +40,7 @@ func GetDevHomePath() string {
|
|||||||
panic("cannot get dev home path.")
|
panic("cannot get dev home path.")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(file)
|
//$DevHomePath/code/tool/util/util_file.go
|
||||||
|
|
||||||
dir := GetDirOfPath(file)
|
dir := GetDirOfPath(file)
|
||||||
dir = GetDirOfPath(dir)
|
dir = GetDirOfPath(dir)
|
||||||
dir = GetDirOfPath(dir)
|
dir = GetDirOfPath(dir)
|
||||||
@ -69,10 +68,12 @@ func GetHomePath() string {
|
|||||||
|
|
||||||
//如果exPath中包含了 \\AppData\\Local\\Temp 我们认为是在Win的开发环境中
|
//如果exPath中包含了 \\AppData\\Local\\Temp 我们认为是在Win的开发环境中
|
||||||
systemUser, err := user.Current()
|
systemUser, err := user.Current()
|
||||||
|
if systemUser != nil {
|
||||||
winDev := strings.HasPrefix(exPath, systemUser.HomeDir+"\\AppData\\Local\\Temp")
|
winDev := strings.HasPrefix(exPath, systemUser.HomeDir+"\\AppData\\Local\\Temp")
|
||||||
if winDev {
|
if winDev {
|
||||||
exPath = GetDevHomePath() + "/tmp"
|
exPath = GetDevHomePath() + "/tmp"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return exPath
|
return exPath
|
||||||
}
|
}
|
@ -1,16 +1,14 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
//带有panic恢复的方法
|
//带有panic恢复的方法
|
||||||
func PanicHandler() {
|
func RunWithRecovery(f func()) {
|
||||||
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
//TODO 全局日志记录
|
//TODO 全局日志记录
|
||||||
//LOGGER.Error("异步任务错误: %v", err)
|
//LOGGER.Error("异步任务错误: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
|
|
||||||
//带有panic恢复的方法
|
|
||||||
func SafeMethod(f func()) {
|
|
||||||
defer PanicHandler()
|
|
||||||
//执行函数
|
//执行函数
|
||||||
f()
|
f()
|
||||||
}
|
}
|
||||||
|
11
main.go
11
main.go
@ -3,8 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/eyebluecn/tank/code/config"
|
"github.com/eyebluecn/tank/code/config"
|
||||||
"github.com/eyebluecn/tank/code/logger"
|
|
||||||
"github.com/eyebluecn/tank/code/rest"
|
"github.com/eyebluecn/tank/code/rest"
|
||||||
|
"github.com/eyebluecn/tank/code/support"
|
||||||
|
"github.com/eyebluecn/tank/code/tool/inter"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -13,8 +14,10 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
//日志第一优先级保障
|
//日志第一优先级保障
|
||||||
logger.LOGGER.Init()
|
tankLogger := &support.TankLogger{}
|
||||||
defer logger.LOGGER.Destroy()
|
tankLogger.Init()
|
||||||
|
defer tankLogger.Destroy()
|
||||||
|
inter.LOGGER = tankLogger
|
||||||
|
|
||||||
//装载配置文件,这个决定了是否需要执行安装过程
|
//装载配置文件,这个决定了是否需要执行安装过程
|
||||||
config.CONFIG.Init()
|
config.CONFIG.Init()
|
||||||
@ -25,7 +28,7 @@ func main() {
|
|||||||
|
|
||||||
http.Handle("/", rest.CONTEXT.Router)
|
http.Handle("/", rest.CONTEXT.Router)
|
||||||
|
|
||||||
logger.LOGGER.Info("App started at http://localhost:%v", config.CONFIG.ServerPort)
|
inter.LOGGER.Info("App started at http://localhost:%v", config.CONFIG.ServerPort)
|
||||||
|
|
||||||
dotPort := fmt.Sprintf(":%v", config.CONFIG.ServerPort)
|
dotPort := fmt.Sprintf(":%v", config.CONFIG.ServerPort)
|
||||||
err1 := http.ListenAndServe(dotPort, nil)
|
err1 := http.ListenAndServe(dotPort, nil)
|
||||||
|
Reference in New Issue
Block a user