|
| 1 | +# pybotx-smartapp-smart-logger |
| 2 | + |
| 3 | +## Интеграция [pybotx-smart-logger](https://github.com/ExpressApp/pybotx-smart-logger) и [pybotx-smartapp-rpc](https://github.com/ExpressApp/pybotx-smartapp-rpc) |
| 4 | + |
| 5 | +## Установка и использование |
| 6 | + |
| 7 | +1. Устанавливаем библиотеку: |
| 8 | +```bash |
| 9 | +poetry add pybotx-smartapp-smart-logger |
| 10 | +``` |
| 11 | + |
| 12 | +2. Подключаем `pybotx-smart-logger` по инструкции из [README](https://github.com/ExpressApp/pybotx-smart-logger/blob/master/README.md) |
| 13 | + |
| 14 | +3. Подключаем `pybotx-smartapp-rpc` по инструкции из [README](https://github.com/ExpressApp/pybotx-smartapp-rpc/blob/master/README.md) |
| 15 | + |
| 16 | +4. Подключаем хендлер исключений к смартапу |
| 17 | + |
| 18 | +```python |
| 19 | +from botx_smartapp_smart_logger import smartapp_exception_handler |
| 20 | + |
| 21 | +smartapp = SmartAppRPC( |
| 22 | + routers=..., |
| 23 | + exception_handlers={Exception: smartapp_exception_handler} |
| 24 | +) |
| 25 | +``` |
| 26 | + |
| 27 | +5. Оборачиваем вызов `handle_smartapp_event` в контекстный менеджер: |
| 28 | + |
| 29 | +```python |
| 30 | +from botx_smart_logger import wrap_system_event |
| 31 | + |
| 32 | +@collector.smartapp_event |
| 33 | +async def handle_smartapp_event(event: SmartAppEvent, bot: Bot) -> None: |
| 34 | + with wrap_system_event(event, settings.DEBUG): |
| 35 | + await smartapp.handle_smartapp_event(event, bot) |
| 36 | +``` |
| 37 | + |
| 38 | +## Гдe применять |
| 39 | + |
| 40 | +Добавлять логи лучше везде, где информация из них поможет при диагностике ошибки. Например, здесь выводятся аргументы перед выполением деления. |
| 41 | + |
| 42 | +```python |
| 43 | +from pybotx_smart_logger import smart_log |
| 44 | + |
| 45 | +@rpc.method("divide") |
| 46 | +async def divide( |
| 47 | + smartapp: SmartApp, rpc_arguments: SumArgs |
| 48 | +) -> RPCResultResponse[int]: |
| 49 | + smart_log(f"RPC method `divide` called with args: {rpc_arguments}") |
| 50 | + return RPCResultResponse(result=rpc_arguments.a / rpc_arguments.b) |
| 51 | +``` |
0 commit comments