aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/webui/handlers/log.py
blob: 81324979470140141a23d73a376fdbe1290ac57a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright (c) 2016 Weitian LI <liweitianux@live.com>
# MIT license

"""
Custom logging handlers

WebSocketLogHandler :
    Send logging messages to the WebSocket as JSON-encoded string.
"""


import logging
import json


class WebSocketLogHandler(logging.Handler):
    """
    Push the logging messages to the client(s) through the WebSocket(s)
    as JSON-encoded strings.

    Parameters
    ----------
    websockets : set of `~tornado.websocket.WebSocketHandler`
        Set of opened websockets, through which the logging messages will
        be pushed.
    msg_type : str, optional
        Set the type of the pushed logging messages, for easier handling
        by the client.

    NOTE
    ----
    The pushed logging message is a JSON-encoded string from a dictionary:
    ``{"type": self.msg_type,
       "subtype": "log",
       "action": "push",
       "levelname": record.levelname,
       "levelno": record.levelno,
       "name": record.name,
       "asctime": record.asctime,
       "message": <formatted-message>}``
    """
    def __init__(self, websockets, msg_type=None):
        super().__init__()
        self.websockets = websockets
        self.msg_type = msg_type

    def emit(self, record):
        try:
            message = self.format(record)
            msg = json.dumps({
                "type": self.msg_type,
                "subtype": "log",
                "action": "push",
                "levelname": record.levelname,
                "levelno": record.levelno,
                "name": record.name,
                "asctime": record.asctime,
                "message": message,
            })
            for ws in self.websockets:
                ws.write_message(msg)
        except Exception:
            self.handleError(record)