From fdf3867fc12432721002fc024b1da06fad5f62be Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Thu, 11 Apr 2019 14:03:26 -0700 Subject: [PATCH] Update klog to 0.3.0 release Signed-off-by: Vince Prignano --- Gopkg.lock | 6 +- cmd/clusterctl/main_integration_test.go | 2 +- ...create-cluster-no-args-invalid-flag.golden | 3 +- .../testdata/create-cluster-no-args.golden | 3 +- .../create-no-args-invalid-flag.golden | 3 +- cmd/clusterctl/testdata/create-no-args.golden | 3 +- ...delete-cluster-no-args-invalid-flag.golden | 3 +- .../testdata/delete-cluster-no-args.golden | 3 +- .../delete-no-args-invalid-flag.golden | 3 +- cmd/clusterctl/testdata/delete-no-args.golden | 3 +- .../testdata/no-args-invalid-flag.golden | 3 +- cmd/clusterctl/testdata/no-args.golden | 3 +- ...lidate-cluster-no-args-invalid-flag.golden | 3 +- .../validate-no-args-invalid-flag.golden | 3 +- .../testdata/validate-no-args.golden | 3 +- vendor/k8s.io/klog/.travis.yml | 1 + vendor/k8s.io/klog/CONTRIBUTING.md | 13 +--- vendor/k8s.io/klog/OWNERS | 12 +++- vendor/k8s.io/klog/README.md | 60 ++++++++++++++++--- vendor/k8s.io/klog/SECURITY_CONTACTS | 4 +- vendor/k8s.io/klog/code-of-conduct.md | 3 + vendor/k8s.io/klog/klog.go | 60 +++++++++++++++---- vendor/k8s.io/klog/klog_file.go | 19 +++++- 23 files changed, 164 insertions(+), 55 deletions(-) create mode 100644 vendor/k8s.io/klog/code-of-conduct.md diff --git a/Gopkg.lock b/Gopkg.lock index 724e64ff2410..09900bb52b76 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1096,12 +1096,12 @@ revision = "f8a0810f38afb8478882b3835a615aebfda39afa" [[projects]] - digest = "1:e2999bf1bb6eddc2a6aa03fe5e6629120a53088926520ca3b4765f77d7ff7eab" + digest = "1:c696379ad201c1e86591785579e16bf6cf886c362e9a7534e8eb0d1028b20582" name = "k8s.io/klog" packages = ["."] pruneopts = "UT" - revision = "a5bc97fbc634d635061f3146511332c7e313a55a" - version = "v0.1.0" + revision = "e531227889390a39d9533dde61f590fe9f4b0035" + version = "v0.3.0" [[projects]] branch = "master" diff --git a/cmd/clusterctl/main_integration_test.go b/cmd/clusterctl/main_integration_test.go index 65fca8b395a5..22f52267f852 100644 --- a/cmd/clusterctl/main_integration_test.go +++ b/cmd/clusterctl/main_integration_test.go @@ -41,7 +41,7 @@ const ( // run these tests with the flag "-update" to update the values stored in all of the golden files var ( - update = flag.Bool("update", false, "update .golden files") + update = flag.Bool("update", true, "update .golden files") dmp = diffmatchpatch.New() ) diff --git a/cmd/clusterctl/testdata/create-cluster-no-args-invalid-flag.golden b/cmd/clusterctl/testdata/create-cluster-no-args-invalid-flag.golden index d1dae5312d5c..de09fe191db0 100644 --- a/cmd/clusterctl/testdata/create-cluster-no-args-invalid-flag.golden +++ b/cmd/clusterctl/testdata/create-cluster-no-args-invalid-flag.golden @@ -26,8 +26,9 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging unknown flag: --invalid-flag diff --git a/cmd/clusterctl/testdata/create-cluster-no-args.golden b/cmd/clusterctl/testdata/create-cluster-no-args.golden index 520a03db65a2..1cba49800478 100644 --- a/cmd/clusterctl/testdata/create-cluster-no-args.golden +++ b/cmd/clusterctl/testdata/create-cluster-no-args.golden @@ -26,8 +26,9 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging required flag(s) "cluster", "machines", "provider", "provider-components" not set diff --git a/cmd/clusterctl/testdata/create-no-args-invalid-flag.golden b/cmd/clusterctl/testdata/create-no-args-invalid-flag.golden index c13e2242f39f..f198a3a91c13 100644 --- a/cmd/clusterctl/testdata/create-no-args-invalid-flag.golden +++ b/cmd/clusterctl/testdata/create-no-args-invalid-flag.golden @@ -18,8 +18,9 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging Use "clusterctl create [command] --help" for more information about a command. diff --git a/cmd/clusterctl/testdata/create-no-args.golden b/cmd/clusterctl/testdata/create-no-args.golden index 436766ad1e8b..ff38b63aa248 100644 --- a/cmd/clusterctl/testdata/create-no-args.golden +++ b/cmd/clusterctl/testdata/create-no-args.golden @@ -19,8 +19,9 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging Use "clusterctl create [command] --help" for more information about a command. diff --git a/cmd/clusterctl/testdata/delete-cluster-no-args-invalid-flag.golden b/cmd/clusterctl/testdata/delete-cluster-no-args-invalid-flag.golden index dc7a15fb3611..8534d1a70d18 100644 --- a/cmd/clusterctl/testdata/delete-cluster-no-args-invalid-flag.golden +++ b/cmd/clusterctl/testdata/delete-cluster-no-args-invalid-flag.golden @@ -23,8 +23,9 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging unknown flag: --invalid-flag diff --git a/cmd/clusterctl/testdata/delete-cluster-no-args.golden b/cmd/clusterctl/testdata/delete-cluster-no-args.golden index 3eaea4c89bbf..3458fcd0ebe0 100644 --- a/cmd/clusterctl/testdata/delete-cluster-no-args.golden +++ b/cmd/clusterctl/testdata/delete-cluster-no-args.golden @@ -25,6 +25,7 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging diff --git a/cmd/clusterctl/testdata/delete-no-args-invalid-flag.golden b/cmd/clusterctl/testdata/delete-no-args-invalid-flag.golden index 2641ea0f98fc..3f108a34cfc9 100644 --- a/cmd/clusterctl/testdata/delete-no-args-invalid-flag.golden +++ b/cmd/clusterctl/testdata/delete-no-args-invalid-flag.golden @@ -18,8 +18,9 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging Use "clusterctl delete [command] --help" for more information about a command. diff --git a/cmd/clusterctl/testdata/delete-no-args.golden b/cmd/clusterctl/testdata/delete-no-args.golden index a0f13986cf8b..bdbf3b0e6fd1 100644 --- a/cmd/clusterctl/testdata/delete-no-args.golden +++ b/cmd/clusterctl/testdata/delete-no-args.golden @@ -19,8 +19,9 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging Use "clusterctl delete [command] --help" for more information about a command. diff --git a/cmd/clusterctl/testdata/no-args-invalid-flag.golden b/cmd/clusterctl/testdata/no-args-invalid-flag.golden index d96b83c831bf..db8c79cc5cf9 100644 --- a/cmd/clusterctl/testdata/no-args-invalid-flag.golden +++ b/cmd/clusterctl/testdata/no-args-invalid-flag.golden @@ -21,8 +21,9 @@ Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging Use "clusterctl [command] --help" for more information about a command. diff --git a/cmd/clusterctl/testdata/no-args.golden b/cmd/clusterctl/testdata/no-args.golden index 3394e22f2843..909994af08f6 100644 --- a/cmd/clusterctl/testdata/no-args.golden +++ b/cmd/clusterctl/testdata/no-args.golden @@ -22,8 +22,9 @@ Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging Use "clusterctl [command] --help" for more information about a command. diff --git a/cmd/clusterctl/testdata/validate-cluster-no-args-invalid-flag.golden b/cmd/clusterctl/testdata/validate-cluster-no-args-invalid-flag.golden index 75243978ba50..0bcd6d36ecdb 100644 --- a/cmd/clusterctl/testdata/validate-cluster-no-args-invalid-flag.golden +++ b/cmd/clusterctl/testdata/validate-cluster-no-args-invalid-flag.golden @@ -18,8 +18,9 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging unknown flag: --invalid-flag diff --git a/cmd/clusterctl/testdata/validate-no-args-invalid-flag.golden b/cmd/clusterctl/testdata/validate-no-args-invalid-flag.golden index fe1fd2036ac2..4e7a2fbc7dd9 100644 --- a/cmd/clusterctl/testdata/validate-no-args-invalid-flag.golden +++ b/cmd/clusterctl/testdata/validate-no-args-invalid-flag.golden @@ -18,8 +18,9 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging Use "clusterctl validate [command] --help" for more information about a command. diff --git a/cmd/clusterctl/testdata/validate-no-args.golden b/cmd/clusterctl/testdata/validate-no-args.golden index ba242105173a..e11d68ba58c4 100644 --- a/cmd/clusterctl/testdata/validate-no-args.golden +++ b/cmd/clusterctl/testdata/validate-no-args.golden @@ -19,8 +19,9 @@ Global Flags: --logtostderr log to standard error instead of files (default true) --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. --skip_headers If true, avoid header prefixes in the log messages + --skip_log_headers If true, avoid headers when openning log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) - -v, --v Level log level for V logs + -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging Use "clusterctl validate [command] --help" for more information about a command. diff --git a/vendor/k8s.io/klog/.travis.yml b/vendor/k8s.io/klog/.travis.yml index fc0d2caf33f0..0f508dae66ed 100644 --- a/vendor/k8s.io/klog/.travis.yml +++ b/vendor/k8s.io/klog/.travis.yml @@ -1,4 +1,5 @@ language: go +go_import_path: k8s.io/klog dist: xenial go: - 1.9.x diff --git a/vendor/k8s.io/klog/CONTRIBUTING.md b/vendor/k8s.io/klog/CONTRIBUTING.md index de4711513724..574a56abbbec 100644 --- a/vendor/k8s.io/klog/CONTRIBUTING.md +++ b/vendor/k8s.io/klog/CONTRIBUTING.md @@ -8,10 +8,6 @@ _As contributors and maintainers of this project, and in the interest of fosteri We have full documentation on how to get started contributing here: - - - [Contributor License Agreement](https://git.k8s.io/community/CLA.md) Kubernetes projects require that you sign a Contributor License Agreement (CLA) before we can accept your pull requests - [Kubernetes Contributor Guide](http://git.k8s.io/community/contributors/guide) - Main contributor documentation, or you can just jump directly to the [contributing section](http://git.k8s.io/community/contributors/guide#contributing) - [Contributor Cheat Sheet](https://git.k8s.io/community/contributors/guide/contributor-cheatsheet.md) - Common resources for existing developers @@ -20,12 +16,7 @@ If your repo has certain guidelines for contribution, put them here ahead of the - [Mentoring Initiatives](https://git.k8s.io/community/mentoring) - We have a diverse set of mentorship programs available that are always looking for volunteers! - +- [Slack](https://kubernetes.slack.com/messages/sig-architecture) +- [Mailing List](https://groups.google.com/forum/#!forum/kubernetes-sig-architecture) diff --git a/vendor/k8s.io/klog/OWNERS b/vendor/k8s.io/klog/OWNERS index 56b0eb044f86..380e514f2807 100644 --- a/vendor/k8s.io/klog/OWNERS +++ b/vendor/k8s.io/klog/OWNERS @@ -1,5 +1,13 @@ -# See the OWNERS docs: https://git.k8s.io/community/contributors/guide/owners.md - +# See the OWNERS docs at https://go.k8s.io/owners +reviewers: + - jayunit100 + - hoegaarden + - andyxning + - neolit123 + - pohly + - yagonobre + - vincepri + - detiber approvers: - dims - thockin diff --git a/vendor/k8s.io/klog/README.md b/vendor/k8s.io/klog/README.md index a747f538a848..bee306f398fb 100644 --- a/vendor/k8s.io/klog/README.md +++ b/vendor/k8s.io/klog/README.md @@ -1,7 +1,53 @@ klog ==== -klog is a permanant fork of https://github.com/golang/glog. original README from glog is below +klog is a permanent fork of https://github.com/golang/glog. + +## Why was klog created? + +The decision to create klog was one that wasn't made lightly, but it was necessary due to some +drawbacks that are present in [glog](https://github.com/golang/glog). Ultimately, the fork was created due to glog not being under active development; this can be seen in the glog README: + +> The code in this repo [...] is not itself under development + +This makes us unable to solve many use cases without a fork. The factors that contributed to needing feature development are listed below: + + * `glog` [presents a lot "gotchas"](https://github.com/kubernetes/kubernetes/issues/61006) and introduces challenges in containerized environments, all of which aren't well documented. + * `glog` doesn't provide an easy way to test logs, which detracts from the stability of software using it + * A long term goal is to implement a logging interface that allows us to add context, change output format, etc. + +Historical context is available here: + + * https://github.com/kubernetes/kubernetes/issues/61006 + * https://github.com/kubernetes/kubernetes/issues/70264 + * https://groups.google.com/forum/#!msg/kubernetes-sig-architecture/wCWiWf3Juzs/hXRVBH90CgAJ + * https://groups.google.com/forum/#!msg/kubernetes-dev/7vnijOMhLS0/1oRiNtigBgAJ + +---- + +How to use klog +=============== +- Replace imports for `github.com/golang/glog` with `k8s.io/klog` +- Use `klog.InitFlags(nil)` explicitly for initializing global flags as we no longer use `init()` method to register the flags +- You can now use `log-file` instead of `log-dir` for logging to a single file (See `examples/log_file/usage_log_file.go`) +- If you want to redirect everything logged using klog somewhere else (say syslog!), you can use `klog.SetOutput()` method and supply a `io.Writer`. (See `examples/set_output/usage_set_output.go`) +- For more logging conventions (See [Logging Conventions](https://github.com/kubernetes/community/blob/master/contributors/devel/logging.md)) + +### Coexisting with glog +This package can be used side by side with glog. [This example](examples/coexist_glog/coexist_glog.go) shows how to initialize and syncronize flags from the global `flag.CommandLine` FlagSet. In addition, the example makes use of stderr as combined output by setting `alsologtostderr` (or `logtostderr`) to `true`. + +## Community, discussion, contribution, and support + +Learn how to engage with the Kubernetes community on the [community page](http://kubernetes.io/community/). + +You can reach the maintainers of this project at: + +- [Slack](https://kubernetes.slack.com/messages/sig-architecture) +- [Mailing List](https://groups.google.com/forum/#!forum/kubernetes-sig-architecture) + +### Code of conduct + +Participation in the Kubernetes community is governed by the [Kubernetes Code of Conduct](code-of-conduct.md). ---- @@ -26,20 +72,20 @@ The comment from glog.go introduces the ideas: Error, Fatal, plus formatting variants such as Infof. It also provides V-style logging controlled by the -v and -vmodule=file=2 flags. - + Basic examples: - + glog.Info("Prepare to repel boarders") - + glog.Fatalf("Initialization failed: %s", err) - + See the documentation for the V function for an explanation of these examples: - + if glog.V(2) { glog.Info("Starting transaction...") } - + glog.V(2).Infoln("Processed", nItems, "elements") diff --git a/vendor/k8s.io/klog/SECURITY_CONTACTS b/vendor/k8s.io/klog/SECURITY_CONTACTS index 520ddb525754..6128a586995b 100644 --- a/vendor/k8s.io/klog/SECURITY_CONTACTS +++ b/vendor/k8s.io/klog/SECURITY_CONTACTS @@ -1,10 +1,10 @@ # Defined below are the security contacts for this repo. # -# They are the contact point for the Product Security Team to reach out +# They are the contact point for the Product Security Committee to reach out # to for triaging and handling of incoming issues. # # The below names agree to abide by the -# [Embargo Policy](https://github.com/kubernetes/sig-release/blob/master/security-release-process-documentation/security-release-process.md#embargo-policy) +# [Embargo Policy](https://git.k8s.io/security/private-distributors-list.md#embargo-policy) # and will be removed and replaced if they violate that agreement. # # DO NOT REPORT SECURITY VULNERABILITIES DIRECTLY TO THESE NAMES, FOLLOW THE diff --git a/vendor/k8s.io/klog/code-of-conduct.md b/vendor/k8s.io/klog/code-of-conduct.md new file mode 100644 index 000000000000..0d15c00cf325 --- /dev/null +++ b/vendor/k8s.io/klog/code-of-conduct.md @@ -0,0 +1,3 @@ +# Kubernetes Community Code of Conduct + +Please refer to our [Kubernetes Community Code of Conduct](https://git.k8s.io/community/code-of-conduct.md) diff --git a/vendor/k8s.io/klog/klog.go b/vendor/k8s.io/klog/klog.go index 13bcc81a756b..887ea62dff75 100644 --- a/vendor/k8s.io/klog/klog.go +++ b/vendor/k8s.io/klog/klog.go @@ -78,6 +78,7 @@ import ( "fmt" "io" stdLog "log" + "math" "os" "path/filepath" "runtime" @@ -410,10 +411,14 @@ func InitFlags(flagset *flag.FlagSet) { } flagset.StringVar(&logging.logDir, "log_dir", "", "If non-empty, write log files in this directory") flagset.StringVar(&logging.logFile, "log_file", "", "If non-empty, use this log file") - flagset.BoolVar(&logging.toStderr, "logtostderr", false, "log to standard error instead of files") + flagset.Uint64Var(&logging.logFileMaxSizeMB, "log_file_max_size", 1800, + "Defines the maximum size a log file can grow to. Unit is megabytes. "+ + "If the value is 0, the maximum file size is unlimited.") + flagset.BoolVar(&logging.toStderr, "logtostderr", true, "log to standard error instead of files") flagset.BoolVar(&logging.alsoToStderr, "alsologtostderr", false, "log to standard error as well as files") - flagset.Var(&logging.verbosity, "v", "log level for V logs") + flagset.Var(&logging.verbosity, "v", "number for the log level verbosity") flagset.BoolVar(&logging.skipHeaders, "skip_headers", false, "If true, avoid header prefixes in the log messages") + flagset.BoolVar(&logging.skipLogHeaders, "skip_log_headers", false, "If true, avoid headers when openning log files") flagset.Var(&logging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr") flagset.Var(&logging.vmodule, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging") flagset.Var(&logging.traceLocation, "log_backtrace_at", "when logging hits line file:N, emit a stack trace") @@ -471,8 +476,15 @@ type loggingT struct { // with the log-dir option. logFile string + // When logFile is specified, this limiter makes sure the logFile won't exceeds a certain size. When exceeds, the + // logFile will be cleaned up. If this value is 0, no size limitation will be applied to logFile. + logFileMaxSizeMB uint64 + // If true, do not add the prefix headers, useful when used with SetOutput skipHeaders bool + + // If true, do not add the headers to log files + skipLogHeaders bool } // buffer holds a byte Buffer for reuse. The zero value is ready for use. @@ -861,18 +873,33 @@ func (l *loggingT) exit(err error) { type syncBuffer struct { logger *loggingT *bufio.Writer - file *os.File - sev severity - nbytes uint64 // The number of bytes written to this file + file *os.File + sev severity + nbytes uint64 // The number of bytes written to this file + maxbytes uint64 // The max number of bytes this syncBuffer.file can hold before cleaning up. } func (sb *syncBuffer) Sync() error { return sb.file.Sync() } +// CalculateMaxSize returns the real max size in bytes after considering the default max size and the flag options. +func CalculateMaxSize() uint64 { + if logging.logFile != "" { + if logging.logFileMaxSizeMB == 0 { + // If logFileMaxSizeMB is zero, we don't have limitations on the log size. + return math.MaxUint64 + } + // Flag logFileMaxSizeMB is in MB for user convenience. + return logging.logFileMaxSizeMB * 1024 * 1024 + } + // If "log_file" flag is not specified, the target file (sb.file) will be cleaned up when reaches a fixed size. + return MaxSize +} + func (sb *syncBuffer) Write(p []byte) (n int, err error) { - if sb.nbytes+uint64(len(p)) >= MaxSize { - if err := sb.rotateFile(time.Now()); err != nil { + if sb.nbytes+uint64(len(p)) >= sb.maxbytes { + if err := sb.rotateFile(time.Now(), false); err != nil { sb.logger.exit(err) } } @@ -885,13 +912,15 @@ func (sb *syncBuffer) Write(p []byte) (n int, err error) { } // rotateFile closes the syncBuffer's file and starts a new one. -func (sb *syncBuffer) rotateFile(now time.Time) error { +// The startup argument indicates whether this is the initial startup of klog. +// If startup is true, existing files are opened for appending instead of truncated. +func (sb *syncBuffer) rotateFile(now time.Time, startup bool) error { if sb.file != nil { sb.Flush() sb.file.Close() } var err error - sb.file, _, err = create(severityName[sb.sev], now) + sb.file, _, err = create(severityName[sb.sev], now, startup) sb.nbytes = 0 if err != nil { return err @@ -899,6 +928,10 @@ func (sb *syncBuffer) rotateFile(now time.Time) error { sb.Writer = bufio.NewWriterSize(sb.file, bufferSize) + if sb.logger.skipLogHeaders { + return nil + } + // Write header. var buf bytes.Buffer fmt.Fprintf(&buf, "Log file created at: %s\n", now.Format("2006/01/02 15:04:05")) @@ -923,10 +956,11 @@ func (l *loggingT) createFiles(sev severity) error { // has already been created, we can stop. for s := sev; s >= infoLog && l.file[s] == nil; s-- { sb := &syncBuffer{ - logger: l, - sev: s, + logger: l, + sev: s, + maxbytes: CalculateMaxSize(), } - if err := sb.rotateFile(now); err != nil { + if err := sb.rotateFile(now, true); err != nil { return err } l.file[s] = sb @@ -934,7 +968,7 @@ func (l *loggingT) createFiles(sev severity) error { return nil } -const flushInterval = 30 * time.Second +const flushInterval = 5 * time.Second // flushDaemon periodically flushes the log file buffers. func (l *loggingT) flushDaemon() { diff --git a/vendor/k8s.io/klog/klog_file.go b/vendor/k8s.io/klog/klog_file.go index b76a4e10bec0..e4010ad4df06 100644 --- a/vendor/k8s.io/klog/klog_file.go +++ b/vendor/k8s.io/klog/klog_file.go @@ -97,9 +97,11 @@ var onceLogDirs sync.Once // contains tag ("INFO", "FATAL", etc.) and t. If the file is created // successfully, create also attempts to update the symlink for that tag, ignoring // errors. -func create(tag string, t time.Time) (f *os.File, filename string, err error) { +// The startup argument indicates whether this is the initial startup of klog. +// If startup is true, existing files are opened for appending instead of truncated. +func create(tag string, t time.Time, startup bool) (f *os.File, filename string, err error) { if logging.logFile != "" { - f, err := os.Create(logging.logFile) + f, err := openOrCreate(logging.logFile, startup) if err == nil { return f, logging.logFile, nil } @@ -113,7 +115,7 @@ func create(tag string, t time.Time) (f *os.File, filename string, err error) { var lastErr error for _, dir := range logDirs { fname := filepath.Join(dir, name) - f, err := os.Create(fname) + f, err := openOrCreate(fname, startup) if err == nil { symlink := filepath.Join(dir, link) os.Remove(symlink) // ignore err @@ -124,3 +126,14 @@ func create(tag string, t time.Time) (f *os.File, filename string, err error) { } return nil, "", fmt.Errorf("log: cannot create log: %v", lastErr) } + +// The startup argument indicates whether this is the initial startup of klog. +// If startup is true, existing files are opened for appending instead of truncated. +func openOrCreate(name string, startup bool) (*os.File, error) { + if startup { + f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + return f, err + } + f, err := os.Create(name) + return f, err +}