示例#1
0
func NewLogger(writer io.Writer, verbose bool, cfTrace, configTrace string) Printer {
	LoggingToStdout = verbose

	var printers []Printer

	stdoutLogger := NewWriterPrinter(writer, true)

	for _, path := range []string{cfTrace, configTrace} {
		b, err := strconv.ParseBool(path)
		LoggingToStdout = LoggingToStdout || b

		if path != "" && err != nil {
			file, err := fileutils.Open(path)

			if err == nil {
				printers = append(printers, NewWriterPrinter(file, false))
			} else {
				stdoutLogger.Printf(T("CF_TRACE ERROR CREATING LOG FILE {{.Path}}:\n{{.Err}}",
					map[string]interface{}{"Path": path, "Err": err}))

				LoggingToStdout = true
			}
		}
	}

	if LoggingToStdout {
		printers = append(printers, stdoutLogger)
	}

	return CombinePrinters(printers)
}
	. "github.com/onsi/gomega"
)

var _ = Describe("Fileutils File", func() {
	var fixturePath = filepath.Clean("../fixtures/fileutils/supervirus.zsh")
	var fixtureBytes []byte

	BeforeEach(func() {
		var err error
		fixtureBytes, err = ioutil.ReadFile(fixturePath)
		Expect(err).NotTo(HaveOccurred())
	})

	Describe("Open", func() {
		It("opens an existing file", func() {
			fd, err := fileutils.Open(fixturePath)
			Expect(err).NotTo(HaveOccurred())

			fileBytes, err := ioutil.ReadAll(fd)
			Expect(err).NotTo(HaveOccurred())
			fd.Close()

			Expect(fileBytes).To(Equal(fixtureBytes))
		})

		It("creates a non-existing file and all intermediary directories", func() {
			fd, err := ioutil.TempFile("", "open_test")
			Expect(err).NotTo(HaveOccurred())

			_, err = fd.WriteString("Never Gonna Give You Up")
			Expect(err).NotTo(HaveOccurred())