| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Python logging integration for the AWS CRT.""" |
| 5 |
|
| 6 |
import logging |
| 7 |
import threading |
| 8 |
from enum import IntEnum |
| 9 |
|
| 10 |
import _awscrt |
| 11 |
from awscrt.io import LogLevel |
| 12 |
|
| 13 |
__all__ = ['init_logging', 'set_log_level', 'logf', 'LogSubject', 'CRT_LOG_FORMAT'] |
| 14 |
|
| 15 |
_CRT_TO_PY_LEVEL = { |
| 16 |
0: logging.NOTSET, |
| 17 |
1: logging.CRITICAL, |
| 18 |
2: logging.ERROR, |
| 19 |
3: logging.WARNING, |
| 20 |
4: logging.INFO, |
| 21 |
5: logging.DEBUG, |
| 22 |
6: logging.DEBUG, |
| 23 |
} |
| 24 |
|
| 25 |
_PY_TO_CRT_LEVEL = { |
| 26 |
logging.NOTSET: LogLevel.NoLogs, |
| 27 |
logging.CRITICAL: LogLevel.Fatal, |
| 28 |
logging.ERROR: LogLevel.Error, |
| 29 |
logging.WARNING: LogLevel.Warn, |
| 30 |
logging.INFO: LogLevel.Info, |
| 31 |
logging.DEBUG: LogLevel.Trace, |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
CRT_LOG_FORMAT = '%(asctime)s [%(threadName)s] %(levelname)s %(name)s - %(message)s' |
| 36 |
|
| 37 |
|
| 38 |
class LogSubject(IntEnum): |
| 39 |
"""Log subject identifiers for CRT subsystems.""" |
| 40 |
|
| 41 |
COMMON_GENERAL = 0x000 |
| 42 |
COMMON_TASK_SCHEDULER = 0x001 |
| 43 |
|
| 44 |
|
| 45 |
IO_GENERAL = 0x400 |
| 46 |
IO_EVENT_LOOP = 0x401 |
| 47 |
IO_SOCKET = 0x402 |
| 48 |
IO_SOCKET_HANDLER = 0x403 |
| 49 |
IO_TLS = 0x404 |
| 50 |
IO_ALPN = 0x405 |
| 51 |
IO_DNS = 0x406 |
| 52 |
IO_PKI = 0x407 |
| 53 |
IO_CHANNEL = 0x408 |
| 54 |
IO_CHANNEL_BOOTSTRAP = 0x409 |
| 55 |
IO_FILE_UTILS = 0x40A |
| 56 |
IO_SHARED_LIBRARY = 0x40B |
| 57 |
|
| 58 |
|
| 59 |
HTTP_GENERAL = 0x800 |
| 60 |
HTTP_CONNECTION = 0x801 |
| 61 |
HTTP_SERVER = 0x802 |
| 62 |
HTTP_STREAM = 0x803 |
| 63 |
HTTP_CONNECTION_MANAGER = 0x804 |
| 64 |
HTTP_WEBSOCKET = 0x805 |
| 65 |
HTTP_WEBSOCKET_SETUP = 0x806 |
| 66 |
|
| 67 |
|
| 68 |
EVENT_STREAM_GENERAL = 0x1000 |
| 69 |
EVENT_STREAM_CHANNEL_HANDLER = 0x1001 |
| 70 |
EVENT_STREAM_RPC_SERVER = 0x1002 |
| 71 |
EVENT_STREAM_RPC_CLIENT = 0x1003 |
| 72 |
|
| 73 |
|
| 74 |
MQTT_GENERAL = 0x1400 |
| 75 |
MQTT_CLIENT = 0x1401 |
| 76 |
MQTT_TOPIC_TREE = 0x1402 |
| 77 |
|
| 78 |
|
| 79 |
AUTH_GENERAL = 0x1800 |
| 80 |
AUTH_PROFILE = 0x1801 |
| 81 |
AUTH_CREDENTIALS_PROVIDER = 0x1802 |
| 82 |
AUTH_SIGNING = 0x1803 |
| 83 |
|
| 84 |
|
| 85 |
CAL_GENERAL = 0x1C00 |
| 86 |
CAL_ECC = 0x1C01 |
| 87 |
CAL_HASH = 0x1C02 |
| 88 |
CAL_HMAC = 0x1C03 |
| 89 |
CAL_DER = 0x1C04 |
| 90 |
CAL_LIBCRYPTO_RESOLVE = 0x1C05 |
| 91 |
CAL_RSA = 0x1C06 |
| 92 |
CAL_ED25519 = 0x1C07 |
| 93 |
|
| 94 |
|
| 95 |
S3_GENERAL = 0x3800 |
| 96 |
S3_CLIENT = 0x3801 |
| 97 |
|
| 98 |
|
| 99 |
SDKUTILS_GENERAL = 0x3C00 |
| 100 |
SDKUTILS_PROFILE = 0x3C01 |
| 101 |
SDKUTILS_ENDPOINTS_PARSING = 0x3C02 |
| 102 |
SDKUTILS_ENDPOINTS_RESOLVE = 0x3C03 |
| 103 |
SDKUTILS_ENDPOINTS_GENERAL = 0x3C04 |
| 104 |
SDKUTILS_PARTITIONS_PARSING = 0x3C05 |
| 105 |
SDKUTILS_ENDPOINTS_REGEX = 0x3C06 |
| 106 |
|
| 107 |
|
| 108 |
_PACKAGE_ID_TO_MODULE = { |
| 109 |
0: 'common', |
| 110 |
1: 'io', |
| 111 |
2: 'http', |
| 112 |
4: 'event-stream', |
| 113 |
5: 'mqtt', |
| 114 |
6: 'auth', |
| 115 |
7: 'cal', |
| 116 |
14: 's3', |
| 117 |
15: 'sdkutils', |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
def _python_logging_callback(crt_level, message, subject_id, subject_name, thread_name): |
| 122 |
"""Called from C for each CRT log message.""" |
| 123 |
module = _PACKAGE_ID_TO_MODULE.get(subject_id >> 10, 'unknown') |
| 124 |
logger = logging.getLogger('awscrt.{}.{}'.format(module, subject_name)) |
| 125 |
py_level = _CRT_TO_PY_LEVEL.get(crt_level, logging.DEBUG) |
| 126 |
are |
| 127 |
trol |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
if threading.current_thread().name.startswith("Dummy") and thread_name is not None: |
| 134 |
threading.current_thread().name = thread_name |
| 135 |
logger.log(py_level, '%s', message) |
| 136 |
|
| 137 |
|
| 138 |
def init_logging(level: int = logging.DEBUG): |
| 139 |
"""Initialize CRT logging, routing output through Python's logging module. |
| 140 |
|
| 141 |
Log messages appear under the ``awscrt`` logger hierarchy, with each CRT |
| 142 |
subsystem as a child logger (e.g. ``awscrt.io.event-loop``, |
| 143 |
``awscrt.s3.S3Client``). |
| 144 |
|
| 145 |
This is mutually exclusive with :func:`~awscrt.io.init_logging` -- use one |
| 146 |
or the other, not both. Can only be called once. |
| 147 |
|
| 148 |
Example usage:: |
| 149 |
|
| 150 |
import logging |
| 151 |
from awscrt.logging import init_logging |
| 152 |
|
| 153 |
logging.basicConfig(level=logging.DEBUG) |
| 154 |
init_logging(logging.DEBUG) |
| 155 |
|
| 156 |
Args: |
| 157 |
level (int): Python logging level (e.g. ``logging.DEBUG``, |
| 158 |
``logging.WARNING``). |
| 159 |
|
| 160 |
Raises: |
| 161 |
RuntimeError: If CRT logging has already been initialized. |
| 162 |
""" |
| 163 |
root_logger = logging.getLogger('awscrt') |
| 164 |
|
| 165 |
try: |
| 166 |
crt_level = _PY_TO_CRT_LEVEL[level] |
| 167 |
except KeyError: |
| 168 |
raise ValueError(f"Invalid log level: {level}. Use logging.DEBUG, INFO, WARNING, ERROR, or CRITICAL") |
| 169 |
|
| 170 |
if root_logger.level == logging.NOTSET: |
| 171 |
root_logger.setLevel(_CRT_TO_PY_LEVEL.get(int(crt_level), logging.DEBUG)) |
| 172 |
|
| 173 |
_awscrt.init_python_logging(int(crt_level), _python_logging_callback) |
| 174 |
|
| 175 |
|
| 176 |
def set_log_level(level: int): |
| 177 |
"""Change the CRT log level. :func:`init_logging` must have been called first. |
| 178 |
|
| 179 |
Set log level to logging.NOTSET to disable the logger. Cleaning up the logger |
| 180 |
is dangerous in a multi-threaded environment. |
| 181 |
|
| 182 |
Args: |
| 183 |
level (int): Python logging level (e.g. ``logging.DEBUG``, |
| 184 |
``logging.WARNING``). |
| 185 |
""" |
| 186 |
try: |
| 187 |
crt_level = _PY_TO_CRT_LEVEL[level] |
| 188 |
except KeyError: |
| 189 |
raise ValueError(f"Invalid log level: {level}. Use logging.DEBUG, INFO, WARNING, ERROR, or CRITICAL") |
| 190 |
_awscrt.set_log_level(int(crt_level)) |
| 191 |
|
| 192 |
|
| 193 |
def log(level: int, subject: LogSubject, message: str): |
| 194 |
"""Log a message through the CRT's configured logger. |
| 195 |
|
| 196 |
Args: |
| 197 |
level (int): Python logging level (e.g. logging.DEBUG, logging.INFO). |
| 198 |
subject (LogSubject): Log subject identifying the subsystem. |
| 199 |
message (str): The message to log. |
| 200 |
""" |
| 201 |
try: |
| 202 |
crt_level = _PY_TO_CRT_LEVEL[level] |
| 203 |
except KeyError: |
| 204 |
raise ValueError(f"Invalid log level: {level}. Use logging.DEBUG, INFO, WARNING, ERROR, or CRITICAL") |
| 205 |
_awscrt.logger_log(int(crt_level), int(subject), message) |