You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contributors/devel/sig-instrumentation/logging.md
+56-16Lines changed: 56 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,37 +1,77 @@
1
-
## Logging Conventions
1
+
## Logging
2
2
3
-
The following conventions for the klog levels to use.
4
-
[klog](http://godoc.org/github.com/kubernetes/klog) is globally preferred to
5
-
[log](http://golang.org/pkg/log/) for better runtime control.
3
+
This document provides an overview of the recommended way to develop and implement
4
+
logging for components of Kubernetes.
5
+
For [k/kubernetes](https://github.com/kubernetes/kubernetes) these conventions should be seen
6
+
as a strong recommendation, however any Kubernetes or external project is welcome to follow
7
+
the guidelines below. Most of the ideas can be applied more broadly.
6
8
7
-
Shared libraries, such as `client-go`, should not use `klog.ErrorS()` and `klog.InfoS()`,
9
+
### Logging in Kubernetes
10
+
11
+
Kubernetes project uses [klog](https://github.com/kubernetes/klog) for logging.
12
+
This library was created as a permanent fork of glog, as the original project was no longer developed.
13
+
With the introduction of [Structured Logging](https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/1602-structured-logging) and [Contextual Logging](https://github.com/kubernetes/enhancements/blob/master/keps/sig-instrumentation/3077-contextual-logging/README.md), Kubernetes is being migrated to use [logr](https://github.com/go-logr/logr) as its logging interface. Any changes in klog are done to ensure a smooth transition to `logr`. Klog is then only used to manage the logging configuration (`SetLogger`, retrieving the logger) and its logging calls should not be used anymore.
14
+
``
15
+
16
+
For projects that want to integrate with Kubernetes or write the same log format, retrieving a `logr.Logger` instance from klog will return an implementation that emits log output the same way as the traditional klog logging calls.
17
+
18
+
### Logging Conventions
19
+
20
+
### When not to log?
21
+
22
+
Shared libraries, such as [`client-go`](https://github.com/kubernetes/client-go), should _not_
23
+
log errors themselves
8
24
but just return `error`, because client libraries may be used in CLI UIs that wish to control output.
9
25
10
-
Please see the [Structured Logging Guide](migration-to-structured-logging.md#structured-logging-in-kubernetes) for more information on how to set keys and values in structured logs.
26
+
### How to log
11
27
12
-
* klog.ErrorS() - Errors should be used to indicate unexpected behaviours in code, like unexpected errors returned by subroutine function calls.
13
-
Logs generated by `ErrorS` command may be enhanced with additional debug information (by logging library). Calling `ErrorS` with `nil` as error may be acceptable if there is error condition that deserves a stack trace at this origin point.
28
+
There are two main klog methods for writing logs: `klog.InfoS` and `klog.ErrorS`. Both methods are part of a [logr](https://github.com/go-logr/logr) compatible interface.
29
+
For those two functions exists additional flavors, like `klog.V(2).InfoS` that allows caller to increase log verbosity (making it only available when `--v=2` flag is provided to binary) and `klog.InfoSDepth` with `klog.ErorSDepth` that give control to change caller information.
30
+
31
+
Any other non-structured (without `S`) klog methods like `klog.Infof` that use old C like string format interface are no longer recommended for use.
32
+
Those log calls using those methods should be migrated to their structured variants.
33
+
Please see the [Structured Logging Guide](migration-to-structured-logging.md#structured-logging-in-kubernetes) for more information on how to migrate such logs.
34
+
35
+
All structured logging methods accept the log message as a string, along with any number of key/value pairs that you provide via a variadic `interface{}` argument.
36
+
As variadic arguments represent key value pairs, they should always be even in count with first element being key of type string and second value of any type matching that key.
14
37
15
-
* klog.InfoS() - Structured logs to the INFO log. `InfoS` should be used for routine logging. It can also be used to log warnings for expected errors (errors that can happen during routine operations).
*`klog.ErrorS` - Errors should be used to indicate unexpected behaviours in code, like unexpected errors returned by subroutine function calls.
54
+
Logs generated by `ErrorS` command may be enhanced with additional debug information (by logging library). Calling `ErrorS` with `nil` as error may be acceptable if there is error condition that deserves a stack trace at this origin point.
16
55
17
-
* klog.InfoS() has multiple levels:
18
-
* klog.V(0) - Generally useful for this to ALWAYS be visible to an operator
56
+
*`klog.InfoS` - Structured logs to the INFO log. `InfoS` should be used for routine logging. It can also be used to log warnings for expected errors (errors that can happen during routine operations).
57
+
Depending on log severity it's important to pick a proper verbosity level to ensure that consumer is neither under nor overwhelmed by log volume:
58
+
*`klog.V(0).InfoS` = `klog.InfoS` - Generally useful for this to **always** be visible to a cluster operator
19
59
* Programmer errors
20
60
* Logging extra info about a panic
21
61
* CLI argument handling
22
-
* klog.V(1) - A reasonable default log level if you don't want verbosity.
62
+
*`klog.V(1).InfoS` - A reasonable default log level if you don't want verbosity.
23
63
* Information about config (listening on X, watching Y)
24
64
* Errors that repeat frequently that relate to conditions that can be corrected (pod detected as unhealthy)
25
-
* klog.V(2) - Useful steady state information about the service and important log messages that may correlate to significant changes in the system. This is the recommended default log level for most systems.
65
+
*`klog.V(2).InfoS` - Useful steady state information about the service and important log messages that may correlate to significant changes in the system. This is the recommended default log level for most systems.
26
66
* Logging HTTP requests and their exit code
27
67
* System state changing (killing pod)
28
68
* Controller state change events (starting pods)
29
69
* Scheduler log messages
30
-
* klog.V(3) - Extended information about changes
70
+
*`klog.V(3).InfoS` - Extended information about changes
31
71
* More info about system state changes
32
-
* klog.V(4) - Debug level verbosity
72
+
*`klog.V(4).InfoS` - Debug level verbosity
33
73
* Logging in particularly thorny parts of code where you may want to come back later and check it
34
-
* klog.V(5) - Trace level verbosity
74
+
*`klog.V(5).InfoS` - Trace level verbosity
35
75
* Context to understand the steps leading up to errors and warnings
36
76
* More information for troubleshooting reported issues
Copy file name to clipboardExpand all lines: contributors/devel/sig-instrumentation/migration-to-structured-logging.md
+36-25Lines changed: 36 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,15 +19,15 @@ With organized, milestone-based, large-scale migration we try to target delivera
19
19
20
20
For non-organized migrations, the onus is, generally, on the individual contributors.
21
21
22
-
* Organized Migration
22
+
####Organized Migration
23
23
24
24
Organized migration is carried out in a two state cycle aligning with the cadence of Kubernetes releases.
25
25
26
26
In the first stage, we pick a particular migration milestone & create an issue to split the work into smaller chunks (for example, migrating just a single file). This ensures that the work can be easily picked up by multiple individual contributors and also avoids conflicts. After the migration activity is complete, we mark the directory as migrated to avoid regressions with the help of tooling that we have developed.
27
27
28
28
In the second milestone, we take a break from migration to analyze results & improve our existing processes. Adding structured information to logs is a very costly & time-consuming affair. Setting aside time in the second stage to collect feedback, analyze the impact of changes on the log volume & performance, and better our PR review process helps us avoid mistakes and duplicate efforts.
29
29
30
-
* Non-organized migration
30
+
####Non-organized migration
31
31
32
32
As aforementioned, our non-organized migration efforts are spearheaded by individual contributors who need to migrate particular code sections to utilize new features early.
33
33
@@ -40,7 +40,8 @@ Before sending a PR our way, please ensure that there isn't one already in place
40
40
### Current status
41
41
42
42
* 1.21 Kubelet was migrated
43
-
* 1.22 We are collecting feedback and making improvements to the migration process.
43
+
* 1.22 Collected feedback and improved the process.
44
+
* 1.23 kube-scheduler and kube-proxy were migrated.
44
45
45
46
## Sending a Structured Logging Migration Pull Request
46
47
@@ -172,7 +173,7 @@ type KMetadata interface {
172
173
1. Change log functions to structured equivalent
173
174
1. Remove string formatting from log message
174
175
1. Name arguments
175
-
1.Use `klog.KObj` and `klog.KRef` for Kubernetes objects references
0 commit comments