Skip to content

Commit cde3015

Browse files
committed
Use trace level for event content and other sensitive info
1 parent 818d800 commit cde3015

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

mautrix/api/http.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ def __init__(self, base_url: str, token: str, *, client_session: ClientSession =
123123
self.log: Optional[logging.Logger] = log or logging.getLogger("mautrix.api")
124124
self.loop = loop or asyncio.get_event_loop()
125125
self.session: ClientSession = client_session or ClientSession(loop=self.loop)
126-
self.log_sync = False
127126
if txn_id is not None:
128127
self.txn_id: int = txn_id
129128

@@ -155,18 +154,17 @@ def _log_request(self, method: Method, path: PathBuilder, content: Union[str, by
155154
orig_content, query_params: Dict[str, str]) -> None:
156155
if not self.log:
157156
return
158-
if not self.log_sync and path == Path.sync:
159-
return
160157
log_content = content if not isinstance(content, bytes) else f"<{len(content)} bytes>"
161158
as_user = query_params.get("user_id", None)
162-
self.log.debug(f"{method} {path} {log_content}".strip(" "),
163-
extra={"matrix_http_request": {
164-
"method": str(method),
165-
"path": str(path),
166-
"content": (orig_content if isinstance(orig_content, (dict, list))
167-
else log_content),
168-
"user": as_user,
169-
}})
159+
level = 1 if path == Path.sync else 5
160+
self.log.log(level, f"{method} {path} {log_content}".strip(" "),
161+
extra={"matrix_http_request": {
162+
"method": str(method),
163+
"path": str(path),
164+
"content": (orig_content if isinstance(orig_content, (dict, list))
165+
else log_content),
166+
"user": as_user,
167+
}})
170168

171169
async def request(self, method: Method, path: PathBuilder,
172170
content: Optional[Union[JSON, bytes, str]] = None,

mautrix/bridge/matrix.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
EncryptedEvent)
1616
from mautrix.errors import IntentError, MatrixError, MForbidden
1717
from mautrix.appservice import AppService
18+
from mautrix.util.logging import TraceLogger
1819

1920
from .commands import CommandProcessor
2021

@@ -32,7 +33,7 @@
3233

3334

3435
class BaseMatrixHandler(ABC):
35-
log: logging.Logger = logging.getLogger("mau.mx")
36+
log: TraceLogger = logging.getLogger("mau.mx")
3637
az: AppService
3738
commands: CommandProcessor
3839
config: 'BaseBridgeConfig'
@@ -249,10 +250,11 @@ async def handle_message(self, room_id: RoomID, user_id: UserID, message: Messag
249250
event_id: EventID) -> None:
250251
sender = await self.get_user(user_id)
251252
if not sender or not await self.allow_message(sender):
252-
self.log.debug(f"Ignoring message \"{message}\" from {user_id} to {room_id}:"
253+
self.log.debug(f"Ignoring message {event_id} from {user_id} to {room_id}:"
253254
" User is not whitelisted.")
254255
return
255-
self.log.debug(f"Received Matrix event \"{message}\" from {sender.mxid} in {room_id}")
256+
self.log.debug(f"Received Matrix event {event_id} from {sender.mxid} in {room_id}")
257+
self.log.trace("Event %s content: %s", event_id, message)
256258

257259
if isinstance(message, TextMessageEventContent):
258260
message.trim_reply_fallback()
@@ -296,7 +298,7 @@ async def try_handle_sync_event(self, evt: Event) -> None:
296298
if isinstance(evt, (ReceiptEvent, PresenceEvent, TypingEvent)):
297299
await self.handle_ephemeral_event(evt)
298300
else:
299-
self.log.debug("Unknown event type received from sync:", evt)
301+
self.log.trace("Unknown event type received from sync: %s", evt)
300302
except Exception:
301303
self.log.exception("Error handling manually received Matrix event")
302304

@@ -324,7 +326,7 @@ async def int_handle_event(self, evt: Event) -> None:
324326
await self.e2ee.handle_room_membership(evt)
325327
if self.filter_matrix_event(evt):
326328
return
327-
self.log.debug("Received event: %s", evt)
329+
self.log.trace("Received event: %s", evt)
328330
start_time = time.time()
329331

330332
if evt.type == EventType.ROOM_MEMBER:

mautrix/util/logging/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .color import ColorFormatter
2-
from .trace import TRACE
2+
from .trace import TRACE, TraceLogger

mautrix/util/logging/trace.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
TRACE = logging.TRACE = 5
1010
logging.addLevelName(TRACE, "TRACE")
1111

12+
SILLY = logging.SILLY = 1
13+
logging.addLevelName(SILLY, "SILLY")
14+
1215
OldLogger: Type[logging.Logger] = cast(Type[logging.Logger], logging.getLoggerClass())
1316

1417

1518
class TraceLogger(OldLogger):
1619
def trace(self, msg, *args, **kwargs) -> None:
1720
self.log(TRACE, msg, *args, **kwargs)
1821

22+
def silly(self, msg, *args, **kwargs) -> None:
23+
self.log(SILLY, msg, *args, **kwargs)
24+
1925

2026
logging.setLoggerClass(TraceLogger)

0 commit comments

Comments
 (0)