| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
"""Functionality for streaming Cylc logs over network interfaces.""" |
| 18 |
|
| 19 |
from logging import NOTSET, Handler, LogRecord |
| 20 |
from typing import TYPE_CHECKING |
| 21 |
|
| 22 |
from ansimarkup import strip as cstrip |
| 23 |
|
| 24 |
if TYPE_CHECKING: |
| 25 |
from cylc.flow.scheduler import Scheduler |
| 26 |
|
| 27 |
|
| 28 |
class ProtobufStreamHandler(Handler): |
| 29 |
"""Log handler for routing log messages via Protobuf.""" |
| 30 |
|
| 31 |
def __init__(self, schd: 'Scheduler', level: int = NOTSET): |
| 32 |
Handler.__init__(self, level=level) |
| 33 |
self.schd = schd |
| 34 |
|
| 35 |
def emit(self, record: LogRecord): |
| 36 |
self.schd.data_store_mgr.delta_log_record( |
| 37 |
record.levelname, cstrip(self.format(record)) |
| 38 |
) |