github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/trace/logger_provider.go (about)

     1  package trace
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"path/filepath"
     7  	"strconv"
     8  
     9  	. "code.cloudfoundry.org/cli/cf/i18n"
    10  )
    11  
    12  func NewLogger(writer io.Writer, verbose bool, boolsOrPaths ...string) Printer {
    13  	LoggingToStdout = verbose
    14  
    15  	var printers []Printer
    16  
    17  	stdoutLogger := NewWriterPrinter(writer, true)
    18  
    19  	for _, path := range boolsOrPaths {
    20  		b, err := strconv.ParseBool(path)
    21  		LoggingToStdout = LoggingToStdout || b
    22  
    23  		if path != "" && err != nil {
    24  			var file *os.File
    25  			err = os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm)
    26  			if err == nil {
    27  				file, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
    28  			}
    29  
    30  			if err == nil {
    31  				printers = append(printers, NewWriterPrinter(file, false))
    32  			} else {
    33  				stdoutLogger.Printf(T("CF_TRACE ERROR CREATING LOG FILE {{.Path}}:\n{{.Err}}",
    34  					map[string]interface{}{"Path": path, "Err": err}))
    35  
    36  				LoggingToStdout = true
    37  			}
    38  		}
    39  	}
    40  
    41  	if LoggingToStdout {
    42  		printers = append(printers, stdoutLogger)
    43  	}
    44  
    45  	return CombinePrinters(printers)
    46  }