-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish python logging events to the Language Client (#27)
- Loading branch information
Showing
4 changed files
with
53 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Fix discovery of roles so that roles in Sphinx domains are used and that unimplemented | ||
``docutils`` roles are not surfaced. | ||
**Language Server:** Fix discovery of roles so that roles in Sphinx domains are used and | ||
that unimplemented ``docutils`` roles are not surfaced. |
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 @@ | ||
**Language Server:** Python log events can now published to Language Clients |
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,34 @@ | ||
"""This module defines a custom logging handler that publishes log messages to an LSP | ||
client.""" | ||
|
||
import logging | ||
|
||
from pygls.server import LanguageServer | ||
from pygls.types import MessageType | ||
|
||
_LOG_LEVELS = { | ||
logging.DEBUG: MessageType.Info, | ||
logging.INFO: MessageType.Info, | ||
logging.WARNING: MessageType.Warning, | ||
logging.ERROR: MessageType.Error, | ||
logging.CRITICAL: MessageType.Error, | ||
} | ||
|
||
|
||
class LspHandler(logging.Handler): | ||
"""A logging handler that will send log records to an LSP client.""" | ||
|
||
def __init__(self, server: LanguageServer): | ||
super().__init__() | ||
self.server = server | ||
|
||
def emit(self, record: logging.LogRecord) -> None: | ||
"""Sends the record to the client.""" | ||
|
||
# To avoid infinite recursions, it's simpler to just ignore all log records | ||
# coming from pygls... | ||
if "pygls" in record.name: | ||
return | ||
|
||
log = self.format(record) | ||
self.server.show_message_log(log, msg_type=_LOG_LEVELS[record.levelno]) |