package main
import (
"github.com/robfig/cron"
"time"
"fmt"
"os"
log "github.com/cihub/seelog"
)
var (
ttt int
)
const (
logFilePath = "hard/log/test/test.log"
)
func init() {
logLevel := os.Getenv("LOG_LEVEL")
if logLevel == "" {
logLevel = "error"
}
logger, err := log.LoggerFromConfigAsString(`
<seelog minlevel="` + logLevel + `" maxlevel="critical">
<outputs formatid="common">
<console />
<rollingfile formatid="common" type="date" datepattern="2006-01-02" filename="` + logFilePath + `" maxrolls="7"/>
</outputs>
<formats>
<format id="common" format="%Date %Time %LEVEL (%File:%Line) - %Msg%n"/>
</formats>
</seelog>
`)
if err != nil {
_ = log.Errorf("load seelog config failed! message: %s", err)
}
err = log.ReplaceLogger(logger)
if err != nil {
_ = log.Error(err)
}
}
func test(){
fmt.Printf("test=%d\n",ttt)
ttt++
}
func main() {
fmt.Println(os.Getenv("GOPATH"))
ttt=1
c := cron.New()
c.AddFunc("0/5 * * * * ?", func() { log.Info("Every hour on the half hour") })
c.AddFunc("0/5 * * * * ?", test)//5秒执行一次,12×5=60,所以一共执行12次
c.Start()
//log.Info("Every hour on the half hour")
time.Sleep(time.Minute) //一分钟后主线程退出
fmt.Println("aaa")
}
----------------------------------------
package mainimport ("github.com/robfig/cron"// "time""fmt""os"log "github.com/cihub/seelog")var (ttt int)const (logFilePath = "D:\\hard/log/test/test.log")func init() {logLevel := os.Getenv("LOG_LEVEL")if logLevel == "" {logLevel = "error"}
logger, err := log.LoggerFromConfigAsString(`<seelog minlevel="` + logLevel + `" maxlevel="critical"><outputs formatid="common"><console /><rollingfile formatid="common" type="date" datepattern="2006-01-02" filename="` + logFilePath + `" maxrolls="7"/></outputs>
<formats><format id="common" format="%Date %Time %LEVEL (%File:%Line) - %Msg%n"/></formats></seelog>`)if err != nil {_ = log.Errorf("load seelog config failed! message: %s", err)}
err = log.ReplaceLogger(logger)if err != nil {_ = log.Error(err)}
}func test(){
fmt.Printf("test=%d\n",ttt)ttt++
}func main() {fmt.Println(os.Getenv("GOPATH"))ttt=1c := cron.New()c.AddFunc("0 33 * * * * ", func() { log.Info("Every hour on the half hour") }) //每小时的第33分钟调用c.AddFunc("0 33 * * * * ", test)c.Start()
//log.Info("Every hour on the half hour")select{}
}-------------
func test(){log.Error("test")}func main() {c := cron.New()c.AddFunc("@daily", func() { log.Error("Every hour on the half hour") })//每天调用一次c.AddFunc("@midnight", test)//每天调用一次(和daily一样)c.Start()log.Info("Every hour on the half hour")time.Sleep(time.Minute)
fmt.Println("aaa")select{} //阻塞主线程不退出}
有疑问加站长微信联系(非本文作者)