Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail-Safe error handling for AutoColor feature #122

Closed
NodyHub opened this issue Jan 25, 2023 · 1 comment · Fixed by #123
Closed

Fail-Safe error handling for AutoColor feature #122

NodyHub opened this issue Jan 25, 2023 · 1 comment · Fixed by #123

Comments

@NodyHub
Copy link
Contributor

NodyHub commented Jan 25, 2023

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?

@evanphx
Copy link
Contributor

evanphx commented Jan 25, 2023

Hi!

Looks like the unix version of the colorization code is too strict, at the very least. It should be allowing any writer, not just *os.Files.

That being said, I think your question is more about what AutoColor should do and if it should, if colorization is not available on the output, simply disable colorization. That seems like better logic than we have today.

evanphx added a commit that referenced this issue Jan 26, 2023
Cleanup AutoColor to make it never fail, instead do it's best to
activate color if possible only.

Fixes #122
evanphx added a commit that referenced this issue Jan 26, 2023
Cleanup AutoColor to make it never fail, instead do it's best to
activate color if possible only.

Fixes #122
evanphx added a commit that referenced this issue Jan 26, 2023
Cleanup AutoColor to make it never fail, instead do it's best to
activate color if possible only.

Fixes #122
evanphx added a commit that referenced this issue Jan 26, 2023
Cleanup AutoColor to make it never fail, instead do it's best to
activate color if possible only.

Fixes #122
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants