Skip to content

Commit 704a8b5

Browse files
committed
chore: use zerolog instead of logrus
1 parent e7f74c1 commit 704a8b5

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

handler/document.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/armbian/ansi-hastebin/internal/keygenerator"
1111
"github.com/armbian/ansi-hastebin/internal/storage"
1212
"github.com/go-chi/chi/v5"
13-
"github.com/sirupsen/logrus"
13+
"github.com/rs/zerolog/log"
1414
)
1515

1616
// DocumentHandler manages document operations
@@ -50,34 +50,34 @@ func (h *DocumentHandler) HandleGet(w http.ResponseWriter, r *http.Request) {
5050
data, err := h.Store.Get(key, false)
5151

5252
if data != "" && err == nil {
53-
logrus.WithField("key", key).Info("Retrieved document")
53+
log.Info().Str("key", key).Msg("Retrieved document")
5454
w.Header().Set("Content-Type", "application/json")
5555
if r.Method == http.MethodHead {
5656
w.WriteHeader(http.StatusOK)
5757
return
5858
}
5959
json.NewEncoder(w).Encode(map[string]string{"data": data, "key": key})
6060
} else {
61-
logrus.WithField("key", key).Warn("Document not found")
61+
log.Info().Str("key", key).Msg("Document not found")
6262
http.Error(w, `{"message": "Document not found."}`, http.StatusNotFound)
6363
}
6464
}
6565

6666
// Handle retrieving raw document
6767
func (h *DocumentHandler) HandleRawGet(w http.ResponseWriter, r *http.Request) {
68-
6968
key := strings.Split(chi.URLParam(r, "id"), ".")[0]
7069
data, err := h.Store.Get(key, false)
70+
7171
if data != "" && err == nil {
72-
logrus.WithField("key", key).Info("Retrieved raw document")
72+
log.Info().Str("key", key).Msg("Retrieved raw document")
7373
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
7474
if r.Method == http.MethodHead {
7575
w.WriteHeader(http.StatusOK)
7676
return
7777
}
7878
w.Write([]byte(data))
7979
} else {
80-
logrus.WithField("key", key).Warn("Raw document not found")
80+
log.Info().Str("key", key).Msg("Raw document not found")
8181
http.Error(w, `{"message": "Document not found."}`, http.StatusNotFound)
8282
}
8383
}
@@ -91,15 +91,15 @@ func (h *DocumentHandler) HandlePost(w http.ResponseWriter, r *http.Request) {
9191
}
9292

9393
if h.MaxLength > 0 && buffer.Len() > h.MaxLength {
94-
logrus.Warn("Document exceeds max length")
94+
log.Info().Str("key", "").Msg("Document exceeds max length")
9595
http.Error(w, `{"message": "Document exceeds maximum length."}`, http.StatusBadRequest)
9696
return
9797
}
9898

9999
key := h.KeyGenerator.Generate(h.KeyLength)
100100
h.Store.Set(key, buffer.String(), false)
101101

102-
logrus.WithField("key", key).Info("Added document")
102+
log.Info().Str("key", key).Msg("Added document")
103103
w.Header().Set("Content-Type", "application/json")
104104
json.NewEncoder(w).Encode(map[string]string{"key": key})
105105
}
@@ -113,15 +113,15 @@ func (h *DocumentHandler) HandlePutLog(w http.ResponseWriter, r *http.Request) {
113113
}
114114

115115
if h.MaxLength > 0 && buffer.Len() > h.MaxLength {
116-
logrus.Warn("Document exceeds max length")
116+
log.Info().Str("key", "").Msg("Document exceeds max length")
117117
http.Error(w, `{"message": "Document exceeds maximum length."}`, http.StatusBadRequest)
118118
return
119119
}
120120

121121
key := h.KeyGenerator.Generate(h.KeyLength)
122122
h.Store.Set(key, buffer.String(), false)
123123

124-
logrus.WithField("key", key).Info("Added document with log link")
124+
log.Info().Str("key", key).Msg("Added document with log link")
125125
w.Header().Set("Content-Type", "text/plain")
126126
fmt.Fprintf(w, "\nhttps://%s/%s\n\n", r.Host, key)
127127
}
@@ -136,7 +136,7 @@ func (h *DocumentHandler) readBody(r *http.Request, buffer *strings.Builder) err
136136
} else {
137137
data, err := io.ReadAll(r.Body)
138138
if err != nil {
139-
logrus.Error("Connection error: ", err)
139+
log.Error().Err(err).Msg("Error reading request body")
140140
return err
141141
}
142142
buffer.WriteString(string(data))

handler/document_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
"testing"
1111

1212
"github.com/go-chi/chi/v5"
13-
"github.com/sirupsen/logrus"
13+
"github.com/rs/zerolog"
14+
"github.com/rs/zerolog/log"
1415
"github.com/stretchr/testify/require"
1516
)
1617

@@ -176,7 +177,7 @@ func BenchmarkHandlePost(b *testing.B) {
176177
handler.RegisterRoutes(router)
177178

178179
// Disable logging for benchmark
179-
logrus.StandardLogger().Level = logrus.FatalLevel
180+
log.Logger = log.Level(zerolog.Disabled)
180181

181182
body := bytes.NewBufferString("benchmark content")
182183
for i := 0; i < b.N; i++ {
@@ -190,7 +191,7 @@ func BenchmarkHandleGet(b *testing.B) {
190191
handler.RegisterRoutes(router)
191192

192193
// Disable logging for benchmark
193-
logrus.StandardLogger().Level = logrus.FatalLevel
194+
log.Logger = log.Level(zerolog.Disabled)
194195

195196
// Add document
196197
handler.Store.Set("test123", "benchmark data", false)
@@ -206,7 +207,7 @@ func BenchmarkHandlePutLog(b *testing.B) {
206207
handler.RegisterRoutes(router)
207208

208209
// Disable logging for benchmark
209-
logrus.StandardLogger().Level = logrus.FatalLevel
210+
log.Logger = log.Level(zerolog.Disabled)
210211

211212
body := bytes.NewBufferString("benchmark log entry")
212213
for i := 0; i < b.N; i++ {
@@ -220,7 +221,7 @@ func BenchmarkHandleRawGet(b *testing.B) {
220221
handler.RegisterRoutes(router)
221222

222223
// Disable logging for benchmark
223-
logrus.StandardLogger().Level = logrus.FatalLevel
224+
log.Logger = log.Level(zerolog.Disabled)
224225

225226
// Add document
226227
handler.Store.Set("test123", "benchmark data", false)

0 commit comments

Comments
 (0)