Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c5a9fe8
added logging pipeline
azkrishpy Apr 4, 2026
1804aab
add test and lint
azkrishpy Apr 6, 2026
2dc8981
remove python formatter
azkrishpy Apr 6, 2026
0f84ece
formatting
azkrishpy Apr 6, 2026
2946561
fix compilation and linting
azkrishpy Apr 6, 2026
d2043f5
set log level at teardown
azkrishpy Apr 6, 2026
e6dafb3
add documentation
azkrishpy Apr 6, 2026
24fa990
change to use logging level instead of crt level
azkrishpy Apr 6, 2026
23bc05f
re-organize logging
azkrishpy Apr 7, 2026
4a199fc
remove emit helper
azkrishpy Apr 7, 2026
ae006cf
Merge branch 'main' into py-logging
azkrishpy Apr 7, 2026
3ec9bfc
changes to formatting and pipeline
azkrishpy Apr 8, 2026
0b8c21b
lint
azkrishpy Apr 8, 2026
150f901
complete revamp of logger
azkrishpy Apr 9, 2026
ea68050
fix tests
azkrishpy Apr 9, 2026
429c49c
better logging
azkrishpy Apr 9, 2026
b6ef442
tested and actually caught a bug
azkrishpy Apr 9, 2026
c98b39f
update doc
azkrishpy Apr 10, 2026
85306ba
fix cleanup
azkrishpy Apr 10, 2026
206b1cd
add failure test
azkrishpy Apr 10, 2026
51faa3f
remove doc
azkrishpy Apr 10, 2026
1006e20
logger.log() instead
azkrishpy Apr 10, 2026
efd8b77
Merge branch 'main' into py-logging
azkrishpy Apr 13, 2026
1b4fb57
more updates
azkrishpy Apr 14, 2026
5d813e4
addressing comments
azkrishpy Apr 14, 2026
c44ecf1
make logger init atomic
azkrishpy Apr 14, 2026
b97de43
more comment addressing
azkrishpy Apr 14, 2026
cd02ec4
lint
azkrishpy Apr 14, 2026
b5e8f4b
handle error correctly and change default naming
azkrishpy Apr 15, 2026
41d7b85
raise py error right
azkrishpy Apr 15, 2026
d1708c5
fix it right
azkrishpy Apr 15, 2026
63092ad
logger crash should crash, don't want crash? don't init logger and cr…
azkrishpy Apr 15, 2026
a8d4d49
return none right
azkrishpy Apr 15, 2026
eae85a3
fix the ternary
azkrishpy Apr 15, 2026
1362242
lint
azkrishpy Apr 15, 2026
076e914
redefine types and decrement ref
azkrishpy Apr 15, 2026
7e24817
handle for 3.9-
azkrishpy Apr 15, 2026
44f4b75
ugh
azkrishpy Apr 15, 2026
d5faf92
return error with explanation
azkrishpy Apr 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions awscrt/logging.py
Comment thread
azkrishpy marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.

"""Python logging integration for the AWS CRT."""

import logging
from enum import IntEnum

import _awscrt
from awscrt.io import LogLevel


_CRT_TO_PY_LEVEL = {
0: logging.NOTSET,
1: logging.CRITICAL,
2: logging.ERROR,
3: logging.WARNING,
4: logging.INFO,
5: logging.DEBUG,
6: logging.DEBUG,
}

_PY_TO_CRT_LEVEL = {
logging.NOTSET: LogLevel.NoLogs,
logging.CRITICAL: LogLevel.Fatal,
logging.ERROR: LogLevel.Error,
logging.WARNING: LogLevel.Warn,
logging.INFO: LogLevel.Info,
logging.DEBUG: LogLevel.Trace,
}

#: Recommended format string for CRT log output. Includes timestamp, CRT thread
#: name, level, logger name, and message. Use with a :class:`logging.Handler`::
#:
#: import logging
#: from awscrt.logging import init_logging, CRT_LOG_FORMAT
#:
#: handler = logging.StreamHandler()
#: handler.setFormatter(logging.Formatter(CRT_LOG_FORMAT))
#: logging.getLogger('awscrt').addHandler(handler)
#: init_logging(logging.DEBUG)
CRT_LOG_FORMAT = '%(asctime)s [%(threadName)s] %(levelname)s %(name)s - %(message)s'


class LogSubject(IntEnum):
"""Log subject identifiers for CRT subsystems."""
# aws-c-common
CommonGeneral = 0x000
CommonTaskScheduler = 0x001

# aws-c-io
IoGeneral = 0x400
IoEventLoop = 0x401
IoSocket = 0x402
IoSocketHandler = 0x403
IoTls = 0x404
IoAlpn = 0x405
IoDns = 0x406
IoPki = 0x407
IoChannel = 0x408
IoChannelBootstrap = 0x409
IoFileUtils = 0x40A
IoSharedLibrary = 0x40B

# aws-c-http
HttpGeneral = 0x800
HttpConnection = 0x801
HttpServer = 0x802
HttpStream = 0x803
HttpConnectionManager = 0x804
HttpWebsocket = 0x805
HttpWebsocketSetup = 0x806

# aws-c-mqtt
MqttGeneral = 0x1400
MqttClient = 0x1401
MqttTopicTree = 0x1402

# aws-c-auth
AuthGeneral = 0x1800
AuthProfile = 0x1801
AuthCredentialsProvider = 0x1802
AuthSigning = 0x1803

# aws-c-s3
S3General = 0x4000
S3Client = 0x4001


def _python_logging_callback(crt_level, subject_name, message, thread_name, timestamp):
"""Called from C writer thread for each CRT log message."""
logger = logging.getLogger('awscrt.{}'.format(subject_name))
py_level = _CRT_TO_PY_LEVEL.get(crt_level, logging.DEBUG)
record = logger.makeRecord(
logger.name, py_level, '', 0, '%s', (message,), None
)
record.created = timestamp
record.threadName = thread_name
record.msecs = (timestamp - int(timestamp)) * 1000
logger.handle(record)


def init_logging(level: int):
"""Initialize CRT logging, routing output through Python's logging module.

Log messages appear under the ``awscrt`` logger hierarchy, with each CRT
subsystem as a child logger (e.g. ``awscrt.event-loop``,
``awscrt.task-scheduler``).

A default handler with timestamp and thread name formatting is attached
to the ``awscrt`` logger if it has no handlers yet.

This is mutually exclusive with :func:`~awscrt.io.init_logging` -- use one
or the other, not both. Can only be called once.

Example usage::

import logging
from awscrt.logging import init_logging

logging.basicConfig(level=logging.DEBUG)
init_logging(logging.DEBUG)

Args:
level (int): Python logging level (e.g. ``logging.DEBUG``,
``logging.WARNING``).
"""
root_logger = logging.getLogger('awscrt')

crt_level = _PY_TO_CRT_LEVEL.get(level, LogLevel.Warn)

if root_logger.level == logging.NOTSET:
root_logger.setLevel(_CRT_TO_PY_LEVEL.get(int(crt_level), logging.DEBUG))

_awscrt.init_python_logging(int(crt_level), _python_logging_callback)


def set_log_level(level: int):
"""Change the CRT log level. :func:`init_logging` must have been called first.
Comment thread
azkrishpy marked this conversation as resolved.

Args:
level (int): Python logging level (e.g. ``logging.DEBUG``,
``logging.WARNING``).
"""
crt_level = _PY_TO_CRT_LEVEL.get(level, LogLevel.Warn)
_awscrt.set_log_level(int(crt_level))


def logf(level: int, subject: LogSubject, message: str):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why naming it as logf? in Java, we name it just log. I believe f stardards for format and google told me:

Log: Takes a variable number of arguments and prints them (similar to fmt.Println).
Logf: Takes a format string and arguments, allowing for formatted output using placeholders like %d or %s (similar to fmt.Printf). 

And here we just take a message and subject, I think it's better to name it to log instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only called it logf because we are directly calling AWS_LOGF() i some sense.. but yes we can use log as well... but it would also conflict with logger.log() is that a problem we need to worry about?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, it's like we have two init_logging method...

"""Log a message through the CRT's configured logger.

Args:
level (int): Python logging level (e.g. logging.DEBUG, logging.INFO).
subject (LogSubject): Log subject identifying the subsystem.
message (str): The message to log.
"""
crt_level = _PY_TO_CRT_LEVEL.get(level, LogLevel.Warn)
_awscrt.logger_log(int(crt_level), int(subject), message)
1 change: 1 addition & 0 deletions source/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ PyObject *aws_py_init_logging(PyObject *self, PyObject *args);
* Change logging level of the global logger.
*/
PyObject *aws_py_set_log_level(PyObject *self, PyObject *args);
PyObject *aws_py_init_python_logging(PyObject *self, PyObject *args);

/**
* Returns True if ALPN is available, False if it is not.
Expand Down
Loading
Loading