X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=log%2Flog.go;h=c76648348b796e53ce317c4ec1f9404e783704c5;hb=1ccb4267d3a1017fd29f41e6dba81890759a9253;hp=43bcc9ca29d10db7006b63a732425f973fbed3d7;hpb=bc213b29d91743bb9cb23c043f2856f47b34bb3e;p=bytom%2Fvapor.git diff --git a/log/log.go b/log/log.go index 43bcc9ca..c7664834 100644 --- a/log/log.go +++ b/log/log.go @@ -1,7 +1,11 @@ package log import ( + "fmt" + "io/ioutil" + "os" "path/filepath" + "strings" "sync" "time" @@ -18,9 +22,17 @@ const ( var defaultFormatter = &logrus.TextFormatter{DisableColors: true} -func InitLogFile(config *config.Config) { - hook := newBtmHook(config.LogDir()) +func InitLogFile(config *config.Config) error { + logPath := config.LogDir() + if err := clearLockFiles(logPath); err != nil { + return err + } + + hook := newBtmHook(logPath) logrus.AddHook(hook) + logrus.SetOutput(ioutil.Discard) //控制台不输出 + fmt.Printf("all logs are output in the %s directory\n", logPath) + return nil } type BtmHook struct { @@ -60,6 +72,24 @@ func (hook *BtmHook) ioWrite(entry *logrus.Entry) error { return err } +func clearLockFiles(logPath string) error { + files, err := ioutil.ReadDir(logPath) + if os.IsNotExist(err) { + return nil + }else if err != nil { + return err + } + + for _, file := range files { + if ok := strings.HasSuffix(file.Name(), "_lock"); ok { + if err := os.Remove(filepath.Join(logPath, file.Name())); err != nil { + return err + } + } + } + return nil +} + func (hook *BtmHook) Fire(entry *logrus.Entry) error { hook.lock.Lock() defer hook.lock.Unlock()