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

Logger.SetLevel #503

Closed
ChimeraCoder opened this issue Mar 14, 2017 · 7 comments
Closed

Logger.SetLevel #503

ChimeraCoder opened this issue Mar 14, 2017 · 7 comments

Comments

@ChimeraCoder
Copy link
Contributor

It appears that we can't (safely) configure the loglevels of loggers except for the standard logger.

For the standard logger, we can use the logrus.SetLevel method, which protects the level with a mutex, but this mutex is unexported. So if we set var log = logrus.New() in our application (which is recommended in the docs), we actually can't configure the log level.

The only other thing we can do is to call SetLevel on the standard logger and then assigning it:

var log *logrus.Logger

func init(){
    logrus.SetLevel(logrus.DebugLevel)
    log = logrus.StandardLogger()
}

but that's a bit clumsier, and it also means we're restricted to just using the standard logger.

Am I missing an alternative?

@flashmob
Copy link

👍 Had a similar situation.

Perhaps instead logrus.New() try instantiating logger on your own:

&log.Logger{
    Out: os.Stderr,
    Formatter: new(log.TextFormatter),
    Hooks: make(log.LevelHooks),
    Level: log.DebugLevel,
}

@ChimeraCoder
Copy link
Contributor Author

Unfortunately that doesn't work for us, because we need to be able to set the log level based on the configuration file that's passed. The only way to do that and also preserve the var log *logrus.Logger declaration as a drop-in replacement for the standard library log is to have SetLevel. Otherwise, it still introduces a race condition.

@sirupsen I'm happy to put together a PR for this if it's something you're interested in adding.

@flashmob
Copy link

Yes, that's still a limitation - can't change the log level after it has been set,.

Would be interested in testing out your PR too.

@sirupsen
Copy link
Owner

This makes sense, feel free to propose a PR upstream.

@DmitriyMV
Copy link
Contributor

@ChimeraCoder @sirupsen
because #512 had been merged - maybe it will make sense to change from func (logger *Logger) setLevel(level Level) to func (logger *Logger) SetLevel(level Level) { to make this method publicly available? Any downsides?

stevejarvis pushed a commit to stevejarvis/logrus that referenced this issue Jul 7, 2017
dmathieu pushed a commit that referenced this issue Jul 13, 2017
Export logger's set level function to address #503.
@dmathieu
Copy link
Contributor

This has shipped 🎉

msiebeneicher pushed a commit to msiebeneicher/logrus that referenced this issue Jul 17, 2017
…ure/add_color_setting_support_in_text_formatter

* 'master' of https://github.com/sirupsen/logrus: (21 commits)
  Improve logrus.Entry.Level documentation
  remove QuoteCharacter option
  add changelog entry about making SetLevel public
  Mention quoting bugfix in CHANGELOG
  Error is not a special case anymore
  Generalize test case
  Reduce duplicate code
  Quote non-string values if necessary
  remove ^ from custom quote characters
  readme: link to comment on casing
  Mention bugfix in changelog
  Extract quoting into separate method
  Use custom quote char and escape it
  Safely format data when printing
  Export logger's set level function to address sirupsen#503.
  fix casing
  readme: add note about lookign for maintainers
  Allow more chars in unquoted text formatter output
  Update hook name
  README: add hook for Mattermost
  ...

# Conflicts:
#	text_formatter.go
@sirupsen
Copy link
Owner

🎉

Paddy-Truck pushed a commit to Paddy-Truck/logrus that referenced this issue Sep 8, 2017
cgxxv pushed a commit to cgxxv/logrus that referenced this issue Mar 25, 2022
cgxxv pushed a commit to cgxxv/logrus that referenced this issue Mar 25, 2022
Export logger's set level function to address sirupsen#503.
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

No branches or pull requests

5 participants