pkg: remove sunteng log

pull/1/head
miraclesu 2017-05-12 15:38:50 +08:00
parent dcc1c421eb
commit b1e95b07bf
20 changed files with 97 additions and 79 deletions

View File

@ -6,11 +6,10 @@ import (
"flag" "flag"
"runtime" "runtime"
"sunteng/commons/log"
"github.com/shunfei/cronsun" "github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/conf" "github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/event" "github.com/shunfei/cronsun/event"
"github.com/shunfei/cronsun/log"
"github.com/shunfei/cronsun/node" "github.com/shunfei/cronsun/node"
) )
@ -25,18 +24,18 @@ func main() {
runtime.GOMAXPROCS(*gomax) runtime.GOMAXPROCS(*gomax)
if err := cronsun.Init(); err != nil { if err := cronsun.Init(); err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
return return
} }
n, err := node.NewNode(conf.Config) n, err := node.NewNode(conf.Config)
if err != nil { if err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
return return
} }
if err = n.Register(); err != nil { if err = n.Register(); err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
return return
} }
@ -45,11 +44,11 @@ func main() {
} }
if err = n.Run(); err != nil { if err = n.Run(); err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
return return
} }
log.Noticef("cronsun %s service started, Ctrl+C or send kill sign to exit", n.String()) log.Infof("cronsun %s service started, Ctrl+C or send kill sign to exit", n.String())
// 注册退出事件 // 注册退出事件
event.On(event.EXIT, n.Stop, conf.Exit, cronsun.Exit) event.On(event.EXIT, n.Stop, conf.Exit, cronsun.Exit)
// 注册监听配置更新事件 // 注册监听配置更新事件
@ -58,5 +57,5 @@ func main() {
event.Wait() event.Wait()
// 处理退出事件 // 处理退出事件
event.Emit(event.EXIT, nil) event.Emit(event.EXIT, nil)
log.Notice("exit success") log.Infof("exit success")
} }

View File

@ -6,23 +6,22 @@ import (
"github.com/cockroachdb/cmux" "github.com/cockroachdb/cmux"
"sunteng/commons/log" "github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/conf" "github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/event" "github.com/shunfei/cronsun/event"
"github.com/shunfei/cronsun" "github.com/shunfei/cronsun/log"
"github.com/shunfei/cronsun/web" "github.com/shunfei/cronsun/web"
) )
func main() { func main() {
if err := cronsun.Init(); err != nil { if err := cronsun.Init(); err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
return return
} }
l, err := net.Listen("tcp", conf.Config.Web.BindAddr) l, err := net.Listen("tcp", conf.Config.Web.BindAddr)
if err != nil { if err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
return return
} }
@ -31,7 +30,7 @@ func main() {
httpL := m.Match(cmux.HTTP1Fast()) httpL := m.Match(cmux.HTTP1Fast())
httpServer, err := web.InitRouters() httpServer, err := web.InitRouters()
if err != nil { if err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
return return
} }
@ -43,7 +42,7 @@ func main() {
} else { } else {
mailer, err := cronsun.NewMail(10 * time.Second) mailer, err := cronsun.NewMail(10 * time.Second)
if err != nil { if err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
return return
} }
noticer = mailer noticer = mailer
@ -60,11 +59,11 @@ func main() {
go m.Serve() go m.Serve()
log.Noticef("cronsun web server started on %s, Ctrl+C or send kill sign to exit", conf.Config.Web.BindAddr) log.Infof("cronsun web server started on %s, Ctrl+C or send kill sign to exit", conf.Config.Web.BindAddr)
// 注册退出事件 // 注册退出事件
event.On(event.EXIT, conf.Exit) event.On(event.EXIT, conf.Exit)
// 监听退出信号 // 监听退出信号
event.Wait() event.Wait()
event.Emit(event.EXIT, nil) event.Emit(event.EXIT, nil)
log.Notice("exit success") log.Infof("exit success")
} }

View File

@ -9,10 +9,9 @@ import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/go-gomail/gomail" "github.com/go-gomail/gomail"
"sunteng/commons/log"
"github.com/shunfei/cronsun/event"
"github.com/shunfei/cronsun/db" "github.com/shunfei/cronsun/db"
"github.com/shunfei/cronsun/event"
"github.com/shunfei/cronsun/log"
"github.com/shunfei/cronsun/utils" "github.com/shunfei/cronsun/utils"
) )
@ -64,7 +63,6 @@ type Conf struct {
// 默认 300 // 默认 300
LockTtl int64 LockTtl int64
Log *log.Config
Etcd client.Config Etcd client.Config
Mgo *db.Config Mgo *db.Config
Web webConfig Web webConfig
@ -136,7 +134,6 @@ func (c *Conf) parse() error {
} else { } else {
c.Mgo.Timeout *= time.Second c.Mgo.Timeout *= time.Second
} }
log.InitConf(c.Log)
c.Node = cleanKeyPrefix(c.Node) c.Node = cleanKeyPrefix(c.Node)
c.Proc = cleanKeyPrefix(c.Proc) c.Proc = cleanKeyPrefix(c.Proc)
@ -193,7 +190,7 @@ func (c *Conf) watch() error {
func (c *Conf) reload() { func (c *Conf) reload() {
cf := new(Conf) cf := new(Conf)
if err := cf.parse(); err != nil { if err := cf.parse(); err != nil {
log.Warn("config file reload err:", err.Error()) log.Warnf("config file reload err: %s", err.Error())
return return
} }
@ -201,7 +198,7 @@ func (c *Conf) reload() {
cf.Node, cf.Proc, cf.Cmd, cf.Once, cf.Lock, cf.Group, cf.Noticer = c.Node, c.Proc, c.Cmd, c.Once, c.Lock, c.Group, c.Noticer cf.Node, cf.Proc, cf.Cmd, cf.Once, cf.Lock, cf.Group, cf.Noticer = c.Node, c.Proc, c.Cmd, c.Once, c.Lock, c.Group, c.Noticer
*c = *cf *c = *cf
log.Noticef("config file[%s] reload success", *confFile) log.Infof("config file[%s] reload success", *confFile)
return return
} }

View File

@ -17,7 +17,6 @@
"ProcReq": 5, "ProcReq": 5,
"#LockTtl": "任务锁最大过期时间,单位秒,默认 600", "#LockTtl": "任务锁最大过期时间,单位秒,默认 600",
"LockTtl": 600, "LockTtl": 600,
"Log": "@extend:log.json",
"Etcd": "@extend:etcd.json", "Etcd": "@extend:etcd.json",
"Mgo": "@extend:db.json", "Mgo": "@extend:db.json",
"Mail": "@extend:mail.json", "Mail": "@extend:mail.json",

View File

@ -1,15 +0,0 @@
{
"Appenders":{
"Node":{
"Type":"file",
"Target":"/data/logs/cronsun/node.log"
},
"stdout":{
"Type":"console"
}
},
"Root":{
"Level":"DEBUG",
"Appenders":["stdout", "Node"]
}
}

View File

@ -8,8 +8,8 @@ import (
client "github.com/coreos/etcd/clientv3" client "github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb" "github.com/coreos/etcd/mvcc/mvccpb"
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf" "github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
) )
// 结点类型分组 // 结点类型分组

6
job.go
View File

@ -18,8 +18,8 @@ import (
client "github.com/coreos/etcd/clientv3" client "github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb" "github.com/coreos/etcd/mvcc/mvccpb"
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf" "github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
"github.com/shunfei/cronsun/node/cron" "github.com/shunfei/cronsun/node/cron"
) )
@ -244,13 +244,13 @@ func (c *Cmd) lock() *locker {
resp, err := DefalutClient.Grant(lk.ttl) resp, err := DefalutClient.Grant(lk.ttl)
if err != nil { if err != nil {
log.Noticef("job[%s] didn't get a lock, err: %s", c.Job.Key(), err.Error()) log.Infof("job[%s] didn't get a lock, err: %s", c.Job.Key(), err.Error())
return nil return nil
} }
ok, err := DefalutClient.GetLock(c.Job.ID, resp.ID) ok, err := DefalutClient.GetLock(c.Job.ID, resp.ID)
if err != nil { if err != nil {
log.Noticef("job[%s] didn't get a lock, err: %s", c.Job.Key(), err.Error()) log.Infof("job[%s] didn't get a lock, err: %s", c.Job.Key(), err.Error())
return nil return nil
} }

View File

@ -6,7 +6,7 @@ import (
"gopkg.in/mgo.v2" "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
"sunteng/commons/log" "github.com/shunfei/cronsun/log"
) )
const ( const (
@ -103,7 +103,7 @@ func CreateJobLog(j *Job, t time.Time, rs string, success bool) {
EndTime: et, EndTime: et,
} }
if err := mgoDB.Insert(Coll_JobLog, jl); err != nil { if err := mgoDB.Insert(Coll_JobLog, jl); err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
} }
latestLog := &JobLatestLog{ latestLog := &JobLatestLog{
@ -112,7 +112,7 @@ func CreateJobLog(j *Job, t time.Time, rs string, success bool) {
} }
latestLog.Id = "" latestLog.Id = ""
if err := mgoDB.Upsert(Coll_JobLatestLog, bson.M{"node": jl.Node, "jobId": jl.JobId, "jobGroup": jl.JobGroup}, latestLog); err != nil { if err := mgoDB.Upsert(Coll_JobLatestLog, bson.M{"node": jl.Node, "jobId": jl.JobId, "jobGroup": jl.JobGroup}, latestLog); err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
} }
var inc = bson.M{"total": 1} var inc = bson.M{"total": 1}
@ -124,11 +124,11 @@ func CreateJobLog(j *Job, t time.Time, rs string, success bool) {
err := mgoDB.Upsert(Coll_Stat, bson.M{"name": "job-day", "date": time.Now().Format("2006-01-02")}, bson.M{"$inc": inc}) err := mgoDB.Upsert(Coll_Stat, bson.M{"name": "job-day", "date": time.Now().Format("2006-01-02")}, bson.M{"$inc": inc})
if err != nil { if err != nil {
log.Error("increase stat.job ", err.Error()) log.Errorf("increase stat.job %s", err.Error())
} }
err = mgoDB.Upsert(Coll_Stat, bson.M{"name": "job"}, bson.M{"$inc": inc}) err = mgoDB.Upsert(Coll_Stat, bson.M{"name": "job"}, bson.M{"$inc": inc})
if err != nil { if err != nil {
log.Error("increase stat.job ", err.Error()) log.Errorf("increase stat.job %s", err.Error())
} }
} }

40
log/log.go Normal file
View File

@ -0,0 +1,40 @@
package log
var (
DefaultLogger Logger
)
type Logger interface {
Debugf(format string, v ...interface{})
Infof(format string, v ...interface{})
Warnf(format string, v ...interface{})
Errorf(format string, v ...interface{})
}
func SetLogger(l Logger) {
DefaultLogger = l
}
func Debugf(format string, v ...interface{}) {
if DefaultLogger != nil {
DefaultLogger.Debugf(format, v...)
}
}
func Infof(format string, v ...interface{}) {
if DefaultLogger != nil {
DefaultLogger.Infof(format, v...)
}
}
func Warnf(format string, v ...interface{}) {
if DefaultLogger != nil {
DefaultLogger.Warnf(format, v...)
}
}
func Errorf(format string, v ...interface{}) {
if DefaultLogger != nil {
DefaultLogger.Errorf(format, v...)
}
}

View File

@ -9,11 +9,11 @@ import (
"time" "time"
client "github.com/coreos/etcd/clientv3" client "github.com/coreos/etcd/clientv3"
mgo "gopkg.in/mgo.v2" "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf" "github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
) )
const ( const (
@ -129,7 +129,7 @@ func WatchNode() client.WatchChan {
func (n *Node) On() { func (n *Node) On() {
n.Alived, n.Version, n.UpTime = true, Version, time.Now() n.Alived, n.Version, n.UpTime = true, Version, time.Now()
if err := mgoDB.Upsert(Coll_Node, bson.M{"_id": n.ID}, n); err != nil { if err := mgoDB.Upsert(Coll_Node, bson.M{"_id": n.ID}, n); err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
} }
} }
@ -137,6 +137,6 @@ func (n *Node) On() {
func (n *Node) Down() { func (n *Node) Down() {
n.Alived, n.DownTime = false, time.Now() n.Alived, n.DownTime = false, time.Now()
if err := mgoDB.Upsert(Coll_Node, bson.M{"_id": n.ID}, n); err != nil { if err := mgoDB.Upsert(Coll_Node, bson.M{"_id": n.ID}, n); err != nil {
log.Error(err.Error()) log.Errorf(err.Error())
} }
} }

View File

@ -47,7 +47,7 @@ func TestRange(t *testing.T) {
t.Errorf("%s => expected %v, got %v", c.expr, c.err, err) t.Errorf("%s => expected %v, got %v", c.expr, c.err, err)
} }
if len(c.err) == 0 && err != nil { if len(c.err) == 0 && err != nil {
t.Error("%s => unexpected error %v", c.expr, err) t.Errorf("%s => unexpected error %v", c.expr, err)
} }
if actual != c.expected { if actual != c.expected {
t.Errorf("%s => expected %d, got %d", c.expr, c.expected, actual) t.Errorf("%s => expected %d, got %d", c.expr, c.expected, actual)

View File

@ -8,10 +8,9 @@ import (
client "github.com/coreos/etcd/clientv3" client "github.com/coreos/etcd/clientv3"
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun" "github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
"github.com/shunfei/cronsun/node/cron" "github.com/shunfei/cronsun/node/cron"
"github.com/shunfei/cronsun/utils" "github.com/shunfei/cronsun/utils"
) )
@ -112,7 +111,7 @@ func (n *Node) keepAlive() {
if err := n.set(); err != nil { if err := n.set(); err != nil {
log.Warnf("%s set lid err: %s, try to reset after %d seconds...", n.String(), err.Error(), n.ttl) log.Warnf("%s set lid err: %s, try to reset after %d seconds...", n.String(), err.Error(), n.ttl)
} else { } else {
log.Noticef("%s set lid[%x] success", n.String(), n.lID) log.Infof("%s set lid[%x] success", n.String(), n.lID)
} }
timer.Reset(duration) timer.Reset(duration)
} }
@ -225,7 +224,7 @@ func (n *Node) addCmd(cmd *cronsun.Cmd, notice bool) {
} }
if notice { if notice {
log.Noticef("job[%s] rule[%s] timer[%s] has added", c.Job.ID, c.JobRule.ID, c.JobRule.Timer) log.Infof("job[%s] rule[%s] timer[%s] has added", c.Job.ID, c.JobRule.ID, c.JobRule.Timer)
} }
return return
} }
@ -233,7 +232,7 @@ func (n *Node) addCmd(cmd *cronsun.Cmd, notice bool) {
func (n *Node) delCmd(cmd *cronsun.Cmd) { func (n *Node) delCmd(cmd *cronsun.Cmd) {
delete(n.cmds, cmd.GetID()) delete(n.cmds, cmd.GetID())
n.Cron.DelJob(cmd) n.Cron.DelJob(cmd)
log.Noticef("job[%s] rule[%s] timer[%s] has deleted", cmd.Job.ID, cmd.JobRule.ID, cmd.JobRule.Timer) log.Infof("job[%s] rule[%s] timer[%s] has deleted", cmd.Job.ID, cmd.JobRule.ID, cmd.JobRule.Timer)
} }
func (n *Node) addGroup(g *cronsun.Group) { func (n *Node) addGroup(g *cronsun.Group) {

View File

@ -11,8 +11,8 @@ import (
client "github.com/coreos/etcd/clientv3" client "github.com/coreos/etcd/clientv3"
"github.com/go-gomail/gomail" "github.com/go-gomail/gomail"
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf" "github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
) )
type Noticer interface { type Noticer interface {

View File

@ -2,15 +2,15 @@ package cronsun
import ( import (
"fmt" "fmt"
"strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
client "github.com/coreos/etcd/clientv3" client "github.com/coreos/etcd/clientv3"
"strings"
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf" "github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
) )
var ( var (
@ -115,7 +115,7 @@ func (l *leaseID) keepAlive() {
if err := l.set(); err != nil { if err := l.set(); err != nil {
log.Warnf("proc lease id set err: %s, try to reset after %d seconds...", err.Error(), l.ttl) log.Warnf("proc lease id set err: %s, try to reset after %d seconds...", err.Error(), l.ttl)
} else { } else {
log.Noticef("proc set lease id[%x] success", l.get()) log.Infof("proc set lease id[%x] success", l.get())
} }
timer.Reset(duration) timer.Reset(duration)
} }

View File

@ -10,7 +10,7 @@ import (
"strings" "strings"
"time" "time"
"sunteng/commons/log" "github.com/shunfei/cronsun/log"
) )
type BaseHandler struct { type BaseHandler struct {

View File

@ -6,8 +6,8 @@ import (
v3 "github.com/coreos/etcd/clientv3" v3 "github.com/coreos/etcd/clientv3"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun" "github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/conf"
) )
type Info struct{} type Info struct{}

View File

@ -5,14 +5,14 @@ import (
"net/http" "net/http"
"sort" "sort"
"strings" "strings"
"time"
"github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/clientv3"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun" "github.com/shunfei/cronsun"
"time" "github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
) )
type Job struct{} type Job struct{}
@ -206,7 +206,7 @@ func (j *Job) GetList(w http.ResponseWriter, r *http.Request) {
m, err := cronsun.GetJobLatestLogListByJobIds(jobIds) m, err := cronsun.GetJobLatestLogListByJobIds(jobIds)
if err != nil { if err != nil {
log.Error("GetJobLatestLogListByJobIds error:", err.Error()) log.Errorf("GetJobLatestLogListByJobIds error: %s", err.Error())
} else { } else {
for i := range jobList { for i := range jobList {
jobList[i].LatestStatus = m[jobList[i].ID] jobList[i].LatestStatus = m[jobList[i].ID]
@ -289,7 +289,7 @@ func (j *Job) GetExecutingJob(w http.ResponseWriter, r *http.Request) {
for i := range gresp.Kvs { for i := range gresp.Kvs {
proc, err := cronsun.GetProcFromKey(string(gresp.Kvs[i].Key)) proc, err := cronsun.GetProcFromKey(string(gresp.Kvs[i].Key))
if err != nil { if err != nil {
log.Error("Failed to unmarshal Proc from key: ", err.Error()) log.Errorf("Failed to unmarshal Proc from key: %s", err.Error())
continue continue
} }

View File

@ -1,6 +1,7 @@
package web package web
import ( import (
"math"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -9,7 +10,6 @@ import (
"gopkg.in/mgo.v2" "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
"math"
"github.com/shunfei/cronsun" "github.com/shunfei/cronsun"
) )

View File

@ -2,16 +2,16 @@ package web
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"strings" "strings"
v3 "github.com/coreos/etcd/clientv3" v3 "github.com/coreos/etcd/clientv3"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"fmt"
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun" "github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
) )
type Node struct{} type Node struct{}
@ -92,7 +92,7 @@ func (n *Node) DeleteGroup(w http.ResponseWriter, r *http.Request) {
gresp, err := cronsun.DefalutClient.Get(conf.Config.Cmd, v3.WithPrefix()) gresp, err := cronsun.DefalutClient.Get(conf.Config.Cmd, v3.WithPrefix())
if err != nil { if err != nil {
errstr := fmt.Sprintf("failed to fetch jobs from etcd after deleted node group[%s]: %s", groupId, err.Error()) errstr := fmt.Sprintf("failed to fetch jobs from etcd after deleted node group[%s]: %s", groupId, err.Error())
log.Error(errstr) log.Errorf(errstr)
outJSONWithCode(w, http.StatusInternalServerError, errstr) outJSONWithCode(w, http.StatusInternalServerError, errstr)
return return
} }

View File

@ -6,8 +6,8 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun" "github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/conf"
) )
func GetVersion(w http.ResponseWriter, r *http.Request) { func GetVersion(w http.ResponseWriter, r *http.Request) {