Beispiel #1
0
func (h *TimedRotatingFileHook) doRollover() {
	if h.debug {
		fmt.Fprintf(os.Stderr, "[DEBUG] Start to rotate the log file.\n")
	}

	h.file.Close()

	dstTime := h.rotatorAt - h.interval
	dstPath := h.filename + "." + time.Unix(dstTime, 0).Format(h.suffix)
	if file.IsExist(dstPath) {
		os.Remove(dstPath)
	}

	if file.IsFile(h.filename) {
		err := os.Rename(h.filename, dstPath)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to rename %v to %v\n", h.filename, dstPath)
		}
	}

	if h.backupCount > 0 {
		files := h.getFilesToDelete()
		for _, f := range files {
			if h.debug {
				fmt.Fprintf(os.Stderr, "[DEBUG] Delete the old log file: %v\n", f)
			}
			os.Remove(f)
		}
	}

	h.file.Open()
	h.ReComputeRollover()
}
Beispiel #2
0
func (self *TimedRotatingFile) doRollover() (err error) {
	if err = self.Close(); err != nil {
		return
	}

	dstTime := self.rotatorAt - self.interval
	dstPath := self.filename + "." + time.Unix(dstTime, 0).Format(time2fmt[self.when])
	if file.IsExist(dstPath) {
		os.Remove(dstPath)
	}

	if file.IsFile(self.filename) {
		if err = os.Rename(self.filename, dstPath); err != nil {
			return err
		}
	}

	if self.backupCount > 0 {
		for _, file := range self.getFilesToDelete() {
			os.Remove(file)
		}
	}

	self.reComputeRollover()
	return self.open()
}