Skip to content

Commit 4c33fe2

Browse files
committed
feat: Actually use interface for logger
1 parent 4e575f5 commit 4c33fe2

File tree

7 files changed

+65
-23
lines changed

7 files changed

+65
-23
lines changed

internal/logger/logger.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,33 @@ import (
77
"os"
88
)
99

10-
// Logger is a wrapper around slog.Logger that provides additional methods
11-
type Logger struct {
10+
var _ Logger = &slogLogger{}
11+
12+
// slogLogger is a wrapper around slog.Logger that provides additional methods
13+
type slogLogger struct {
1214
*slog.Logger
1315
closer io.Closer
1416
}
1517

18+
type Logger interface {
19+
Close() error
20+
Debug(msg string, args ...interface{})
21+
Info(msg string, args ...interface{})
22+
Error(msg string, args ...interface{})
23+
}
24+
1625
// NewLogger builds the standard logger for terragrunt-ls.
1726
//
1827
// When supplied with a filename, it'll create a new file and write logs to it.
1928
// Otherwise, it'll write logs to stderr.
20-
func NewLogger(filename string) *Logger {
29+
func NewLogger(filename string) *slogLogger {
2130
if filename == "" {
22-
handler := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
31+
handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
2332
Level: slog.LevelInfo,
2433
})
2534
logger := slog.New(handler)
2635

27-
return &Logger{
36+
return &slogLogger{
2837
Logger: logger,
2938
}
3039
}
@@ -42,14 +51,14 @@ func NewLogger(filename string) *Logger {
4251
})
4352
logger := slog.New(handler)
4453

45-
return &Logger{
54+
return &slogLogger{
4655
Logger: logger,
4756
closer: file,
4857
}
4958
}
5059

5160
// Close closes the logger
52-
func (l *Logger) Close() error {
61+
func (l *slogLogger) Close() error {
5362
if l.closer != nil {
5463
return l.closer.Close()
5564
}
@@ -58,16 +67,16 @@ func (l *Logger) Close() error {
5867
}
5968

6069
// Debug logs a debug message
61-
func (l *Logger) Debug(msg string, args ...interface{}) {
70+
func (l *slogLogger) Debug(msg string, args ...interface{}) {
6271
l.Logger.Debug(msg, args...)
6372
}
6473

6574
// Info logs an info message
66-
func (l *Logger) Info(msg string, args ...interface{}) {
75+
func (l *slogLogger) Info(msg string, args ...interface{}) {
6776
l.Logger.Info(msg, args...)
6877
}
6978

7079
// Error logs an error message
71-
func (l *Logger) Error(msg string, args ...interface{}) {
80+
func (l *slogLogger) Error(msg string, args ...interface{}) {
7281
l.Logger.Error(msg, args...)
7382
}

internal/testutils/testutils.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
package testutils
33

44
import (
5+
"io"
56
"log/slog"
67
"os"
78
"path/filepath"
89
"terragrunt-ls/internal/logger"
910
"testing"
1011
)
1112

12-
func NewTestLogger(t *testing.T) *logger.Logger {
13+
var _ logger.Logger = &testLogger{}
14+
15+
// Logger is a wrapper around slog.Logger that provides additional methods
16+
type testLogger struct {
17+
*slog.Logger
18+
closer io.Closer
19+
}
20+
21+
func NewTestLogger(t *testing.T) *testLogger {
1322
t.Helper()
1423

1524
// Create a test logger that writes to the test log
@@ -20,7 +29,7 @@ func NewTestLogger(t *testing.T) *logger.Logger {
2029
slogger := slog.New(handler)
2130

2231
// Create a new logger with the test writer
23-
return &logger.Logger{
32+
return &testLogger{
2433
Logger: slogger,
2534
}
2635
}
@@ -54,3 +63,27 @@ func CreateFileWithMode(dir, name, content string, mode os.FileMode) (string, er
5463

5564
return path, nil
5665
}
66+
67+
// Close closes the logger
68+
func (l *testLogger) Close() error {
69+
if l.closer != nil {
70+
return l.closer.Close()
71+
}
72+
73+
return nil
74+
}
75+
76+
// Debug logs a debug message
77+
func (l *testLogger) Debug(msg string, args ...interface{}) {
78+
l.Logger.Debug(msg, args...)
79+
}
80+
81+
// Info logs an info message
82+
func (l *testLogger) Info(msg string, args ...interface{}) {
83+
l.Logger.Info(msg, args...)
84+
}
85+
86+
// Error logs an error message
87+
func (l *testLogger) Error(msg string, args ...interface{}) {
88+
l.Logger.Error(msg, args...)
89+
}

internal/tg/completion/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"go.lsp.dev/protocol"
1111
)
1212

13-
func GetCompletions(l *logger.Logger, store store.Store, position protocol.Position) []protocol.CompletionItem {
13+
func GetCompletions(l logger.Logger, store store.Store, position protocol.Position) []protocol.CompletionItem {
1414
cursorWord := text.GetCursorWord(store.Document, position)
1515
completions := []protocol.CompletionItem{}
1616

internal/tg/definition/definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
DefinitionContextNull = "null"
2222
)
2323

24-
func GetDefinitionTargetWithContext(l *logger.Logger, store store.Store, position protocol.Position) (string, string) {
24+
func GetDefinitionTargetWithContext(l logger.Logger, store store.Store, position protocol.Position) (string, string) {
2525
document := store.Document
2626

2727
scanner := bufio.NewScanner(strings.NewReader(document))

internal/tg/hover/hover.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
HoverContextNull = "null"
2121
)
2222

23-
func GetHoverTargetWithContext(l *logger.Logger, store store.Store, position protocol.Position) (string, string) {
23+
func GetHoverTargetWithContext(l logger.Logger, store store.Store, position protocol.Position) (string, string) {
2424
word := text.GetCursorWord(store.Document, position)
2525
if len(word) == 0 {
2626
l.Debug(

internal/tg/state.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func NewState() State {
2525
return State{Configs: map[string]store.Store{}}
2626
}
2727

28-
func (s *State) OpenDocument(l *logger.Logger, docURI protocol.DocumentURI, text string) []protocol.Diagnostic {
28+
func (s *State) OpenDocument(l logger.Logger, docURI protocol.DocumentURI, text string) []protocol.Diagnostic {
2929
l.Debug(
3030
"Opening document",
3131
"uri", docURI,
@@ -35,7 +35,7 @@ func (s *State) OpenDocument(l *logger.Logger, docURI protocol.DocumentURI, text
3535
return s.updateState(l, docURI, text)
3636
}
3737

38-
func (s *State) UpdateDocument(l *logger.Logger, docURI protocol.DocumentURI, text string) []protocol.Diagnostic {
38+
func (s *State) UpdateDocument(l logger.Logger, docURI protocol.DocumentURI, text string) []protocol.Diagnostic {
3939
l.Debug(
4040
"Updating document",
4141
"uri", docURI,
@@ -45,7 +45,7 @@ func (s *State) UpdateDocument(l *logger.Logger, docURI protocol.DocumentURI, te
4545
return s.updateState(l, docURI, text)
4646
}
4747

48-
func (s *State) updateState(l *logger.Logger, docURI protocol.DocumentURI, text string) []protocol.Diagnostic {
48+
func (s *State) updateState(l logger.Logger, docURI protocol.DocumentURI, text string) []protocol.Diagnostic {
4949
cfg, diags := parseTerragruntBuffer(docURI.Filename(), text)
5050

5151
l.Debug(
@@ -71,7 +71,7 @@ func (s *State) updateState(l *logger.Logger, docURI protocol.DocumentURI, text
7171
return diags
7272
}
7373

74-
func (s *State) Hover(l *logger.Logger, id int, docURI protocol.DocumentURI, position protocol.Position) lsp.HoverResponse {
74+
func (s *State) Hover(l logger.Logger, id int, docURI protocol.DocumentURI, position protocol.Position) lsp.HoverResponse {
7575
store := s.Configs[docURI.Filename()]
7676

7777
l.Debug(
@@ -150,7 +150,7 @@ func wrapAsHCLCodeFence(s string) string {
150150
return "```hcl\n" + s + "\n```"
151151
}
152152

153-
func (s *State) Definition(l *logger.Logger, id int, docURI protocol.DocumentURI, position protocol.Position) lsp.DefinitionResponse {
153+
func (s *State) Definition(l logger.Logger, id int, docURI protocol.DocumentURI, position protocol.Position) lsp.DefinitionResponse {
154154
store := s.Configs[docURI.Filename()]
155155

156156
l.Debug(
@@ -244,7 +244,7 @@ func buildEmptyDefinitionResponse(id int, docURI protocol.DocumentURI, position
244244
}
245245
}
246246

247-
func (s *State) TextDocumentCompletion(l *logger.Logger, id int, docURI protocol.DocumentURI, position protocol.Position) lsp.CompletionResponse {
247+
func (s *State) TextDocumentCompletion(l logger.Logger, id int, docURI protocol.DocumentURI, position protocol.Position) lsp.CompletionResponse {
248248
items := completion.GetCompletions(l, s.Configs[docURI.Filename()], position)
249249

250250
response := lsp.CompletionResponse{

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func main() {
4545
}
4646
}
4747

48-
func handleMessage(l *logger.Logger, writer io.Writer, state tg.State, method string, contents []byte) {
48+
func handleMessage(l logger.Logger, writer io.Writer, state tg.State, method string, contents []byte) {
4949
l.Debug("Received msg", "method", method, "contents", string(contents))
5050

5151
switch method {
@@ -207,7 +207,7 @@ func handleMessage(l *logger.Logger, writer io.Writer, state tg.State, method st
207207
}
208208
}
209209

210-
func writeResponse(l *logger.Logger, writer io.Writer, msg any) {
210+
func writeResponse(l logger.Logger, writer io.Writer, msg any) {
211211
reply := rpc.EncodeMessage(msg)
212212

213213
_, err := writer.Write([]byte(reply))

0 commit comments

Comments
 (0)