Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/20230227175001.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: `[logs]` Introducing the [zap](https://github.com/uber-go/zap) logger since logrus is being deprecated
1 change: 1 addition & 0 deletions changes/202303161517.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: `[error]` Added the `CorrespondTo` function to determine if an error corresponds to a specific description
24 changes: 24 additions & 0 deletions utils/commonerrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// Package commonerrors defines typical errors which can happen.
package commonerrors

import (
"context"
"errors"
"strings"
)

var (
Expand Down Expand Up @@ -40,6 +43,7 @@ var (
ErrMalicious = errors.New("suspected malicious intent")
)

// Any determines whether the target error is of the same type as any of the errors `err`
func Any(target error, err ...error) bool {
for i := range err {
e := err[i]
Expand All @@ -50,6 +54,7 @@ func Any(target error, err ...error) bool {
return false
}

// None determines whether the target error is of none of the types of the errors `err`
func None(target error, err ...error) bool {
for i := range err {
e := err[i]
Expand All @@ -60,6 +65,25 @@ func None(target error, err ...error) bool {
return true
}

// CorrespondTo determines whether a `target` error corresponds to a specific error described by `description`
// It will check whether the error contains the string in its description.
// ```code
//
// CorrespondTo(errors.New("feature a is not supported", "not supported") = True
// ```
func CorrespondTo(target error, description ...string) bool {
if target == nil {
return false
}
desc := target.Error()
for i := range description {
if strings.Contains(desc, description[i]) {
return true
}
}
return false
}

// ConvertContextError converts a context error into common errors.
func ConvertContextError(err error) error {
if err == nil {
Expand Down
7 changes: 7 additions & 0 deletions utils/commonerrors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/bxcodec/faker/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
Expand Down Expand Up @@ -37,6 +38,12 @@ func TestNone(t *testing.T) {
assert.True(t, None(fmt.Errorf("an error %w", ErrNotImplemented), ErrInvalid, ErrUnknown))
}

func TestCorrespondTo(t *testing.T) {
assert.False(t, CorrespondTo(ErrNotImplemented, ErrInvalid.Error(), ErrUnknown.Error()))
assert.True(t, CorrespondTo(ErrNotImplemented, ErrInvalid.Error(), ErrNotImplemented.Error()))
assert.True(t, CorrespondTo(fmt.Errorf("%v %w", faker.Sentence(), ErrUndefined), ErrUndefined.Error()))
}

func TestContextErrorConversion(t *testing.T) {
defer goleak.VerifyNone(t)
task := func(ctx context.Context) {
Expand Down
3 changes: 3 additions & 0 deletions utils/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/go-logr/logr v0.4.0 // Staying on this version until kubernetes uses a more recent one
github.com/go-logr/stdr v0.4.0
github.com/go-logr/zapr v0.4.0
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/gofrs/uuid v4.4.0+incompatible
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f
Expand All @@ -35,6 +36,7 @@ require (
github.com/stretchr/testify v1.8.2
go.uber.org/atomic v1.10.0
go.uber.org/goleak v1.2.1
go.uber.org/zap v1.21.0
golang.org/x/net v0.8.0
golang.org/x/sync v0.1.0
golang.org/x/text v0.8.0
Expand Down Expand Up @@ -73,6 +75,7 @@ require (
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/sys v0.6.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
20 changes: 20 additions & 0 deletions utils/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/bmatcuk/doublestar/v3 v3.0.0 h1:TQtVPlDnAYwcrVNB2JiGuMc++H5qzWZd9PhkNo5WyHI=
github.com/bmatcuk/doublestar/v3 v3.0.0/go.mod h1:6PcTVMw80pCY1RVuoqu3V++99uQB3vsSYKPTd8AWA0k=
github.com/bombsimon/logrusr v1.1.0 h1:Y03FI4Z/Shyrc9jF26vuaUbnPxC5NMJnTtJA/3Lihq8=
Expand Down Expand Up @@ -115,6 +117,8 @@ github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc=
github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/stdr v0.4.0 h1:ijk9G/xzDRZdMU1QRhLYdHuWvNZWqte+NZMOGsiKWbc=
github.com/go-logr/stdr v0.4.0/go.mod h1:NO1vneyJDqKVgJYnxhwXWWmQPOvNM391IG3H8ql3jiA=
github.com/go-logr/zapr v0.4.0 h1:uc1uML3hRYL9/ZZPdgHS/n8Nzo+eaYL/Efxkkamf7OM=
github.com/go-logr/zapr v0.4.0/go.mod h1:tabnROwaDl0UNxkVeFRbY8bwB37GwRv0P8lg6aAiEnk=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
Expand Down Expand Up @@ -237,6 +241,7 @@ github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 h1:q2e307iGHPdTGp
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o=
github.com/pjbgf/sha1cd v0.2.3 h1:uKQP/7QOzNtKYH7UTohZLcjF5/55EnTw0jO/Ru4jZwI=
github.com/pjbgf/sha1cd v0.2.3/go.mod h1:HOK9QrgzdHpbc2Kzip0Q1yi3M2MFGPADtR6HjG65m5M=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
Expand All @@ -256,6 +261,8 @@ github.com/sasha-s/go-deadlock v0.3.1 h1:sqv7fDNShgjcaxkO0JNcOAlr8B9+cV5Ey/OB71e
github.com/sasha-s/go-deadlock v0.3.1/go.mod h1:F73l+cr82YSh10GxyRI6qZiCgK64VaZjwesgfQ1/iLM=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shirou/gopsutil/v3 v3.23.1 h1:a9KKO+kGLKEvcPIs4W62v0nu3sciVDOOOPUD0Hz7z/4=
github.com/shirou/gopsutil/v3 v3.23.1/go.mod h1:NN6mnm5/0k8jw4cBfCnJtr5L7ErOTg18tMNpgFkn0hA=
github.com/shirou/gopsutil/v3 v3.23.2 h1:PAWSuiAszn7IhPMBtXsbSCafej7PqUOvY6YywlQUExU=
github.com/shirou/gopsutil/v3 v3.23.2/go.mod h1:gv0aQw33GLo3pG8SiWKiQrbDzbRY1K80RyZJ7V4Th1M=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down Expand Up @@ -311,10 +318,16 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -399,6 +412,8 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -486,6 +501,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -496,6 +512,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -549,6 +567,7 @@ golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -655,6 +674,7 @@ gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
1 change: 1 addition & 0 deletions utils/logs/file_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package logs

import "github.com/sirupsen/logrus"
Expand Down
10 changes: 7 additions & 3 deletions utils/logs/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// Package logs defines loggers for use in projects.
package logs

import (
Expand All @@ -12,7 +14,9 @@ import (

//go:generate mockgen -destination=../mocks/mock_$GOPACKAGE.go -package=mocks github.com/ARM-software/golang-utils/utils/$GOPACKAGE Loggers,IMultipleLoggers,WriterWithSource,StdLogger

// Loggers define generic loggers.
// Loggers defines generic loggers which separate common logging messages from errors.
// This is to use in cases where it is necessary to separate the two streams e.g. remote procedure call (RPC)
// In most cases however, if only a standard logger is needed, it is advised to use logr.Logger.
type Loggers interface {
io.Closer
// Check returns whether the loggers are correctly defined or not.
Expand All @@ -21,9 +25,9 @@ type Loggers interface {
SetLogSource(source string) error
// SetLoggerSource sets the source of the logger e.g. APIs, Build worker, CMSIS tools.
SetLoggerSource(source string) error
// Log logs to the output logger.
// Log logs to the output stream/logger.
Log(output ...interface{})
// LogError logs to the Error logger.
// LogError logs to the Error stream/logger.
LogError(err ...interface{})
}

Expand Down
22 changes: 11 additions & 11 deletions utils/logs/message_logger.go → utils/logs/json_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"github.com/ARM-software/golang-utils/utils/commonerrors"
)

// Definition of JSON message loggers
// JSONLoggers defines a JSON logger
type JSONLoggers struct {
Loggers
mu sync.RWMutex
source string
loggerSource string
writer WriterWithSource
Zerologger zerolog.Logger
zerologger zerolog.Logger
}

func (l *JSONLoggers) SetLogSource(source string) error {
Expand Down Expand Up @@ -49,7 +49,7 @@ func (l *JSONLoggers) GetLoggerSource() string {
return l.loggerSource
}

// Checks whether the loggers are correctly defined or not.
// Check checks whether the logger is correctly defined or not.
func (l *JSONLoggers) Check() error {
if l.GetSource() == "" {
return commonerrors.ErrNoLogSource
Expand All @@ -64,39 +64,40 @@ func (l *JSONLoggers) Configure() error {
zerolog.TimestampFieldName = "ctime"
zerolog.MessageFieldName = "message"
zerolog.LevelFieldName = "severity"
l.Zerologger = l.Zerologger.With().Timestamp().Logger()
l.zerologger = l.zerologger.With().Timestamp().Logger()
return nil
}

// Logs to the output logger.
// Log logs to the output stream.
func (l *JSONLoggers) Log(output ...interface{}) {
if len(output) == 1 && output[0] == "\n" {
return
}
l.Zerologger.Info().Str("source", l.GetLoggerSource()).Msg(fmt.Sprint(output...))
l.zerologger.Info().Str("source", l.GetLoggerSource()).Msg(fmt.Sprint(output...))
}

// Logs to the Error logger.
// LogError logs to the error stream.
func (l *JSONLoggers) LogError(err ...interface{}) {
if len(err) == 1 && err[0] == "\n" {
return
}
l.Zerologger.Error().Str("source", l.GetLoggerSource()).Msg(fmt.Sprint(err...))
l.zerologger.Error().Str("source", l.GetLoggerSource()).Msg(fmt.Sprint(err...))
}

// Closes the logger
// Close closes the logger
func (l *JSONLoggers) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
return l.writer.Close()
}

// NewJSONLogger creates a Json logger.
func NewJSONLogger(writer WriterWithSource, loggerSource string, source string) (loggers Loggers, err error) {
zerroLogger := JSONLoggers{
source: source,
loggerSource: loggerSource,
writer: writer,
Zerologger: zerolog.New(writer),
zerologger: zerolog.New(writer),
}
err = zerroLogger.Check()
if err != nil {
Expand All @@ -121,7 +122,6 @@ func NewJSONLogger(writer WriterWithSource, loggerSource string, source string)
// loggerSource : logger application name
// source : source string
// droppedMessagesLogger : logger for dropped messages

// If pollInterval is greater than 0, a poller is used otherwise a waiter is used.
func NewJSONLoggerForSlowWriter(slowWriter WriterWithSource, ringBufferSize int, pollInterval time.Duration, loggerSource string, source string, droppedMessagesLogger Loggers) (loggers Loggers, err error) {
return NewJSONLogger(NewDiodeWriterForSlowWriter(slowWriter, ringBufferSize, pollInterval, droppedMessagesLogger), loggerSource, source)
Expand Down
File renamed without changes.
15 changes: 12 additions & 3 deletions utils/logs/logr_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ const (
)

type logrLogger struct {
logger logr.Logger
logger logr.Logger
closeFunc func() error
}

func (l *logrLogger) Close() error {
if l.closeFunc != nil {
return l.closeFunc()
}
return nil
}

Expand Down Expand Up @@ -68,8 +72,13 @@ func (l *logrLogger) LogError(err ...interface{}) {
}

// NewLogrLogger creates loggers based on a logr implementation (https://github.com/go-logr/logr)
func NewLogrLogger(logrImpl logr.Logger, loggerSource string) (loggers Loggers, err error) {
loggers = &logrLogger{logger: logrImpl}
func NewLogrLogger(logrImpl logr.Logger, loggerSource string) (Loggers, error) {
return NewLogrLoggerWithClose(logrImpl, loggerSource, nil)
}

// NewLogrLoggerWithClose creates loggers based on a logr implementation (https://github.com/go-logr/logr)
func NewLogrLoggerWithClose(logrImpl logr.Logger, loggerSource string, closeFunc func() error) (loggers Loggers, err error) {
loggers = &logrLogger{logger: logrImpl, closeFunc: closeFunc}
err = loggers.SetLoggerSource(loggerSource)
return
}
Expand Down
2 changes: 2 additions & 0 deletions utils/logs/logrus_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// Package logs defines loggers for use in projects.
package logs

import (
Expand Down
4 changes: 2 additions & 2 deletions utils/logs/string_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (l *StringLoggers) Close() (err error) {
return
}

// NewStringLogger creates a logger to standard output/error
// NewStringLogger creates a logger to a string builder.
func NewStringLogger(loggerSource string) (loggers *StringLoggers, err error) {
loggers = &StringLoggers{
LogWriter: StringWriter{},
Expand All @@ -73,7 +73,7 @@ func NewStringLogger(loggerSource string) (loggers *StringLoggers, err error) {
return
}

// CreateStringLogger creates a logger to standard output/error
// CreateStringLogger creates a logger to a string builder.
//
// Deprecated: Use NewStringLogger instead
func CreateStringLogger(loggerSource string) (loggers *StringLoggers, err error) {
Expand Down
Loading