1
0
mirror of https://github.com/golang/go synced 2024-11-23 04:30:03 -07:00
Commit Graph

139 Commits

Author SHA1 Message Date
Jonathan Amsterdam
db7bb2742c log/slog: handle times with undefined UnixNanos
slog tries to represent a time.Time without allocations, which involves
storing its UnixNanos value. But UnixNanos is undefined for some valid
times. Provide a fallback representation for those times by storing them
in the `any` field of `Value`.

Fixes #65902.

Change-Id: I736c739a92f77d7b1122ea0831524acdd2c4703f
Reviewed-on: https://go-review.googlesource.com/c/go/+/585519
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
2024-05-16 16:12:08 +00:00
Kevin Burke
13a1f39ade log/slog: use plain string concatenation for Attr.String
Plain string concatenation with the plus operator for Attr.String is
much faster than invoking fmt.Sprintf. Added a benchmark to verify
this (just running on my Mac with stuff in the background but should
be sufficient to demonstrate the effect).

name          old time/op  new time/op  delta
AttrString-8  1.24µs ± 3%  0.43µs ± 0%  -65.17%  (p=0.000 n=20+17)

name          old alloc/op   new alloc/op   delta
AttrString-8      432B ± 0%      152B ± 0%   ~     (p=1.000 n=1+1)

name          old allocs/op  new allocs/op  delta
AttrString-8      30.0 ± 0%      16.0 ± 0%   ~     (p=1.000 n=1+1)

Change-Id: I18ac91cbff1047d168b51a595601e36b5f676615
Reviewed-on: https://go-review.googlesource.com/c/go/+/573517
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2024-03-25 18:23:25 +00:00
Joel Sing
4ce008d7d3 log/slog: correct formatting
Code added in CL #558755 and CL #561315 (both via github) were not
appropriately formatted. Run gofmt to address this.

Change-Id: Iaf7abca9c2ad4e0fabecc417234b743b6b15e9c9
Reviewed-on: https://go-review.googlesource.com/c/go/+/564719
Run-TryBot: Joel Sing <joel@sing.id.au>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
2024-02-20 20:44:14 +00:00
pgxiaolianzi
b6ca586181 log/slog: add test case for level_test.go
adds a test case for the MarshalJSON and MarshalText method of the Level type in the slog package.

Change-Id: I3f79f0b46c41252ad9d743e03e34503e19998f3e
GitHub-Last-Rev: dab00d4c20
GitHub-Pull-Request: golang/go#65525
Reviewed-on: https://go-review.googlesource.com/c/go/+/561315
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
2024-02-15 14:41:24 +00:00
Joe Tsai
cfe7f21dd5 log/slog: document atomicity guarantees and ordering limitations of handlers
The new wording is slightly more strict than before in that
it guarantees each Write only contains exactly one Record,
while the previous wording opened up the possibility for
multiple Records in a Write call.

We also add a comment about the lack of sorting guarantees for
concurrently logged Records. That is, the obtained lock only covers
the Write call, rather than the combination of the call to time.Now,
JSON/text serialization, and also the Write call.

Change-Id: Ia65c50579215a35a1f5b2952c6954ddb60e7fe66
Reviewed-on: https://go-review.googlesource.com/c/go/+/563976
Reviewed-by: Jonathan Amsterdam <jba@google.com>
TryBot-Bypass: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
2024-02-15 14:35:48 +00:00
pgxiaolianzi
702d609a31 log/slog: add missing test in value_test.go
TestValueEqual: Added test cases for DurationValue, StringValue, and TimeValue types.

TestValueAny: Added a test case for string values.

Change-Id: I74ef15bcfcacd0bf5d990debd24846283edf1ecd
GitHub-Last-Rev: 6adb9f6f31
GitHub-Pull-Request: golang/go#65303
Reviewed-on: https://go-review.googlesource.com/c/go/+/558755
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Auto-Submit: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-01-30 20:10:22 +00:00
Jonathan Amsterdam
759849187f log/slog: clarify SetDefault behavior
Change-Id: I53250ca9e6d1ba69cc667949bebb5be2651a020d
Reviewed-on: https://go-review.googlesource.com/c/go/+/530477
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
2024-01-08 18:26:18 +00:00
Danil Timerbulatov
527829a7cb all: remove newline characters after return statements
This commit is aimed at improving the readability and consistency
of the code base. Extraneous newline characters were present after
some return statements, creating unnecessary separation in the code.

Fixes #64610

Change-Id: Ic1b05bf11761c4dff22691c2f1c3755f66d341f7
Reviewed-on: https://go-review.googlesource.com/c/go/+/548316
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2023-12-14 17:22:18 +00:00
Jes Cok
af5d544b6d log/slog: correct doc examples for Handler.WithGroup
Fixes #64519

Change-Id: Iaea5de74402b77eb5a42979bd3baa1276b13d585
GitHub-Last-Rev: 5080a257b2
GitHub-Pull-Request: golang/go#64551
Reviewed-on: https://go-review.googlesource.com/c/go/+/547215
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jes Cok <xigua67damn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-12-05 18:18:13 +00:00
Jes Cok
631a6c2abf all: add missing copyright header
Change-Id: Ic61fb181923159e80a86a41582e83ec466ab9bc4
GitHub-Last-Rev: 9246984566
GitHub-Pull-Request: golang/go#64080
Reviewed-on: https://go-review.googlesource.com/c/go/+/541741
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Run-TryBot: Jes Cok <xigua67damn@gmail.com>
2023-11-17 23:34:11 +00:00
Jes Cok
7bf540012c log/slog: fix comments for *handleState.appendAttr
Change-Id: I8b9a6d177d297b9b1e890bf1d8a46a4b3311910f
GitHub-Last-Rev: 24067b8714
GitHub-Pull-Request: golang/go#64070
Reviewed-on: https://go-review.googlesource.com/c/go/+/541519
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jes Cok <xigua67damn@gmail.com>
2023-11-17 16:28:15 +00:00
Jes Cok
c50e7ded1f log/slog: make hex a const
hex is in fact immutable, declare it as a const to avoid accidental
modification, also for consistency with other packages.

Change-Id: I88a6fed34d27ac57a0332dc7a27d3bf2ba0d6272
GitHub-Last-Rev: 08712be2d4
GitHub-Pull-Request: golang/go#64069
Reviewed-on: https://go-review.googlesource.com/c/go/+/541518
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-11-17 16:18:11 +00:00
lotusirous
e41fb0fd1a slog: factoring out code to make the examples playable
For #61885

Change-Id: I5a0006fec2899dcbc989207174f438ecbfcc63ff
Reviewed-on: https://go-review.googlesource.com/c/go/+/536215
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-11-17 14:56:30 +00:00
Andy Pan
3188758653 log/slog: add LogLoggerLevel to enable setting level on the default logger
Fixes #62418

Change-Id: I889a53d00c8a463b4d7ddb41893c000d7cd0e7b8
Reviewed-on: https://go-review.googlesource.com/c/go/+/525096
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
2023-11-10 21:25:30 +00:00
Kevin Burke
fa903593fb log/slog: fix method name in docs
My hunch is the method was renamed during the development process but
the code comment was not updated to match.

Change-Id: Ib7aafc863f82a6bbe09e68dc3c5e087e16f228a9
Reviewed-on: https://go-review.googlesource.com/c/go/+/540536
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-11-08 16:30:56 +00:00
Allen Li
1e95fc7ffe log/slog: Reorder doc comment for level constants
pkgsite and go doc print the doc comment *after* the code, resulting in:

    const (
            LevelDebug Level = -4
            ...
    )

    Many paragraphs...

    Names for common levels.

The "Names for common levels." feels out of place and confusing at the bottom.

This is also consistent with the recommendation for the first sentence in doc comments to be the "summary".

Change-Id: I656e85e27d2a4b23eaba5f2c1f4f811a88848c83
GitHub-Last-Rev: d9f7ee9b94
GitHub-Pull-Request: golang/go#61943
Reviewed-on: https://go-review.googlesource.com/c/go/+/518537
Reviewed-by: Alan Donovan <alan@alandonovan.net>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: qiulaidongfeng <2645477756@qq.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
2023-10-30 17:34:43 +00:00
cui fliter
f09db2bb93 log: add available godoc link
Change-Id: I80f1377631163cc5843379c36ca10b82fccd5709
Reviewed-on: https://go-review.googlesource.com/c/go/+/535086
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-10-23 22:56:07 +00:00
Jonathan Amsterdam
deb8e29000 log/slog: JSONHandler elides empty groups even with replacement
Previously, the built-in handlers assumed a group was empty if and
only if it had no attributes. But a ReplaceAttr function that
returned an empty Attr could produce an empty group even if the group
had attrs prior to replacement.

The obvious solution, doing the replacement first and then checking,
would require allocating storage to hold the replaced Attrs.  Instead,
we write to the buffer, and if no attributes were written, we back up
to before the group name.

Fixes #62512.

Change-Id: I140e0901f4b157e36594d8d476f1ab326f8f2c2a
Reviewed-on: https://go-review.googlesource.com/c/go/+/529855
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-10-02 13:57:53 +00:00
Kir Kolyshkin
0de57ebf88 log/slog: fix time regexp in test
CL 525556 started using timeRE regexp to match time output from JSON
handler, and relaxed it to allow arbitrary (rather than fixed 3 digit)
precision.

What it missed is in JSON handler the fractional part is omitted
entirely (together with the decimal dot) when the nanoseconds field is
0.

As a result, there are occasional CI failures in js/wasm (which, I guess,
has better chances to return zero nanoseconds).

To fix the flaky test, let's use two different regular expressions,
tailored to text and JSON.

Change-Id: Ie98990fcf278bb0916ab31c9177e6b22a523062a
Reviewed-on: https://go-review.googlesource.com/c/go/+/530675
Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Andy Pan <panjf2000@gmail.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
2023-09-27 01:15:17 +00:00
Jonathan Amsterdam
5e9afab7df log/slog: ensure ReplaceAttr does not see a group
The ReplaceAttr function should not see groups, only leaf attributes.

Previously, we checked an Value for being a group, then resolved it,
then called ReplaceAttr. We neglected to see if it was a group
after resolving it.

Now we resolve first, then check.

Fixes #62731.

Change-Id: I2fc40758e77c445f82deb2c9de8cae7a3b0e22cf
Reviewed-on: https://go-review.googlesource.com/c/go/+/530478
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-09-25 20:53:33 +00:00
Chris Duncan
65c53a1833 log/slog: fix Record.back slice always too small during Add()
When slog.Record.Add(args) is called, with enough args to cause the
Record.back []Attr to be created, it is being created 1 too small, which
results in it immediately being grown again by append before the function
exits (needless allocation and copying).
This is because it is created with a capacity equal to countAttrs(args),
but forgets that there is an additional attribute to be appended: a
(args is just the remaining unconsumed attributes).
This PR fixes that by adding 1 to the capacity to account for the `a` attribute.

Additionally, when Record.back already exists, it will most likely be at
max capacity already. Rather than append to it and risk having it grown
multiple times, or grow too large, this adds a slices.Grow call to set it
to the right capacity, similar to what is already done in the
Record.AddAttrs(attrs) function.

Change-Id: Ic4bcf45909fe4436c586ccd2b8d61f24606b6be8
GitHub-Last-Rev: 4c924b610a
GitHub-Pull-Request: golang/go#62388
Reviewed-on: https://go-review.googlesource.com/c/go/+/524618
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2023-09-14 17:38:25 +00:00
Andy Pan
f72693d3e9 log/slog: generate valid JSON string with empty attributes in Groups
Fixes #62152

Change-Id: I1f76ab26eae3a44292ebe6003006258d5e31d50f
Reviewed-on: https://go-review.googlesource.com/c/go/+/521135
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Andy Pan <panjf2000@gmail.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-09-11 20:29:15 +00:00
Andy Pan
31c1f4a611 log/slog: use the general regex expression of datetime in TestPanics
When I added this test in CL 514135, I missed the timeRE,
we should use it to make this test more precise.

Change-Id: I486f8e8bbbc3a17166107ef361d242ddf4ea2928
Reviewed-on: https://go-review.googlesource.com/c/go/+/525556
Run-TryBot: Andy Pan <panjf2000@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-09-07 17:11:06 +00:00
Sean Liao
c9911b9a84 log/slog: package doc fixes
Fixes #62286

Change-Id: If463436e3b5ba6e4eb850097395e00482d0dc671
Reviewed-on: https://go-review.googlesource.com/c/go/+/523196
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Sean Liao <sean@liao.dev>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
2023-08-30 19:31:37 +00:00
Mateusz Poliwczak
111ab75432 log/slog: use atomic.Pointer instead of atomic.Value
There is no need to use atomic.Value, atomic.Pointer should also
be more performant here.

Change-Id: I04cc1befbe1112665a9d9e16197ea3caef5e5fa3
GitHub-Last-Rev: 577902519f
GitHub-Pull-Request: golang/go#62288
Reviewed-on: https://go-review.googlesource.com/c/go/+/523035
TryBot-Result: Gopher Robot <gobot@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Mateusz Poliwczak <mpoliwczak34@gmail.com>
2023-08-29 14:56:38 +00:00
Sean Liao
92f7209ebd log/slog: fix documented value type for SourceKey
Fixes #62294

Change-Id: I5df0d8bdf5ae8bc11ffa0e66e3e8776fd3e565a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/523195
Run-TryBot: Sean Liao <sean@liao.dev>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Joedian Reid <joedian@golang.org>
2023-08-29 13:26:51 +00:00
Artem Teleshev
bcf83825ca log/slog: rename receiver name for the Value struct from a to v
Change-Id: Ib06234e781ed2faf5fa36efeda75c5ae439890ba
GitHub-Last-Rev: 760af94367
GitHub-Pull-Request: golang/go#61886
Reviewed-on: https://go-review.googlesource.com/c/go/+/517556
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
2023-08-17 21:35:14 +00:00
Joe Tsai
d63652b7c6 log/slog: simplify and optimize RFC3339 handling
In CL 421877 and CL 444278, time.Time.AppendFormat has been
specially optimized for the time.RFC3339Nano representation.
Relying on that optimization and modify the output to obtain the
fixed-width millisecond resolution that slog uses.

This both removes a lot of code and also improves performance:

	name       old time/op  new time/op  delta
	WriteTime  93.0ns ± 1%  80.8ns ± 0%  -13.17%  (p=0.000 n=8+9)

Change-Id: I61e8f4476c111443e3e2098a45b2c21a76137345
Reviewed-on: https://go-review.googlesource.com/c/go/+/478757
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
2023-08-17 16:48:35 +00:00
Jes Cok
5b8ceb38a1 log/slog: remove redundant dot in doc
Change-Id: Ic4fcfe7335dab219790c19ded3bbb7265857404f
GitHub-Last-Rev: afc69c79b2
GitHub-Pull-Request: golang/go#62062
Reviewed-on: https://go-review.googlesource.com/c/go/+/519955
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
2023-08-16 19:01:27 +00:00
Andy Pan
4e728e5121 log/slog: restore the original log setting before test exits
Change-Id: Ib3daffb8a4cc018d62ed6e5741355b1c1a206034
Reviewed-on: https://go-review.googlesource.com/c/go/+/515775
Run-TryBot: Andy Pan <panjf2000@gmail.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
2023-08-07 16:07:50 +00:00
Jonathan Amsterdam
cf39736f87 log/slog: don't panic on aliased Record
If the shared slice in a copied is modified, make a copy of it
and insert an attribute that warns of the bug.

Previously, we panicked, and panics in logging code should be avoided.

Change-Id: I24e9b0bf5c8cd09cf733e7dae8a82d025ef214e5
Reviewed-on: https://go-review.googlesource.com/c/go/+/513760
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-08-07 15:10:55 +00:00
Andy Pan
73667209c1 log/slog: catch panics during formatting
Fixes #61648

Change-Id: I6b7f4948ca89142a358d74624607daf42ea8b304
Reviewed-on: https://go-review.googlesource.com/c/go/+/514135
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Andy Pan <panjf2000@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
2023-08-03 21:34:15 +00:00
zikaeroh
050d4d3b9e log/slog: fix comment above log levels
This extra newline causes pkg.go.dev and gopls to only show the bottom
half of this comment; I'm pretty sure this entire thing is meant to be
in the docs.

Change-Id: I5bbf081fb2072d9d773d5a995bc3693dc44f65ff
Reviewed-on: https://go-review.googlesource.com/c/go/+/511855
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-07-21 14:43:18 +00:00
Jonathan Amsterdam
fe5af1532a log/slog: doc commonHandler.handle
Change-Id: Id301b772e472e1cb7cd8bccaa5a13ff7b6f94711
Reviewed-on: https://go-review.googlesource.com/c/go/+/509596
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
2023-07-14 21:29:53 +00:00
Will Roden
847d40d699 log/slog: fix issue with concurrent writes
This causes instances commonHandler created by withAttrs or withGroup to
share a mutex with their parent preventing concurrent writes to their
shared writer.

Fixes #61321

Change-Id: Ieec225e88ad51c84b41bad6c409fac48c90320b2
Reviewed-on: https://go-review.googlesource.com/c/go/+/509196
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
2023-07-14 17:30:35 +00:00
Jonathan Amsterdam
651869716a log/slog: replace nil contexts with context.Background()
Passing nil for a context is discouraged. We should avoid it.

Fixes #61219.

Change-Id: I664387070aacb56af580b6b0791ca40982d2a711
Reviewed-on: https://go-review.googlesource.com/c/go/+/508437
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2023-07-11 17:06:26 +00:00
Jonathan Amsterdam
3f8b04bfb5 log/slog: change XXXCtx functions to XXXContext
Fixes #61200.

Change-Id: I3071dbf673bcd2d24e62644b40d481c953703978
Reviewed-on: https://go-review.googlesource.com/c/go/+/508195
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-07-11 16:33:46 +00:00
Jonathan Amsterdam
d3d78b4bcc log/slog: handle recursively empty groups
Handlers should not display empty groups.

A group with no attributes is certainly empty. But we also want to
consider a group to be empty if all its attributes are empty groups.
The built-in handlers did not handle this second case properly.
This CL fixes that.

There are two places in the implementation that we need to consider.

For Values of KindGroup, we change the GroupValue constructor to omit
Attrs that are empty groups. A Group is then empty if and only if it
has no Attrs. This avoids a recursive check for emptiness.
It does require allocation, but that doesn't worry us because Group
values should be relatively rare.

For groups established by WithGroup, we avoid opening such groups
unless the Record contains non-empty groups. As we did for values, we
avoid adding empty groups to records in the first place, so we only
need to check that the record has at least one Attr.

We are doing extra work, so we need to make sure we aren't slowing
things down unduly. Benchmarks before and after this change show
minimal differences.

Fixes #61067.

Change-Id: I684c7ca834bbf69210516faecae04ee548846fb7
Reviewed-on: https://go-review.googlesource.com/c/go/+/508436
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-07-07 15:07:02 +00:00
Merrick Clay
36ea4f9680 log/slog: fix faulty test
Adds an optional close quote in the expected log message regex for TestConnections to prevent failing when the source filepath is surrounded in quotes due to it containing one or more spaces.

Fixes #61161

Change-Id: I0dd71fb4389bff963bbfdc668ef4e4dfe787eafc
Reviewed-on: https://go-review.googlesource.com/c/go/+/508055
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-07-06 00:01:44 +00:00
Cuong Manh Le
164aceea08 log/slog: fix broken link to AnyValue in comment
Change-Id: Ida52a2bf6a415017942bf9ccd74a1ea9ed02bc46
Reviewed-on: https://go-review.googlesource.com/c/go/+/505535
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2023-06-23 12:15:52 +00:00
Christopher Taylor
82e17c4d11 log/slog: fix broken link to Record.Clone in package docs
Change-Id: If8b937fa9db89a537ad7d4ccb8c04f84d2cff3db
GitHub-Last-Rev: fdd4338118
GitHub-Pull-Request: golang/go#60938
Reviewed-on: https://go-review.googlesource.com/c/go/+/505075
TryBot-Bypass: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
2023-06-23 12:15:01 +00:00
Jonathan Amsterdam
f3bf18117b log/slog: fix HandlerOptions.ReplaceAttr doc
It said that Attrs with an empty key are ignored.
In fact, zero Attrs are ignored.

Fixes #60870.

Change-Id: I221d3a25b0f0cc9001e06e9cc76bab29292c0741
Reviewed-on: https://go-review.googlesource.com/c/go/+/504535
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-06-20 23:57:50 +00:00
Jonathan Amsterdam
9b57b33556 log/slog: add link to handler-writing guide
Add a shortlink in the doc to a guide to writing handlers, which is a work
in progress.

Change-Id: I1b01c90468382ffe53d9ad6f38253906e3f44857
Reviewed-on: https://go-review.googlesource.com/c/go/+/495920
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
2023-05-24 20:58:16 +00:00
Sean Liao
64c95d24f0 log/slog: JSONHandler doesn't use special source format
Fixes #60329

Change-Id: Idb19da4830fa14c459bedbf143d550ce7c1dfdbd
Reviewed-on: https://go-review.googlesource.com/c/go/+/496815
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-05-23 20:22:03 +00:00
Jonathan Amsterdam
6761bff433 log/slog: increase test coverage
Change-Id: I2c7f3ca27ee1b1c2d65d713ffa6256c3cfdd8aad
Reviewed-on: https://go-review.googlesource.com/c/go/+/495979
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-05-22 19:05:59 +00:00
Jonathan Amsterdam
da2476a803 log/slog: improve test coverage of JSON handler
Change-Id: I31e96fc1329bb17937974ed3dbfda3448e53d37e
Reviewed-on: https://go-review.googlesource.com/c/go/+/495978
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-05-22 19:05:49 +00:00
Jonathan Amsterdam
2f1e643229 log/slog: fix check for nil prefix
Previously, handleState.prefix was nil if and only if the length of
the prefix was zero. Now, prefix is never nil.

Fix the nil check in the code by also checking if the length is non-zero.

Change-Id: I9f69c0029cb1c73fe6c2919c78fee7d4085bfd85
Reviewed-on: https://go-review.googlesource.com/c/go/+/495977
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
2023-05-22 18:27:16 +00:00
Jonathan Amsterdam
bc96901e89 log/slog: empty calls to With and WithGroup are no-ops
It doesn't make sense to call Logger.WithGroup with the empty string.
Make it a no-op by returning the receiver.
This relieves handlers of the burden of detecting that case themselves.

Less importantly, but for consistency, if Logger.With is called with
no args, make it a no-op by returning the receiver.

Along the way, fix obsolete mentions of "the Logger's context" in the
doc.

Change-Id: Ia6caa4f1ca70c1c4b0cab3e222b2fda48be73fef
Reviewed-on: https://go-review.googlesource.com/c/go/+/496175
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
2023-05-22 18:27:07 +00:00
Jonathan Amsterdam
6d4d71c5ab cmd/vet: add slog checker
Add the slog static analysis pass to `go vet`.

Vendor in golang.org/x/tools@master to pick up the pass.

Tweak a test in slog to avoid triggering the vet check.

Change-Id: I55ceac9a4e6876c8385897784542761ea0af2481
Reviewed-on: https://go-review.googlesource.com/c/go/+/496156
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-05-22 18:11:19 +00:00
Jonathan Amsterdam
6fc5e7d4b5 log/slog: create prefix buffer earlier
It's possible that the replacement for a built-in attribute is a Group.
That would cause a nil pointer exception because the handleState.prefix
field isn't set until later, in appendNonBuiltIns.

So create the prefix field earlier, at the start of commonHandler.handle.

Once we do this, we can simplify the code by creating and freeing the
prefix in newHandleState.

Along the way I discovered a line that wasn't being tested:
	state.prefix.WriteString(h.groupPrefix)
so I modified an existing test case to cover it.

Change-Id: Ib385e3c13451017cb093389fd5a1647d53e610bf
Reviewed-on: https://go-review.googlesource.com/c/go/+/494037
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-05-16 18:01:59 +00:00