func()

in agent/logging/agent_logging.go [137:175]


func (a *AgentLogWriterImpl) monitorAndRotateLog(agentConfig config.AgentConfig, watcher *fsnotify.Watcher) {

	logPath := path.Join(
		agentConfig.EnvoyLoggingDestination,
		agentConfig.EnvoyLogFileName,
	)

	watcher.Add(logPath)

	// We cannot log in this function.  This loop operates on inotify events
	// so logging here ends up begin recursive.
	var maxSize int64 = int64(agentConfig.MaxLogFileSizeMB * 1_048_576)
	for {
		select {
		case event := <-watcher.Events:
			if event.Op&fsnotify.Write == fsnotify.Write {
				fileInfo, err := os.Stat(logPath)
				if err != nil {
					continue
				}

				if fileInfo.Size() > maxSize {
					// Append the epoch to the filename.  This represents the end of the log's
					// contents
					var dstFileName string = fmt.Sprintf("%s.%d", logPath, time.Now().Unix())
					copyLogFile(logPath, dstFileName)

					// Truncate the existing log file
					a.defaultSink.Truncate(0)
					a.defaultSink.Seek(0, 0)
					a.defaultSink.Sync()

					// Purge old files, and preserve the configured file count
					CleanupLogFiles(logPath, agentConfig.MaxLogCount)
				}
			}
		}
	}
}