修改日志格式ïï,增加时间统计

master
zhangshunping 5 years ago
parent 198c642f25
commit 4268d71896

@ -22,6 +22,7 @@ import (
"objectss/handler"
"objectss/utils"
"sync"
"time"
)
// obsCmd represents the obs command
@ -60,6 +61,8 @@ var uploadgitCmd = &cobra.Command{
Short: "huawei cloud obs upaloadgit ",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
startT:=time.Now()
utils.Log.Infof("Staring,开始迁移任务.")
wg := new(sync.WaitGroup) //需要值引用
var ch = make(chan string, ChannelCap)
// 初始化databse
@ -73,6 +76,7 @@ var uploadgitCmd = &cobra.Command{
go handler.Consumer(ch, dbw, wg, ObjectStorgeLink)
}
wg.Wait()
utils.Log.Info("Ending,消费者消费的gitpath个数为:", handler.ComsuNum)
eT := time.Since(startT)
utils.Log.Infof("Ending,消费者消费的gitpath个数为:%d,耗时:%v", handler.ComsuNum,eT)
},
}

@ -3,6 +3,7 @@ module objectss
go 1.14
require (
github.com/antonfisher/nested-logrus-formatter v1.1.0
github.com/go-sql-driver/mysql v1.5.0
github.com/mitchellh/go-homedir v1.1.0
github.com/sirupsen/logrus v1.2.0

@ -17,6 +17,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/antonfisher/nested-logrus-formatter v1.1.0 h1:wb5SkAtQD/VMTOkYimj8PtdNvbNEs0QWOQXSZAw/Ars=
github.com/antonfisher/nested-logrus-formatter v1.1.0/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=

@ -22,6 +22,11 @@ func Productor(channel chan string, dbw *database.DbWorker) {
// 消费者负责执行shell
func Consumer(channel chan string, dbw *database.DbWorker, wg *sync.WaitGroup, osslink string) {
//errOut :=*utils.Log
//logfile,_:=os.OpenFile("logs/objectss_err.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
//errOut.SetOutput(logfile)
for {
path, ok := <-channel // 此处会阻塞, 如果信道中没有数据的话
if ok {
@ -35,12 +40,12 @@ func Consumer(channel chan string, dbw *database.DbWorker, wg *sync.WaitGroup, o
fmt.Println("shell:", copyExec)
s, err := Exec_linux_shell(copyExec)
utils.Log.Info("copy exec_shell: ", s, " err", err)
// 拷贝完成后,更新数据 oss=1
if err == nil {
dbw.UpdateRepositoryOssbyPath(path)
} else {
utils.Log.Errorf("objectStore upload Failedcommad %s Git Path %s:shell exec return %", copyExec, path, s, " err", err)
utils.Log.Errorf("对象存储命令行执行失败,错误为: %s ; 执行命令为: %s ; 命令返回值: %s ",err,copyExec,s )
}
}

@ -4,8 +4,8 @@ import (
"github.com/sirupsen/logrus"
"os"
nested "github.com/antonfisher/nested-logrus-formatter"
"time"
)
var Log = logrus.New()
@ -17,9 +17,18 @@ func init() {
Log.SetOutput(logfile)
// 为当前logrus实例设置消息输出格式为json格式.
// 同样地,也可以单独为某个logrus实例设置日志级别和hook,这里不详细叙述.
Log.Formatter = &logrus.JSONFormatter{}
// Log.Formatter = &logrus.JSONFormatter{}
//Log.Formatter = &logrus.TextFormatter{}
Log.SetFormatter(&nested.Formatter{
HideKeys: true,
TimestampFormat: time.RFC3339,
TrimMessages: true,
NoColors: true,
})
// 日志等级
Log.SetLevel(logrus.InfoLevel)
// 日志输出 执行的程序函数名和路径
Log.SetReportCaller(true)
}
@ -28,3 +37,4 @@ func init() {

Loading…
Cancel
Save