Skip to content

Commit 60bf3a8

Browse files
authored
[logs] Support for slog the standard library logger (#294)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description - `slog` will become the standard library logger and we are supporting bridges between logr.Logger and this implementation ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 2d915b4 commit 60bf3a8

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

changes/20230731185357.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: `[logs]` Added support for [slog](https://pkg.go.dev/golang.org/x/exp/slog), the future standard library logger

utils/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ require (
3838
github.com/spf13/pflag v1.0.5
3939
github.com/spf13/viper v1.15.0
4040
github.com/stretchr/testify v1.8.4
41+
github.com/zailic/slogr v0.0.2-alpha
4142
go.uber.org/atomic v1.11.0
4243
go.uber.org/goleak v1.2.1
4344
go.uber.org/zap v1.24.0
45+
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691
4446
golang.org/x/crypto v0.13.0
4547
golang.org/x/net v0.15.0
4648
golang.org/x/sync v0.3.0

utils/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
360360
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
361361
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
362362
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
363+
github.com/zailic/slogr v0.0.2-alpha h1:ZZ+96+AOnk4L9JoPkZ6aGbGXnn90/53A9zm9JcjYSYc=
364+
github.com/zailic/slogr v0.0.2-alpha/go.mod h1:cwplHb/RBT+83E4QzPcVtCs0Z/sAjmgMtC09XGm9SCU=
363365
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
364366
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
365367
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
@@ -403,6 +405,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
403405
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
404406
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
405407
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
408+
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 h1:/yRP+0AN7mf5DkD3BAI6TOFnd51gEoDEb8o35jIFtgw=
409+
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
406410
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
407411
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
408412
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

utils/logs/logrimp/logr_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logrimp
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/bxcodec/faker/v3"
@@ -9,6 +10,7 @@ import (
910
"github.com/sirupsen/logrus"
1011
"github.com/stretchr/testify/require"
1112
"go.uber.org/zap"
13+
"golang.org/x/exp/slog"
1214

1315
"github.com/ARM-software/golang-utils/utils/commonerrors"
1416
)
@@ -40,6 +42,10 @@ func TestLoggerImplementations(t *testing.T) {
4042
Logger: NewLogrusLogger(logrus.New()),
4143
name: "Logrus",
4244
},
45+
{
46+
Logger: NewSlogLogger(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{}))),
47+
name: "slog",
48+
},
4349
}
4450
for i := range tests {
4551
test := tests[i]

utils/logs/logrimp/slog.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package logrimp
2+
3+
import (
4+
"github.com/go-logr/logr"
5+
"github.com/zailic/slogr"
6+
"golang.org/x/exp/slog"
7+
)
8+
9+
// NewSlogLogger returns a new [slog logger](see https://pkg.go.dev/golang.org/x/exp/slog) which will be part of the standard library.
10+
func NewSlogLogger(logger *slog.Logger) logr.Logger {
11+
// FIXME change dependency when needed https://github.com/go-logr/logr/issues/171
12+
return slogr.New(logger)
13+
}

utils/logs/slog_logger.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
// Package logs defines loggers for use in projects.
7+
package logs
8+
9+
import (
10+
"golang.org/x/exp/slog"
11+
12+
"github.com/ARM-software/golang-utils/utils/commonerrors"
13+
"github.com/ARM-software/golang-utils/utils/logs/logrimp"
14+
)
15+
16+
// NewSlogLogger returns a logger which uses slog logger (andard library package )
17+
func NewSlogLogger(slogL *slog.Logger, loggerSource string) (loggers Loggers, err error) {
18+
if slogL == nil {
19+
err = commonerrors.ErrNoLogger
20+
return
21+
}
22+
return NewLogrLogger(logrimp.NewSlogLogger(slogL), loggerSource)
23+
}

utils/logs/slog_logger_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package logs
6+
7+
import (
8+
"os"
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
"golang.org/x/exp/slog"
13+
)
14+
15+
func TestSlogLogger(t *testing.T) {
16+
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
17+
loggers, err := NewSlogLogger(logger, "Test")
18+
require.NoError(t, err)
19+
testLog(t, loggers)
20+
}

0 commit comments

Comments
 (0)