-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lsp: enable levelled logging (#1188)
* lsp: Extend server logging with levels Levels can be used to control how verbose the server logging is. Signed-off-by: Charlie Egan <charlie@styra.com> * lsp: add more debug log statements Signed-off-by: Charlie Egan <charlie@styra.com> --------- Signed-off-by: Charlie Egan <charlie@styra.com>
- Loading branch information
1 parent
e3fa956
commit 1dbfc7e
Showing
12 changed files
with
261 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package log | ||
|
||
// Level controls the level of server logging and corresponds to TraceValue in | ||
// the LSP spec: | ||
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#traceValue. | ||
type Level int | ||
|
||
const ( | ||
// LevelOff is used to disable logging completely. | ||
LevelOff Level = iota | ||
// LogLevelMessage are intended to contain errors and other messages that | ||
// should be shown in normal operation. | ||
LevelMessage | ||
// LogLevelDebug is includes LogLevelMessage, but also information that is | ||
// not expected to be useful unless debugging the server. | ||
LevelDebug | ||
) | ||
|
||
func (l Level) String() string { | ||
return [...]string{"Off", "Messages", "Debug"}[l] | ||
} | ||
|
||
func (l Level) ShouldLog(incoming Level) bool { | ||
if l == LevelOff { | ||
return false | ||
} | ||
|
||
if l == LevelDebug { | ||
return true | ||
} | ||
|
||
return l == LevelMessage && incoming == LevelMessage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package log | ||
|
||
import "testing" | ||
|
||
func TestShouldLog(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
Level Level | ||
IncomingMessageLevel Level | ||
ExpectLog bool | ||
}{ | ||
"Off, no logs": { | ||
Level: LevelOff, | ||
IncomingMessageLevel: LevelMessage, | ||
ExpectLog: false, | ||
}, | ||
"Off, no logs from debug": { | ||
Level: LevelOff, | ||
IncomingMessageLevel: LevelDebug, | ||
ExpectLog: false, | ||
}, | ||
"Message, no logs from debug": { | ||
Level: LevelMessage, | ||
IncomingMessageLevel: LevelDebug, | ||
ExpectLog: false, | ||
}, | ||
"Debug, logs from Message": { | ||
Level: LevelDebug, | ||
IncomingMessageLevel: LevelMessage, | ||
ExpectLog: true, | ||
}, | ||
"Debug, all logs": { | ||
Level: LevelDebug, | ||
IncomingMessageLevel: LevelDebug, | ||
ExpectLog: true, | ||
}, | ||
} | ||
|
||
for label, tc := range testCases { | ||
t.Run(label, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
if got := tc.Level.ShouldLog(tc.IncomingMessageLevel); got != tc.ExpectLog { | ||
t.Errorf("expected %v, got %v", tc.ExpectLog, got) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.