Skip to content

InitFlags does not reset klog configuration if application already did loggingΒ #67

@losipiuk

Description

@losipiuk

/kind bug

What steps did you take and what happened:

  1. Application uses klog.Infof()
  2. Logging happens based on klog default configuration.
  3. Application calls klog.InitFlags(nil) and flag.Parse(). Flags specify log file.
  4. Application uses klog.Infof() again.
  5. Logs are NOT appended to output file specified by flags

What did you expect to happen:

Ideally klog should properly reinitialize itself to configuration specified via flags on (4) and log from (5) should be appended to the log file.

Alternatively klog could disallows calling InitFlag() after logging was done before (panic?).
Yet this is suboptimal as one not always can prevent logging from happening before InitFlags. As logging can occur in init function of some dependent module.

Anything else you would like to add:

The source of described problem lies in behavior of createFiles (https://github.com/kubernetes/klog/blob/master/klog.go#L968). This method is called on first logging and log appenders are created then and put in logging.files array. Those are not reset later even if configuration changes.

Actually the situation is even more confusing than that. As appenders are created only for log levels up to requested one, we end up in inconsistent state of klog in following sequence of events:

  1. klog.Infof
  2. InitFlags
  3. klog.ErrorF

Then the appenders for DEBUG and INFO will be based on default configuration. And appender for ERROR will take configuration changes from flags into account.

I spotted the problem when playing with flag related to log file but possibly one can run into issues with other flags too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.lifecycle/frozenIndicates that an issue or PR should not be auto-closed due to staleness.priority/important-longtermImportant over the long term, but may not be staffed and/or may need multiple releases to complete.

    Type

    No type

    Projects

    Status

    Needs Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions