Skip to content
New issue

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

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

Already on GitHub? # to your account

Add option to disable color in logs #416

Merged
merged 1 commit into from
Jan 26, 2018
Merged

Conversation

PierreF
Copy link
Contributor

@PierreF PierreF commented Jan 22, 2018

This PR will fix #415
It's not currently working (color are always disabled for now).

This issue is that I didn't find a way to pass option to pkg/log before logger is build. Logger initialization happens during init()/package import... while parsing option happens in main().
It does not seems to be dynamically updateable like is the level. To update the value, I need to build a new logger (which means that I would need to update all place where old logger is used).

I could think of the following way to solve this issue:

  • After argument are parsed, create a logger with correct color settings and update place where logger is used (cmd/sentinel.log & pkg/postgresql.log for stolon-sentinel)
  • Use go-isatty directly in init() of pkg/log and don't use flag to forcefully disable color but maybe environment variable

I'm not fan of either solutions that both looks hack. I'm open to better suggestion.

@sgotti
Copy link
Member

sgotti commented Jan 23, 2018

@PierreF Thanks for the PR! Yeah, since we cannot in place update the encoder I think we should define in pkg/log two kind of loggers (with and without color), by default set the global logger as a non colored logger and replace the it with a colored logger in the main.

For the subpackages (now only pkg/postgres) we could add a global function (SetLogger) to set/update its global logger (like already done here: https://github.com/sorintlab/stolon/blob/master/cmd/proxy/proxy.go#L368) or just pass the logger in the constructor.

@PierreF PierreF changed the title WIP: Add option to disable color in logs Add option to disable color in logs Jan 23, 2018
@PierreF
Copy link
Contributor Author

PierreF commented Jan 23, 2018

Updated PR to have two loggers. By default the non-colored one and if condition are meet, replace logger with the colored one.
Condition are:

  • User provided the --log-level=true
  • OR, user didn't provided --log-level=false and stderr is a tty

Copy link
Member

@sgotti sgotti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PierreF Thanks! I agree and think it's the correct logic (also thanks to pflags Changed() function). Just a small nit to move the common logic but if you see some problems doing this let's keep it as is.

Otherwise it LGTM!

P.S. Just noticed that semaphore output is still colored so it's acting as a tty.

@@ -115,6 +117,7 @@ func init() {
cmdKeeper.PersistentFlags().StringVar(&cfg.pgSUUsername, "pg-su-username", user, "postgres superuser user name. Used for keeper managed instance access and pg_rewind based synchronization. It'll be created on db initialization. Defaults to the name of the effective user running stolon-keeper. Must be the same for all keepers.")
cmdKeeper.PersistentFlags().StringVar(&cfg.pgSUPassword, "pg-su-password", "", "postgres superuser password. Only one of --pg-su-password or --pg-su-passwordfile must be provided. Must be the same for all keepers.")
cmdKeeper.PersistentFlags().StringVar(&cfg.pgSUPasswordFile, "pg-su-passwordfile", "", "postgres superuser password file. Only one of --pg-su-password or --pg-su-passwordfile must be provided. Must be the same for all keepers)")
cmdKeeper.PersistentFlags().BoolVar(&cfg.logColor, "log-color", false, "enable color in log output (default if attached to a terminal)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could move this options and related logic inside the common command config: https://github.com/sorintlab/stolon/blob/master/cmd/common.go to avoid replicating it.

Signed-off-by: Pierre Fersing <pierre.fersing@bleemeo.com>
@sgotti
Copy link
Member

sgotti commented Jan 26, 2018

@PierreF LGTM! Let me know if you have other pending changes or I can merge it.

@PierreF
Copy link
Contributor Author

PierreF commented Jan 26, 2018

No, It's good for me.

@sgotti sgotti merged commit 70464d3 into sorintlab:master Jan 26, 2018
sgotti added a commit that referenced this pull request Jan 26, 2018
Add option to disable color in logs
@sgotti
Copy link
Member

sgotti commented Jan 26, 2018

@PierreF Thanks a lot! Merged.

@sgotti sgotti added this to the v0.9.0 milestone Feb 1, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disable color in log output
2 participants