Skip to content

Fail-Safe error handling for AutoColor feature #122

@nodyhub

Description

@nodyhub

Hi folks,

I am using hclog in a project and instantiate the logger with a custom io.Writer, which should be fine according to the struct:

Output io.Writer

If ColorOption is set to AutoColor, we get only a collored output, if Output from the previous line is a true io.File

go-hclog/logger.go

Lines 273 to 275 in fb16e2d

// Color the output. On Windows, colored logs are only available for io.Writers that
// are concretely instances of *os.File.
Color ColorOption

But, if Output is not an io.File, hclog panics, instead of a fail-safe operation and omitting colouring.

fi := l.checkWriterIsFile()

go-hclog/intlogger.go

Lines 879 to 887 in fb16e2d

// checks if the underlying io.Writer is a file, and
// panics if not. For use by colorization.
func (l *intLogger) checkWriterIsFile() *os.File {
fi, ok := l.writer.w.(*os.File)
if !ok {
panic("Cannot enable coloring of non-file Writers")
}
return fi
}

As a fail-safe option, we can always switch to ColorOff, instead of panic.

ColorOff ColorOption = iota

Or, might i miss something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions