Skip to content

Commit adcfd51

Browse files
authored
refactor: replace logrus with log/slog in mage scripts (#5310)
* refactor: replace logrus with log/slog in mage scripts the downloads package is using logrus. Replace it with log/slog and drop the dependency Add custom Trace and Fatal level * fix: expand slice when passing fields vararg * fix: use correct logger
1 parent 623256e commit adcfd51

File tree

9 files changed

+318
-304
lines changed

9 files changed

+318
-304
lines changed

NOTICE.txt

+31-31
Original file line numberDiff line numberDiff line change
@@ -8659,37 +8659,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86598659
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
86608660
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86618661

8662-
--------------------------------------------------------------------------------
8663-
Dependency : github.com/sirupsen/logrus
8664-
Version: v1.9.3
8665-
Licence type (autodetected): MIT
8666-
--------------------------------------------------------------------------------
8667-
8668-
Contents of probable licence file $GOMODCACHE/github.com/sirupsen/logrus@v1.9.3/LICENSE:
8669-
8670-
The MIT License (MIT)
8671-
8672-
Copyright (c) 2014 Simon Eskildsen
8673-
8674-
Permission is hereby granted, free of charge, to any person obtaining a copy
8675-
of this software and associated documentation files (the "Software"), to deal
8676-
in the Software without restriction, including without limitation the rights
8677-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8678-
copies of the Software, and to permit persons to whom the Software is
8679-
furnished to do so, subject to the following conditions:
8680-
8681-
The above copyright notice and this permission notice shall be included in
8682-
all copies or substantial portions of the Software.
8683-
8684-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8685-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8686-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8687-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8688-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
8689-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
8690-
THE SOFTWARE.
8691-
8692-
86938662
--------------------------------------------------------------------------------
86948663
Dependency : github.com/spf13/cobra
86958664
Version: v1.8.1
@@ -49672,6 +49641,37 @@ Exhibit B - "Incompatible With Secondary Licenses" Notice
4967249641

4967349642

4967449643

49644+
--------------------------------------------------------------------------------
49645+
Dependency : github.com/sirupsen/logrus
49646+
Version: v1.9.3
49647+
Licence type (autodetected): MIT
49648+
--------------------------------------------------------------------------------
49649+
49650+
Contents of probable licence file $GOMODCACHE/github.com/sirupsen/logrus@v1.9.3/LICENSE:
49651+
49652+
The MIT License (MIT)
49653+
49654+
Copyright (c) 2014 Simon Eskildsen
49655+
49656+
Permission is hereby granted, free of charge, to any person obtaining a copy
49657+
of this software and associated documentation files (the "Software"), to deal
49658+
in the Software without restriction, including without limitation the rights
49659+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
49660+
copies of the Software, and to permit persons to whom the Software is
49661+
furnished to do so, subject to the following conditions:
49662+
49663+
The above copyright notice and this permission notice shall be included in
49664+
all copies or substantial portions of the Software.
49665+
49666+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49667+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
49668+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49669+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49670+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49671+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
49672+
THE SOFTWARE.
49673+
49674+
4967549675
--------------------------------------------------------------------------------
4967649676
Dependency : github.com/sourcegraph/conc
4967749677
Version: v0.3.0

dev-tools/mage/downloads/buckets.go

+23-24
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ package downloads
66

77
import (
88
"fmt"
9+
"log/slog"
910
"strings"
10-
11-
log "github.com/sirupsen/logrus"
1211
)
1312

1413
// BeatsCIArtifactsBase name of the bucket used to store the artifacts generated on Beats CI
@@ -65,14 +64,14 @@ func (r *BeatsLegacyURLResolver) Resolve() (string, string, string) {
6564
object = artifact + "/" + fileName
6665
}
6766

68-
log.WithFields(log.Fields{
69-
"beat": r.Beat,
70-
"bucket": r.Bucket,
71-
"fileName": r.FileName,
72-
"object": object,
73-
"prefix": prefix,
74-
"variant": r.Variant,
75-
}).Debug("Resolving URL from Beats Legacy resolver")
67+
logger.Debug("Resolving URL from Beats Legacy resolver",
68+
slog.String("beat", r.Beat),
69+
slog.String("bucket", r.Bucket),
70+
slog.String("fileName", r.FileName),
71+
slog.String("object", object),
72+
slog.String("prefix", prefix),
73+
slog.String("variant", r.Variant),
74+
)
7675

7776
return r.Bucket, prefix, object
7877
}
@@ -120,14 +119,14 @@ func (r *BeatsURLResolver) Resolve() (string, string, string) {
120119
object = artifact + "/" + fileName
121120
}
122121

123-
log.WithFields(log.Fields{
124-
"beat": r.Beat,
125-
"bucket": r.Bucket,
126-
"fileName": r.FileName,
127-
"object": object,
128-
"prefix": prefix,
129-
"variant": r.Variant,
130-
}).Debug("Resolving URL from Beats resolver")
122+
logger.Debug("Resolving URL from Beats resolver",
123+
slog.String("beat", r.Beat),
124+
slog.String("bucket", r.Bucket),
125+
slog.String("fileName", r.FileName),
126+
slog.String("object", object),
127+
slog.String("prefix", prefix),
128+
slog.String("variant", r.Variant),
129+
)
131130

132131
return r.Bucket, prefix, object
133132
}
@@ -172,12 +171,12 @@ func (r *ProjectURLResolver) Resolve() (string, string, string) {
172171
prefix = fmt.Sprintf("%s/commits/%s", artifact, GithubCommitSha1)
173172
}
174173

175-
log.WithFields(log.Fields{
176-
"bucket": r.Bucket,
177-
"object": r.FileName,
178-
"prefix": prefix,
179-
"project": artifact,
180-
}).Info("Resolving URL from Project resolver")
174+
logger.Info("Resolving URL from Project resolver",
175+
slog.String("bucket", r.Bucket),
176+
slog.String("object", r.FileName),
177+
slog.String("prefix", prefix),
178+
slog.String("project", artifact),
179+
)
181180

182181
return r.Bucket, prefix, r.FileName
183182
}

dev-tools/mage/downloads/curl.go

+21-22
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import (
99
"context"
1010
"fmt"
1111
"io"
12+
"log/slog"
1213
"net/http"
1314
"net/url"
14-
15-
log "github.com/sirupsen/logrus"
1615
)
1716

1817
// httpRequest configures an HTTP request
@@ -86,28 +85,28 @@ func put(r httpRequest) (string, error) {
8685
func request(r httpRequest) (string, error) {
8786
escapedURL := r.GetURL()
8887

89-
fields := log.Fields{
90-
"method": r.method,
91-
"escapedURL": escapedURL,
88+
fields := []any{
89+
slog.String("method", r.method),
90+
slog.String("escapedURL", escapedURL),
9291
}
9392

9493
var body io.Reader
9594
if r.Payload != "" {
9695
body = bytes.NewReader([]byte(r.Payload))
97-
fields["payload"] = r.Payload
96+
fields = append(fields, slog.String("payload", r.Payload))
9897
} else {
9998
body = nil
10099
}
101100

102-
log.WithFields(fields).Trace("Executing request")
101+
logger.Log(context.Background(), TraceLevel, "Executing request", fields...)
103102

104103
req, err := http.NewRequestWithContext(context.TODO(), r.method, escapedURL, body)
105104
if err != nil {
106-
log.WithFields(log.Fields{
107-
"error": err,
108-
"method": r.method,
109-
"escapedURL": escapedURL,
110-
}).Warn("Error creating request")
105+
logger.Warn("Error creating request",
106+
slog.String("error", err.Error()),
107+
slog.String("method", r.method),
108+
slog.String("escapedURL", escapedURL),
109+
)
111110
return "", err
112111
}
113112

@@ -123,22 +122,22 @@ func request(r httpRequest) (string, error) {
123122

124123
resp, err := http.DefaultClient.Do(req)
125124
if err != nil {
126-
log.WithFields(log.Fields{
127-
"error": err,
128-
"method": r.method,
129-
"escapedURL": escapedURL,
130-
}).Warn("Error executing request")
125+
logger.Warn("Error executing request",
126+
slog.String("error", err.Error()),
127+
slog.String("method", r.method),
128+
slog.String("escapedURL", escapedURL),
129+
)
131130
return "", err
132131
}
133132
defer resp.Body.Close()
134133

135134
bodyBytes, err := io.ReadAll(resp.Body)
136135
if err != nil {
137-
log.WithFields(log.Fields{
138-
"error": err,
139-
"method": r.method,
140-
"escapedURL": escapedURL,
141-
}).Warn("Could not read response body")
136+
logger.Warn("Could not read response body",
137+
slog.String("error", err.Error()),
138+
slog.String("method", r.method),
139+
slog.String("escapedURL", escapedURL),
140+
)
142141
return "", err
143142
}
144143
bodyString := string(bodyBytes)

dev-tools/mage/downloads/io.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
package downloads
66

77
import (
8+
"context"
9+
"log/slog"
810
"os"
9-
10-
log "github.com/sirupsen/logrus"
1111
)
1212

1313
// exists checks if a path exists in the file system
@@ -27,10 +27,12 @@ func mkdirAll(path string) error {
2727
if _, err := os.Stat(path); os.IsNotExist(err) {
2828
err = os.MkdirAll(path, 0755)
2929
if err != nil {
30-
log.WithFields(log.Fields{
31-
"error": err,
32-
"path": path,
33-
}).Fatal("Directory cannot be created")
30+
logger.Log(context.Background(),
31+
FatalLevel,
32+
"Directory cannot be created",
33+
slog.String("error", err.Error()),
34+
slog.String("path", path),
35+
)
3436

3537
return err
3638
}

dev-tools/mage/downloads/logger.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package downloads
6+
7+
import (
8+
"log/slog"
9+
"os"
10+
)
11+
12+
var (
13+
LogLevel = new(slog.LevelVar) // Info by default
14+
15+
logger = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: LogLevel}))
16+
)
17+
18+
const (
19+
TraceLevel = slog.Level(-12)
20+
FatalLevel = slog.Level(12)
21+
)

0 commit comments

Comments
 (0)