Skip to content

Commit 5adea29

Browse files
authored
Merge pull request #6255 from serathius/logging-formats
Documment logging formats
2 parents 37ae09d + 1317bc7 commit 5adea29

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

contributors/devel/sig-instrumentation/logging.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,36 @@ As per the comments, the practical default level is V(2). Developers and QE
3939
environments may wish to run at V(3) or V(4). If you wish to change the log
4040
level, you can pass in `-v=X` where X is the desired maximum level to log.
4141

42-
## Logging header formats
42+
## Logging Formats
4343

44-
An example logging message is
44+
Kubernetes supports multiple logging formats to allow users flexibility in picking a format that best matches their needs.
45+
As new features and logging formats are proposed, we expect that the Kubernetes community might find itself in disagreement about what formats and features should be officially supported.
46+
This section will serve as a developer documentation describing high level goals of each format, giving a holistic vision of logging options and allowing the community to resolve any future disagreement when making changes.
47+
48+
### Text logging format
49+
50+
* Default format in Kubernetes.
51+
* Purpose:
52+
* Maintain backward compatibility with klog format.
53+
* Human-readable
54+
* Development
55+
* Features:
56+
* Minimal support for structured metadata - required to maintain backward compatibility.
57+
* Marshals object using fmt.Stringer interface allowing to provide a human-readable reference. For example `pod="kube-system/kube-dns"`.
58+
* Support for multi-line strings with actual line breaks - allows dumping whole yaml objects for debugging.
59+
60+
Logs written using text format will always have standard header, however message format differ based on whether string formatting (Infof, Errorf, ...) or structured logging method was used to call klog (InfoS, ErrorS).
61+
62+
Examples, first written using Infof, in second InfoS was used.
4563
```
46-
I0528 19:15:22.737538 47512 logtest.go:52] msg...
64+
I0528 19:15:22.737538 47512 logtest.go:52] Pod kube-system/kube-dns status was updated to ready
65+
I0528 19:15:22.737538 47512 logtest.go:52] "Pod status was updated" pod="kube-system/kube-dns" status="ready"
4766
```
4867

49-
This log line have this form:
68+
Explanation of header:
5069
```
51-
Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg...
70+
Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg...
71+
5272
where the fields are defined as follows:
5373
L A single character, representing the log level (eg 'I' for INFO)
5474
mm The month (zero padded; ie May is '05')
@@ -59,4 +79,27 @@ where the fields are defined as follows:
5979
line The line number
6080
msg The user-supplied message
6181
```
82+
6283
See more in [here](https://github.com/kubernetes/klog/blob/9ad246211af1ed84621ee94a26fcce0038b69cd1/klog.go#L581-L597)
84+
85+
### JSON logging format
86+
87+
* Requires passing `--logging-format=json` to enable
88+
* Purpose:
89+
* Provide structured metadata in efficient way.
90+
* Optimized for efficient log consumption, processing and querying.
91+
* Machine-readable
92+
* Feature:
93+
* Full support for structured metadata, using `logr.MarshalLog` when available.
94+
* Multi-line strings are quoted to fit into a single output line.
95+
96+
Example:
97+
```json
98+
{"ts": 1580306777.04728,"v": 4,"msg": "Pod status was updated","pod":{"name": "kube-dns","namespace": "kube-system"},"status": "ready"}
99+
```
100+
101+
Keys with special meaning:
102+
* ts - timestamp as Unix time (required, float)
103+
* v - verbosity (only for info and not for error messages, int)
104+
* err - error string (optional, string)
105+
* msg - message (required, string)

0 commit comments

Comments
 (0)