func (log *Logger) rotate() error { nowDate := utils.GetNowStringYMD() if nowDate == log.fileNameDate { return nil } logFileLock[log.fileNameBase].Lock() defer logFileLock[log.fileNameBase].Unlock() if nowDate == log.fileNameDate { return nil } oldFile := log.Out.(*os.File) oldFile.Close() logFile := filepath.Join(log.path, fmt.Sprintf("%s.log", log.fileNameBase)) rotateFileName := filepath.Join(log.path, fmt.Sprintf("%s.log.%s", log.fileNameBase, log.fileNameDate)) err := exec.Command("mv", logFile, rotateFileName).Run() if err != nil { log.Printf("Failed to rotate log file[%s]: %s ", rotateFileName, err.Error()) } log.fileNameDate = nowDate f, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0660) if err != nil { // TODO: return err } log.Out = f return nil }
func (log *Logger) init() { log.fileNameDate = utils.GetNowStringYMD() p := filepath.Join(log.path, fmt.Sprintf("%s.log", log.fileNameBase)) f, err := os.OpenFile(p, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0660) if err != nil { log.Fatalf("Failed to init [%s] log: %s", log.fileNameBase, err.Error()) } log.Out = f log.Formatter = new(logrus.JSONFormatter) }