func getOutputRedirection()

in agent/logging/agent_logging.go [190:229]


func getOutputRedirection(agentconfig config.AgentConfig) ([]*os.File, error) {
	var outputFds []*os.File = make([]*os.File, 3)

	// Set the default file descriptors unless we are configured differently
	outputFds[syscall.Stdin] = os.Stdin
	outputFds[syscall.Stdout] = os.Stdout
	outputFds[syscall.Stderr] = os.Stderr

	// We are configured to log to a disk location
	if agentconfig.EnvoyLoggingDestination != config.ENVOY_LOG_DESTINATION_DEFAULT {

		fileInfo, err := os.Stat(agentconfig.EnvoyLoggingDestination)
		if err != nil {
			log.Errorf("Unable to determine state of log destination %s", agentconfig.EnvoyLoggingDestination)
			return outputFds, err
		}

		if !fileInfo.IsDir() {
			log.Errorf("Log destination %s, is not a directory", agentconfig.EnvoyLoggingDestination)
			return outputFds, errors.New("log destination is not a directory")
		}

		start := time.Now()

		// Create a file and use the copy/truncate method to rotate it.
		redirectedLog, err := openDiskLogFile(agentconfig)
		if err != nil {
			log.Warnf("Unable to redirect output to the configured log file: %v", err)
			return outputFds, err
		}

		// Reset the agent stdout/stderr to the output Fds
		outputFds[syscall.Stdout] = redirectedLog
		outputFds[syscall.Stderr] = redirectedLog

		log.Debugf("Log rotation took [%d us]\n", time.Since(start).Microseconds())
	}

	return outputFds, nil
}