Logging Handlers

class urwid.log_handler.ListWalkerHandler(walker: _Appendable, *, loop: MainLoop, level: int | str = 0, severity_attr: Mapping[Severity, Hashable] | None = None, max_pending: int = 1000)

Append each log record as a Text row to a list walker, on the thread of the loop.

Records may be logged from any thread. A SimpleFocusDequeWalker with maxlen and follow_end=True keeps the newest rows and keeps the newest one in view in a ListBox. The handler needs call_from_thread(), so an event loop that can watch files. Call resume() once at the start of each run; with loop.start(): lets its error reach the caller, while an alarm set before run() only logs it. urwid_filter rejects records of the urwid loggers. To show them, set the handler’s level to INFO or above and remove the filter: urwid logs at DEBUG on each redraw, and each shown record redraws.

>>> import logging
>>> import urwid
>>> from urwid.testing import ScriptedScreen, VirtualClockEventLoop
>>> walker = urwid.SimpleFocusDequeWalker([], maxlen=100, follow_end=True)
>>> event_loop = VirtualClockEventLoop()
>>> loop = urwid.MainLoop(urwid.ListBox(walker), screen=ScriptedScreen(inputs=None), event_loop=event_loop)
>>> handler = ListWalkerHandler(walker, loop=loop, severity_attr={urwid.Severity.WARNING: "warning"})
>>> logger = logging.getLogger("example.walker")
>>> logger.addHandler(handler)
>>> logger.warning("disk almost full")
>>> with loop.start():
...     handler.resume()
...     event_loop.advance(0)
>>> walker[0].get_text()
('disk almost full', [('warning', 16)])
>>> logger.removeHandler(handler)

Create a handler that appends to walker.

Parameters:
  • walker – list walker with an append() method.

  • loop – loop whose thread changes walker.

  • level – lowest level handled.

  • severity_attr – display attribute of a row for each Severity; a severity with no entry is not coloured.

  • max_pending – records held until the loop shows them; the oldest is dropped first.

Raises:
  • ValueError – max_pending is below 1, or level is a name logging does not know.

  • TypeError – level is neither an int nor a str, a key of severity_attr is not a Severity, or a value is not hashable.

close() → None

Drop the queued records and ignore later ones, without touching the widget.

emit(record: LogRecord) → None

Queue the formatted record and hand a call that shows it to the loop.

A failure of the formatter goes to handleError(). Records of this module’s own logger are ignored, so a failing widget cannot report its failures to itself.

flush() → None

Show the records queued now in the widget.

Call it on the loop thread while the loop runs, for example before a widget callback stops the loop or reads the widget, so the widget shows every record logged before the call, without waiting for the loop to hand them over. Records logged while it shows them, such as by the widget, wait for the next hand-over; when they fill max_pending, the oldest queued records are dropped as usual. On any other thread, also one that runs in a copy of the loop’s context, and after close(), it does nothing and the records stay queued. A call from a widget callback during a hand-over shows nothing, and leaves the rest to that hand-over. Any exception from the widget other than ExitMainLoop is logged and the next entry is shown.

Raises:

ExitMainLoop – the widget stopped the loop.

resume() → None

Hand a call that shows the queued records to the loop.

Call it once at the start of each run of the loop, inside with loop.start(): or from an alarm set before run(). stop() drops a queued call, and without resume() the records held before start() or after the drop stay queued, also when new records arrive. It is safe from any thread, and does nothing after close().

Raises:

RuntimeError – the loop is not running, or its event loop cannot watch files, see call_from_thread().

urwid_filter: Final

Filter rejecting records of the urwid logger and its children. urwid logs at DEBUG on each redraw and each alarm it sets, so showing those records logs again without end.

class urwid.log_handler.NotificationsHandler(notifications: Notifications, *, loop: MainLoop, level: int | str = 0, timeout: float | Literal[_Unset.UNSET] | None = <unset>, max_pending: int = 1000)

Post each log record as a message of Notifications, on the thread of the loop.

Records may be logged from any thread. The message carries the record’s Severity. The handler needs call_from_thread(), so an event loop that can watch files. Call resume() once at the start of each run; with loop.start(): lets its error reach the caller, while an alarm set before run() only logs it. urwid_filter rejects records of the urwid loggers. To show them, set the handler’s level to INFO or above and remove the filter: urwid logs at DEBUG on each alarm it sets, and each message sets one.

>>> import logging
>>> import urwid
>>> from urwid.testing import ScriptedScreen, VirtualClockEventLoop
>>> notifications = urwid.Notifications(timeout=5)
>>> event_loop = VirtualClockEventLoop()
>>> loop = urwid.MainLoop(urwid.Filler(notifications), screen=ScriptedScreen(inputs=None), event_loop=event_loop)
>>> handler = NotificationsHandler(notifications, loop=loop)
>>> logger = logging.getLogger("example.notifications")
>>> logger.addHandler(handler)
>>> logger.error("upload failed")
>>> with loop.start():
...     handler.resume()
...     event_loop.advance(0)
...     notifications.notifications
(<Notification 'upload failed' severity=ERROR>,)
>>> logger.removeHandler(handler)

Create a handler that posts to notifications.

Parameters:
  • notifications – widget the messages are posted to.

  • loop – loop whose thread posts the messages; it also runs their alarms.

  • level – lowest level handled.

  • timeout – seconds a message stays, None to keep it, or left out to use the widget’s timeout.

  • max_pending – records held until the loop shows them; the oldest is dropped first.

Raises:
  • ValueError – notifications is bound to another loop, timeout is not finite and > 0, max_pending is below 1, or level is a name logging does not know.

  • TypeError – level is neither an int nor a str, or timeout is not a number or None.

close() → None

Drop the queued records and ignore later ones, without touching the widget.

emit(record: LogRecord) → None

Queue the formatted record and hand a call that shows it to the loop.

A failure of the formatter goes to handleError(). Records of this module’s own logger are ignored, so a failing widget cannot report its failures to itself.

flush() → None

Show the records queued now in the widget.

Call it on the loop thread while the loop runs, for example before a widget callback stops the loop or reads the widget, so the widget shows every record logged before the call, without waiting for the loop to hand them over. Records logged while it shows them, such as by the widget, wait for the next hand-over; when they fill max_pending, the oldest queued records are dropped as usual. On any other thread, also one that runs in a copy of the loop’s context, and after close(), it does nothing and the records stay queued. A call from a widget callback during a hand-over shows nothing, and leaves the rest to that hand-over. Any exception from the widget other than ExitMainLoop is logged and the next entry is shown.

Raises:

ExitMainLoop – the widget stopped the loop.

resume() → None

Hand a call that shows the queued records to the loop.

Call it once at the start of each run of the loop, inside with loop.start(): or from an alarm set before run(). stop() drops a queued call, and without resume() the records held before start() or after the drop stay queued, also when new records arrive. It is safe from any thread, and does nothing after close().

Raises:

RuntimeError – the loop is not running, or its event loop cannot watch files, see call_from_thread().

urwid_filter: Final

Filter rejecting records of the urwid logger and its children. urwid logs at DEBUG on each redraw and each alarm it sets, so showing those records logs again without end.