Skip to content

Commit bead010

Browse files
committed
REORG/MEDIUM: migrate from standard log to log/slog
Replaced the standard log package with the structured logging package log/slog across the codebase. All existing log.Printf, log.Println, and log.Fatal calls have been updated to use appropriate slog levels (slog.Info, slog.Warn, slog.Error) with key-value pairs.
1 parent f92d61c commit bead010

12 files changed

Lines changed: 191 additions & 86 deletions

File tree

aspell/aspell.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package aspell
33
import (
44
"bytes"
55
"fmt"
6-
"log"
6+
"log/slog"
77
"os"
88
"os/exec"
99
"path/filepath"
@@ -224,7 +224,7 @@ func (a Aspell) collectIdentifiers(content []CommitDiff) []string {
224224
readFiles[name] = struct{}{}
225225
data, err := os.ReadFile(name)
226226
if err != nil {
227-
log.Printf("aspell: could not read file %s for identifiers, using diff: %v", name, err)
227+
slog.Warn("could not read file for identifiers, using diff", "file", name, "err", err)
228228
addWords(name, d.Files[name])
229229
continue
230230
}
@@ -258,7 +258,7 @@ func (a Aspell) collectIdentifiers(content []CommitDiff) []string {
258258
identifierWords = append(identifierWords, w)
259259
}
260260
if len(identifierWords) > 0 {
261-
log.Printf("collected %d identifiers (scope: %s) for spell check filtering", len(identifierWords), a.IdentifierScope)
261+
slog.Info("collected identifiers for spell check filtering", "count", len(identifierWords), "scope", string(a.IdentifierScope))
262262
}
263263
return identifierWords
264264
}
@@ -267,7 +267,7 @@ func (a Aspell) checkSubjects(commits []Commit, junitSuite junit.Interface, resp
267267
for _, c := range commits {
268268
if err := a.checkSingle(c.Subject, []string{}); err != nil {
269269
junitSuite.AddMessageFailed("commit message", "aspell check failed", err.Error())
270-
log.Printf("commit %s subject %q %s", c.Hash, c.Subject, err.Error())
270+
slog.Error("aspell check failed on subject", "commit", c.Hash, "subject", c.Subject, "err", err)
271271
_, _ = fmt.Fprintf(response, "commit %s %q: %s\n", c.Hash, c.Subject, err)
272272
}
273273
}
@@ -297,9 +297,11 @@ func (a Aspell) checkFiles(content []CommitDiff, identifierWords []string, junit
297297
location := name
298298
if d.Hash != "" {
299299
location = fmt.Sprintf("commit %s %q %s", d.Hash, d.Subject, name)
300+
slog.Error("aspell check failed", "commit", d.Hash, "subject", d.Subject, "file", name, "err", err)
301+
} else {
302+
slog.Error("aspell check failed", "file", name, "err", err)
300303
}
301304
junitSuite.AddMessageFailed(location, "aspell check failed", err.Error())
302-
log.Println(location, err.Error())
303305
_, _ = fmt.Fprintf(response, "%s: %s\n", location, err)
304306
}
305307
}
@@ -312,13 +314,13 @@ func (a Aspell) checkCommitMessages(commits []Commit, identifierWords []string,
312314
subject := parts[0]
313315
if err := a.checkSingle(subject, []string{}); err != nil {
314316
junitSuite.AddMessageFailed("commit message", "aspell check failed", err.Error())
315-
log.Printf("commit %s subject %q %s", c.Hash, subject, err.Error())
317+
slog.Error("aspell check failed on subject", "commit", c.Hash, "subject", subject, "err", err)
316318
_, _ = fmt.Fprintf(response, "commit %s %q: %s\n", c.Hash, subject, err)
317319
}
318320
if len(parts) > 1 {
319321
if err := a.checkSingle(parts[1], identifierWords); err != nil {
320322
junitSuite.AddMessageFailed("commit message", "aspell check failed", err.Error())
321-
log.Printf("commit %s body %q %s", c.Hash, subject, err.Error())
323+
slog.Error("aspell check failed on body", "commit", c.Hash, "subject", subject, "err", err)
322324
_, _ = fmt.Fprintf(response, "commit %s %q (body): %s\n", c.Hash, subject, err)
323325
}
324326
}
@@ -362,7 +364,7 @@ func checkWithAspellExec(subject string, extraDicts ...string) (string, error) {
362364
cmd.Stderr = &stderr
363365
err := cmd.Run()
364366
if err != nil {
365-
log.Printf("aspell error: %s, stderr: %s", err, stderr.String())
367+
slog.Error("aspell execution failed", "err", err, "stderr", stderr.String())
366368
return "", err
367369
}
368370

aspell/fetch_dictionaries.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7-
"log"
7+
"log/slog"
88
"net/http"
99
"os"
1010
"path/filepath"
@@ -107,7 +107,7 @@ func fetchGitHubDirectory(gh GitHubDictionary) (fetchedDictionaries, error) {
107107
owner, repo, ref, path := m[1], m[2], m[3], m[4]
108108

109109
apiURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/contents/%s?ref=%s", owner, repo, path, ref)
110-
log.Printf("aspell dictionaries: listing GitHub directory: %s", apiURL)
110+
slog.Info("listing GitHub dictionary directory", "url", apiURL)
111111

112112
req, err := http.NewRequest("GET", apiURL, nil)
113113
if err != nil {
@@ -119,7 +119,7 @@ func fetchGitHubDirectory(gh GitHubDictionary) (fetchedDictionaries, error) {
119119
if token != "" {
120120
req.Header.Set("Authorization", "Bearer "+token)
121121
} else {
122-
log.Printf("aspell dictionaries: warning: token env %s is empty", gh.TokenEnv)
122+
slog.Warn("dictionary token env is empty", "env", gh.TokenEnv)
123123
}
124124
}
125125

@@ -148,7 +148,7 @@ func fetchGitHubDirectory(gh GitHubDictionary) (fetchedDictionaries, error) {
148148
if entry.DownloadURL == "" {
149149
continue
150150
}
151-
log.Printf("aspell dictionaries: fetching %s", entry.DownloadURL)
151+
slog.Info("fetching dictionary", "url", entry.DownloadURL)
152152
fetched, err := fetchSingleFile(entry.DownloadURL, entry.Name, gh.TokenEnv)
153153
if err != nil {
154154
return result, fmt.Errorf("fetching %s: %w", entry.Name, err)
@@ -157,7 +157,7 @@ func fetchGitHubDirectory(gh GitHubDictionary) (fetchedDictionaries, error) {
157157
result.rwsFiles = append(result.rwsFiles, fetched.rwsFiles...)
158158
}
159159

160-
log.Printf("aspell dictionaries: loaded %d words and %d .rws files from %s", len(result.words), len(result.rwsFiles), gh.URL)
160+
slog.Info("loaded dictionaries", "words", len(result.words), "rwsFiles", len(result.rwsFiles), "url", gh.URL)
161161
return result, nil
162162
}
163163

@@ -176,7 +176,7 @@ func fetchGitLabDirectory(gl GitLabDictionary) (fetchedDictionaries, error) {
176176
encodedProject := strings.ReplaceAll(project, "/", "%2F")
177177
apiURL := fmt.Sprintf("%s/api/v4/projects/%s/repository/tree?path=%s&ref=%s&per_page=100",
178178
baseURL, encodedProject, path, ref)
179-
log.Printf("aspell dictionaries: listing GitLab directory: %s", apiURL)
179+
slog.Info("listing GitLab dictionary directory", "url", apiURL)
180180

181181
req, err := http.NewRequest("GET", apiURL, nil)
182182
if err != nil {
@@ -210,7 +210,7 @@ func fetchGitLabDirectory(gl GitLabDictionary) (fetchedDictionaries, error) {
210210
encodedPath := strings.ReplaceAll(entry.Path, "/", "%2F")
211211
rawURL := fmt.Sprintf("%s/api/v4/projects/%s/repository/files/%s/raw?ref=%s",
212212
baseURL, encodedProject, encodedPath, ref)
213-
log.Printf("aspell dictionaries: fetching %s", rawURL)
213+
slog.Info("fetching dictionary", "url", rawURL)
214214
fetched, err := fetchSingleFile(rawURL, entry.Name, gl.TokenEnv)
215215
if err != nil {
216216
return result, fmt.Errorf("fetching %s: %w", entry.Name, err)
@@ -219,7 +219,7 @@ func fetchGitLabDirectory(gl GitLabDictionary) (fetchedDictionaries, error) {
219219
result.rwsFiles = append(result.rwsFiles, fetched.rwsFiles...)
220220
}
221221

222-
log.Printf("aspell dictionaries: loaded %d words and %d .rws files from %s", len(result.words), len(result.rwsFiles), gl.URL)
222+
slog.Info("loaded dictionaries", "words", len(result.words), "rwsFiles", len(result.rwsFiles), "url", gl.URL)
223223
return result, nil
224224
}
225225

@@ -229,7 +229,7 @@ func setGitLabToken(req *http.Request, tokenEnv string) {
229229
}
230230
token := os.Getenv(tokenEnv)
231231
if token == "" {
232-
log.Printf("aspell dictionaries: warning: token env %s is empty", tokenEnv)
232+
slog.Warn("dictionary token env is empty", "env", tokenEnv)
233233
return
234234
}
235235
req.Header.Set("PRIVATE-TOKEN", token)
@@ -238,7 +238,7 @@ func setGitLabToken(req *http.Request, tokenEnv string) {
238238
// fetchDictionaryURL fetches a single dictionary file from a URL.
239239
func fetchDictionaryURL(rawURL string) (fetchedDictionaries, error) {
240240
name := filepath.Base(rawURL)
241-
log.Printf("aspell dictionaries: fetching %s", rawURL)
241+
slog.Info("fetching dictionary", "url", rawURL)
242242
return fetchSingleFile(rawURL, name, "")
243243
}
244244

@@ -290,16 +290,16 @@ func fetchSingleFile(url, name, tokenEnv string) (fetchedDictionaries, error) {
290290
}
291291
_ = tmpFile.Close()
292292
result.rwsFiles = append(result.rwsFiles, tmpFile.Name())
293-
log.Printf("aspell dictionaries: saved .rws dictionary %s to %s", name, tmpFile.Name())
293+
slog.Info("saved .rws dictionary", "file", name, "path", tmpFile.Name())
294294
case strings.HasSuffix(lower, ".txt"):
295295
words := parseTxtDictionary(string(data))
296296
result.words = append(result.words, words...)
297-
log.Printf("aspell dictionaries: loaded %d words from %s", len(words), name)
297+
slog.Info("loaded dictionary words", "count", len(words), "file", name)
298298
default:
299299
// Treat unknown extensions as plain text word lists
300300
words := parseTxtDictionary(string(data))
301301
result.words = append(result.words, words...)
302-
log.Printf("aspell dictionaries: loaded %d words from %s (treated as text)", len(words), name)
302+
slog.Info("loaded dictionary words as plain text", "count", len(words), "file", name)
303303
}
304304

305305
return result, nil

aspell/new.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package aspell
22

33
import (
44
"fmt"
5-
"log"
5+
"log/slog"
66
"os"
77
"strings"
88

@@ -14,7 +14,7 @@ func New(filename string) (Aspell, error) {
1414
var err error
1515
fileExists := true
1616
if data, err = os.ReadFile(filename); err != nil {
17-
log.Printf("warning: aspell exceptions file not found (%s)", err.Error())
17+
slog.Info("aspell exceptions file not found", "err", err)
1818
fileExists = false
1919
}
2020

@@ -28,20 +28,18 @@ func New(filename string) (Aspell, error) {
2828
if aspell.RemoteFile.URL != "" || aspell.RemoteFile.URLEnv != "" {
2929
extraAllowedWords, err = fetchRemoteFile(aspell)
3030
if err != nil {
31-
log.Printf("warning: aspell remote file (%s)", err.Error())
32-
return Aspell{}, err
31+
return Aspell{}, err // logged by the caller as one config error
3332
}
3433
if len(extraAllowedWords) == 0 {
35-
log.Print("warning: aspell remote file is empty")
34+
slog.Warn("aspell remote file is empty")
3635
}
3736
}
3837

3938
// Fetch remote dictionaries if configured
4039
if len(aspell.Dictionaries.GitHub) > 0 || len(aspell.Dictionaries.GitLab) > 0 || len(aspell.Dictionaries.URLs) > 0 {
4140
fetched, dictErr := fetchDictionaries(aspell.Dictionaries)
4241
if dictErr != nil {
43-
log.Printf("warning: aspell dictionaries fetch failed: %s", dictErr)
44-
return Aspell{}, dictErr
42+
return Aspell{}, dictErr // logged by the caller as one config error
4543
}
4644
extraAllowedWords = append(extraAllowedWords, fetched.words...)
4745
aspell.ExtraDicts = fetched.rwsFiles
@@ -88,7 +86,7 @@ func New(filename string) (Aspell, error) {
8886
}
8987
}
9088

91-
log.Printf("aspell mode set to %s", aspell.Mode)
89+
slog.Info("aspell mode set", "mode", string(aspell.Mode))
9290
if fileExists {
9391
aspell.HelpText = `aspell can be configured with .aspell.yml file.
9492
Add words to allowed list if its false positive`
@@ -123,7 +121,7 @@ dictionaries:
123121
ignoreFiles := []string{"go.mod", "go.sum"}
124122
for _, file := range ignoreFiles {
125123
if _, err := os.Stat(file); err == nil {
126-
log.Printf("aspell: added %s to ignore list", file)
124+
slog.Info("added file to aspell ignore list", "file", file)
127125
aspell.IgnoreFiles = append(aspell.IgnoreFiles, file)
128126
}
129127
}

aspell/remote.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"log"
7+
"log/slog"
88
"net/http"
99
"os"
1010
"strings"
@@ -16,9 +16,9 @@ func fetchRemoteFile(aspell Aspell) ([]string, error) {
1616
url := aspell.RemoteFile.URL
1717
if aspell.RemoteFile.URLEnv != "" {
1818
url = os.Getenv(aspell.RemoteFile.URLEnv)
19-
log.Printf("aspell remote file: using URL from env %s: %q", aspell.RemoteFile.URLEnv, url)
19+
slog.Info("aspell remote file: using URL from env", "env", aspell.RemoteFile.URLEnv, "url", url)
2020
} else {
21-
log.Printf("aspell remote file: using URL: %q", url)
21+
slog.Info("aspell remote file: using URL", "url", url)
2222
}
2323

2424
if url == "" {
@@ -33,14 +33,14 @@ func fetchRemoteFile(aspell Aspell) ([]string, error) {
3333
if aspell.RemoteFile.HeaderFromENV != "" {
3434
envValue := os.Getenv(aspell.RemoteFile.HeaderFromENV)
3535
if envValue == "" {
36-
log.Printf("aspell remote file: warning: header env %s is empty", aspell.RemoteFile.HeaderFromENV)
36+
slog.Warn("aspell remote file: header env is empty", "env", aspell.RemoteFile.HeaderFromENV)
3737
}
3838
req.Header.Set(aspell.RemoteFile.HeaderFromENV, envValue)
3939
}
4040
if aspell.RemoteFile.PrivateTokenENV != "" {
4141
envValue := os.Getenv(aspell.RemoteFile.PrivateTokenENV)
4242
if envValue == "" {
43-
log.Printf("aspell remote file: warning: private token env %s is empty", aspell.RemoteFile.PrivateTokenENV)
43+
slog.Warn("aspell remote file: private token env is empty", "env", aspell.RemoteFile.PrivateTokenENV)
4444
}
4545
req.Header.Set("PRIVATE-TOKEN", envValue)
4646
}
@@ -78,16 +78,16 @@ func fetchRemoteFile(aspell Aspell) ([]string, error) {
7878
if err != nil {
7979
return nil, fmt.Errorf("aspell remote file: failed to parse YAML block: %w", err)
8080
}
81-
log.Printf("aspell remote file: loaded %d words (yaml block): %v", len(allowedWords), wordSample(allowedWords))
81+
slog.Info("aspell remote file: loaded words", "format", "yaml block", "count", len(allowedWords), "sample", wordSample(allowedWords))
8282
return allowedWords, nil
8383
}
8484
allowedWords = strings.Split(content, "\n")
85-
log.Printf("aspell remote file: loaded %d words (newline-separated): %v", len(allowedWords), wordSample(allowedWords))
85+
slog.Info("aspell remote file: loaded words", "format", "newline-separated", "count", len(allowedWords), "sample", wordSample(allowedWords))
8686
} else {
8787
for _, item := range items {
8888
allowedWords = append(allowedWords, item.(string))
8989
}
90-
log.Printf("aspell remote file: loaded %d words (JSON array): %v", len(allowedWords), wordSample(allowedWords))
90+
slog.Info("aspell remote file: loaded words", "format", "JSON array", "count", len(allowedWords), "sample", wordSample(allowedWords))
9191
}
9292

9393
return allowedWords, nil

aspell/repowords.go

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

33
import (
4-
"log"
4+
"log/slog"
55
"os"
66
"path/filepath"
77

@@ -29,7 +29,7 @@ func collectManifestWords(dir string) []string {
2929
}
3030
extracted := m.extract(string(data))
3131
words = append(words, extracted...)
32-
log.Printf("aspell: collected %d words from %s", len(extracted), m.name)
32+
slog.Info("collected words from manifest", "count", len(extracted), "file", m.name)
3333
}
3434
return words
3535
}
@@ -54,6 +54,6 @@ func collectPathWords(root string) []string {
5454
return nil
5555
})
5656
words := match.GetPathWords(paths)
57-
log.Printf("aspell: collected %d words from %d repository paths", len(words), len(paths))
57+
slog.Info("collected words from repository paths", "count", len(words), "paths", len(paths))
5858
return words
5959
}

aspell_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"bufio"
5-
"log"
65
"os"
76
"strings"
87
"testing"
@@ -12,8 +11,6 @@ import (
1211
)
1312

1413
func Test_Aspell(t *testing.T) {
15-
log.SetFlags(log.LstdFlags | log.Lshortfile)
16-
1714
aspellCheck, err := aspell.New(".aspell.yml")
1815
if err != nil {
1916
t.Errorf("checkWithAspell() error = %v", err)

0 commit comments

Comments
 (0)