|
1 | 1 | # Logging
|
2 | 2 |
|
3 | 3 | AGNext uses Python's built-in [`logging`](https://docs.python.org/3/library/logging.html) module.
|
4 |
| -The logger names are: |
5 | 4 |
|
6 |
| -- `agnext` for the main logger. |
| 5 | +There are two kinds of logging: |
7 | 6 |
|
8 |
| -Example of how to use the logger: |
| 7 | +- **Trace logging**: This is used for debugging and is human readable messages to indicate what is going on. This is intended for a developer to understand what is happening in the code. The content and format of these logs should not be depended on by other systems. |
| 8 | + - Name: {py:attr}`~agnext.application.logging.TRACE_LOGGER_NAME`. |
| 9 | +- **Structured logging**: This logger emits structured events that can be consumed by other systems. The content and format of these logs should be can be depended on by other systems. |
| 10 | + - Name: {py:attr}`~agnext.application.logging.EVENT_LOGGER_NAME`. |
| 11 | + - See the module {py:mod}`agnext.application.logging.events` to see the available events. |
| 12 | +- {py:attr}`~agnext.application.logging.ROOT_LOGGER` can be used to enable or disable all logs at the same time. |
| 13 | + |
| 14 | +## Enabling logging output |
| 15 | + |
| 16 | +To enable trace logging, you can use the following code: |
9 | 17 |
|
10 | 18 | ```python
|
11 | 19 | import logging
|
12 | 20 |
|
| 21 | +from agnext.application.logging import TRACE_LOGGER_NAME |
| 22 | + |
13 | 23 | logging.basicConfig(level=logging.WARNING)
|
14 |
| -logger = logging.getLogger('agnext') |
| 24 | +logger = logging.getLogger(TRACE_LOGGER_NAME) |
15 | 25 | logger.setLevel(logging.DEBUG)
|
16 | 26 | ```
|
| 27 | + |
| 28 | +### Structured logging |
| 29 | + |
| 30 | +Structured logging allows you to write handling logic that deals with the actual events including all fields rather than just a formatted string. |
| 31 | + |
| 32 | +For example, if you had defined this custom event and were emitting it. Then you could write the following handler to receive it. |
| 33 | + |
| 34 | +```python |
| 35 | +import logging |
| 36 | +from dataclasses import dataclass |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class MyEvent: |
| 40 | + timestamp: str |
| 41 | + message: str |
| 42 | + |
| 43 | +class MyHandler(logging.Handler): |
| 44 | + def __init__(self) -> None: |
| 45 | + super().__init__() |
| 46 | + |
| 47 | + def emit(self, record: logging.LogRecord) -> None: |
| 48 | + try: |
| 49 | + # Use the StructuredMessage if the message is an instance of it |
| 50 | + if isinstance(record.msg, MyEvent): |
| 51 | + print(f"Timestamp: {record.msg.timestamp}, Message: {record.msg.message}") |
| 52 | + except Exception: |
| 53 | + self.handleError(record) |
| 54 | +``` |
| 55 | + |
| 56 | +And this is how you could use it: |
| 57 | + |
| 58 | +```python |
| 59 | +logger = logging.getLogger(EVENT_LOGGER_NAME) |
| 60 | +logger.setLevel(logging.INFO) |
| 61 | +my_handler = MyHandler() |
| 62 | +logger.handlers = [my_handler] |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +## Emitting logs |
| 67 | + |
| 68 | +These two names are the root loggers for these types. Code that emits logs should use a child logger of these loggers. For example, if you are writing a module `my_module` and you want to emit trace logs, you should use the logger named: |
| 69 | + |
| 70 | +```python |
| 71 | +import logging |
| 72 | + |
| 73 | +from agnext.application.logging import TRACE_LOGGER_NAME |
| 74 | +logger = logging.getLogger(f"{TRACE_LOGGER_NAME}.my_module") |
| 75 | +``` |
| 76 | + |
| 77 | +### Emitting structured logs |
| 78 | + |
| 79 | +If your event looks like: |
| 80 | + |
| 81 | +```python |
| 82 | +from dataclasses import dataclass |
| 83 | + |
| 84 | +@dataclass |
| 85 | +class MyEvent: |
| 86 | + timestamp: str |
| 87 | + message: str |
| 88 | +``` |
| 89 | + |
| 90 | +Then it could be emitted in code like this: |
| 91 | + |
| 92 | +```python |
| 93 | +from agnext.application.logging import EVENT_LOGGER_NAME |
| 94 | + |
| 95 | +logger = logging.getLogger(EVENT_LOGGER_NAME + ".my_module") |
| 96 | +logger.info(MyEvent("timestamp", "message")) |
| 97 | +``` |
0 commit comments