Skip to content

Commit d14dd59

Browse files
authored
Merge pull request #6283 from serathius/logging-guidelines
Update logging guidelines
2 parents 0d741fd + e5eed38 commit d14dd59

File tree

2 files changed

+92
-41
lines changed

2 files changed

+92
-41
lines changed

contributors/devel/sig-instrumentation/logging.md

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,77 @@
1-
## Logging Conventions
1+
## Logging
22

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.
68

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
824
but just return `error`, because client libraries may be used in CLI UIs that wish to control output.
925

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
1127

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.
1437

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).
38+
Prototype:
39+
```go
40+
func InfoS(message string, keysAndValues ...interface{})
41+
func InfoSDepth(depth int, message string, keysAndValues ...interface{})
42+
func ErrorS(err error, message string, keysAndValues ...interface{})
43+
func ErrorSDepth(depth int, err error, message string, keysAndValues ...interface{})
44+
```
45+
46+
Example:
47+
```go
48+
klog.InfoS("Received HTTP request", "method", "GET", "URL", "/metrics", "latency", time.Second)
49+
```
50+
51+
### What method to use?
52+
53+
* `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.
1655

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
1959
* Programmer errors
2060
* Logging extra info about a panic
2161
* 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.
2363
* Information about config (listening on X, watching Y)
2464
* 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.
2666
* Logging HTTP requests and their exit code
2767
* System state changing (killing pod)
2868
* Controller state change events (starting pods)
2969
* Scheduler log messages
30-
* klog.V(3) - Extended information about changes
70+
* `klog.V(3).InfoS` - Extended information about changes
3171
* More info about system state changes
32-
* klog.V(4) - Debug level verbosity
72+
* `klog.V(4).InfoS` - Debug level verbosity
3373
* 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
3575
* Context to understand the steps leading up to errors and warnings
3676
* More information for troubleshooting reported issues
3777

contributors/devel/sig-instrumentation/migration-to-structured-logging.md

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ With organized, milestone-based, large-scale migration we try to target delivera
1919

2020
For non-organized migrations, the onus is, generally, on the individual contributors.
2121

22-
* Organized Migration
22+
#### Organized Migration
2323

2424
Organized migration is carried out in a two state cycle aligning with the cadence of Kubernetes releases.
2525

2626
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.
2727

2828
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.
2929

30-
* Non-organized migration
30+
#### Non-organized migration
3131

3232
As aforementioned, our non-organized migration efforts are spearheaded by individual contributors who need to migrate particular code sections to utilize new features early.
3333

@@ -40,7 +40,8 @@ Before sending a PR our way, please ensure that there isn't one already in place
4040
### Current status
4141

4242
* 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.
4445

4546
## Sending a Structured Logging Migration Pull Request
4647

@@ -172,7 +173,7 @@ type KMetadata interface {
172173
1. Change log functions to structured equivalent
173174
1. Remove string formatting from log message
174175
1. Name arguments
175-
1. Use `klog.KObj` and `klog.KRef` for Kubernetes objects references
176+
1. Ensure that value is properly passed
176177
1. Verify log output
177178

178179
## Change log functions to structured equivalent
@@ -378,32 +379,35 @@ func ChangePodStatus(newStatus, currentStatus string) {
378379
[lowerCamelCase]: https://en.wiktionary.org/wiki/lowerCamelCase
379380
[alphanumeric]: https://en.wikipedia.org/wiki/Alphanumeric
380381

381-
### Use `klog.KObj` and `klog.KRef` for Kubernetes objects
382+
## Good practice for passing values in structured logging
382383

383-
As part of structured logging migration we want to ensure that kubernetes objects references are consistent within the
384-
codebase. Two new utility functions were introduced to klog `klog.KObj` and `klog.KRef`. Any reference
385-
(name, uid, namespace) to Kubernetes Object (Pod, Node, Deployment, CRD) should be rewritten to utilize those functions.
386-
In situations when object `UID` is would be beneficial for log, it should be added as separate field with `UID` suffix.
384+
When passing a value for a key-value pair, please use following rules:
385+
* Prefer using Kubernetes objects (for example `*v1.Pod`) and log them using `klog.KObj`
386+
* When the original object is not available, use `klog.KRef` instead
387+
* Pass structured values directly (avoid calling `.String()` on them first)
388+
* When the goal is to log a `[]byte` array as string, explicitly convert with `string(<byte array>)`.
389+
390+
### Prefer using Kubernetes objects (for example `*v1.Pod`) and log them using `klog.KObj`
391+
392+
As part of the structured logging migration, we want to ensure that Kubernetes object references are
393+
consistent within the
394+
codebase. Two new utility functions were introduced to klog: `klog.KObj` and `klog.KRef`.
395+
396+
Any existing logging code that makes a reference (such as name, namespace) to a Kubernetes
397+
object (for example: Pod, Node, Deployment, CustomResourceDefinition) should be rewritten to
398+
utilize the new functions.
399+
400+
Logging using this method will ensure that log output include a proper and correctly formatted reference
401+
to that object. The formatting / serialization is automatically selected depending on the output log format.
402+
For example, the same object could be represented as `<namespace>/<name>` in
403+
plain text and as `{"namespace": "<namespace>", "name": "<name>"}` in JSON.
404+
In situations when object `UID` is would be beneficial for log, it should be added as separate key with `UID` suffix.
387405

388406
For example
389407
```go
390408
func updatePod(pod *covev1.Pod) {
391409
...
392-
klog.Infof("Updated pod %s in namespace %s", pod.Name, pod.Namespace)
393-
}
394-
```
395-
should be changed to
396-
```go
397-
func updatePod(pod *covev1.Pod) {
398-
...
399-
klog.InfoS("Updated pod", "pod", klog.KObj(pod))
400-
}
401-
```
402-
And
403-
```go
404-
func updatePod(pod *covev1.Pod) {
405-
...
406-
klog.Infof("Updated pod with uid: %s", pod.Uid)
410+
klog.Infof("Updated pod(%s) with name %s in namespace %s", pod.Uid, pod.Name, pod.Namespace)
407411
}
408412
```
409413
should be changed to
@@ -414,6 +418,7 @@ func updatePod(pod *covev1.Pod) {
414418
}
415419
```
416420

421+
### When the original object is not available, use `klog.KRef` instead
417422
`klog.KObj` requires passing a kubernetes object (struct implementing `metav1.Object` interface). In situations where
418423
the object is not available, we can use `klog.KRef`. Still it is suggested to rewrite the code to use object pointer
419424
instead of strings where possible.
@@ -434,7 +439,13 @@ func updateNode(nodeName string) {
434439
}
435440
```
436441

437-
### Verify log output
442+
### Pass structured values directly
443+
444+
By passing whole structure without any marshalling we can allow logging library to make the decision for us.
445+
This is especially beneficial when Kubernetes supports different log format, so the logging library can make decision based on that.
446+
For example using `<namespace>/<name>` when writing in text format and `{"namespace": "<namespace>", "name": "<name>"}` for json format.
447+
448+
## Verify log output
438449

439450
With the introduction of structured functions log arguments will be formatted automatically instead of depending on the
440451
caller. This means that we can remove the burden of picking the format by caller and ensure greater log consistency, but during

0 commit comments

Comments
 (0)