github.com/timsutton/packer@v1.3.2/log.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"strings"
     9  )
    10  
    11  // These are the environmental variables that determine if we log, and if
    12  // we log whether or not the log should go to a file.
    13  const EnvLog = "PACKER_LOG"          //Set to True
    14  const EnvLogFile = "PACKER_LOG_PATH" //Set to a file
    15  
    16  // logOutput determines where we should send logs (if anywhere).
    17  func logOutput() (logOutput io.Writer, err error) {
    18  	logOutput = nil
    19  	if os.Getenv(EnvLog) != "" && os.Getenv(EnvLog) != "0" {
    20  		logOutput = os.Stderr
    21  
    22  		if logPath := os.Getenv(EnvLogFile); logPath != "" {
    23  			var err error
    24  			logOutput, err = os.Create(logPath)
    25  			if err != nil {
    26  				return nil, err
    27  			}
    28  		} else {
    29  			// no path; do a little light filtering to avoid double-dipping UI
    30  			// calls.
    31  			r, w := io.Pipe()
    32  			scanner := bufio.NewScanner(r)
    33  			go func(scanner *bufio.Scanner) {
    34  				for scanner.Scan() {
    35  					if strings.Contains(scanner.Text(), "ui:") {
    36  						continue
    37  					}
    38  					os.Stderr.WriteString(fmt.Sprint(scanner.Text() + "\n"))
    39  				}
    40  			}(scanner)
    41  			logOutput = w
    42  		}
    43  	}
    44  
    45  	return
    46  }