moonraker: logging improvements

Move logging setup to the Server class and enable asyncio debugging.
Sanitize debug logging for all "/access" endpoints so tokens and
passwords are not logged.

SIgned-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2021-12-11 14:03:15 -05:00
parent c69ded21b0
commit c6cddf4b05
3 changed files with 22 additions and 5 deletions

View File

@ -149,9 +149,7 @@ class MoonrakerApp:
mimetypes.add_type('text/plain', '.gcode')
mimetypes.add_type('text/plain', '.cfg')
self.debug = config.getboolean('enable_debug_logging', False)
log_level = logging.DEBUG if self.debug else logging.INFO
logging.getLogger().setLevel(log_level)
self.debug = self.server.is_debug_enabled()
app_args: Dict[str, Any] = {
'serve_traceback': self.debug,
'websocket_ping_interval': 10,
@ -544,6 +542,14 @@ class DynamicRequestHandler(AuthorizedRequestHandler):
args[key] = value
return args
def _log_debug(self, header: str, args: Dict[str, Any]) -> None:
if self.server.is_debug_enabled():
if self.request.path.startswith('/access'):
resp = {key: "<sanitized>" for key in args}
else:
resp = args
logging.debug(f"{header}::{resp}")
async def get(self, *args, **kwargs) -> None:
await self._process_http_request()
@ -579,7 +585,7 @@ class DynamicRequestHandler(AuthorizedRequestHandler):
conn = self.get_associated_websocket()
args = self.parse_args()
req = f"{self.request.method} {self.request.path}"
logging.debug(f"HTTP Request::{req}::{args}")
self._log_debug(f"HTTP Request::{req}", args)
try:
result = await self._do_request(args, conn)
except ServerError as e:
@ -587,7 +593,7 @@ class DynamicRequestHandler(AuthorizedRequestHandler):
e.status_code, str(e)) from e
if self.wrap_result:
result = {'result': result}
logging.debug(f"HTTP Response::{req}::{result}")
self._log_debug(f"HTTP Response::{req}", result)
self.finish(result)
class FileRequestHandler(AuthorizedFileHandler):

View File

@ -38,6 +38,7 @@ class EventLoop:
self.create_future = self.aioloop.create_future
self.create_task = self.aioloop.create_task
self.call_at = self.aioloop.call_at
self.set_debug = self.aioloop.set_debug
def register_callback(self,
callback: FlexCallback,

View File

@ -88,6 +88,13 @@ class Server:
self.ssl_port: int = config.getint('ssl_port', 7130)
self.exit_reason: str = ""
# Configure Debug Logging
self.debug = config.getboolean('enable_debug_logging', False)
asyncio_debug = config.getboolean('enable_asyncio_debug', False)
log_level = logging.DEBUG if self.debug else logging.INFO
logging.getLogger().setLevel(log_level)
self.event_loop.set_debug(asyncio_debug)
# Event initialization
self.events: Dict[str, List[FlexCallback]] = {}
@ -160,6 +167,9 @@ class Server:
def is_running(self) -> bool:
return self.server_running
def is_debug_enabled(self) -> bool:
return self.debug
async def _start_server(self):
optional_comps: List[Coroutine] = []
for name, component in self.components.items():