refactored logging with json_logs func
This commit is contained in:
		
							
								
								
									
										22
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								main.py
									
									
									
									
									
								
							| @@ -1,8 +1,30 @@ | ||||
| import asyncio | ||||
| import json | ||||
| from typing import Any | ||||
| from multiprocessing import freeze_support | ||||
|  | ||||
| from src.core.master_service import MasterService | ||||
|  | ||||
| from loguru import logger | ||||
|  | ||||
|  | ||||
| def json_logs(message: Any) -> None: | ||||
|     record = message.record | ||||
|     data = { | ||||
|         "timestamp": record["time"].strftime("%d.%m.%y %H.%M.%S %Z%z"), | ||||
|         "level": record["level"].name, | ||||
|         "message": record["message"], | ||||
|         "path": record["file"].path, | ||||
|         "function": record["function"], | ||||
|         "line": record["line"], | ||||
|     } | ||||
|     print(json.dumps(data)) | ||||
|  | ||||
|  | ||||
| logger.remove(0) | ||||
| logger.add(json_logs) | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     freeze_support() | ||||
|     loop = asyncio.new_event_loop() | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| import asyncio | ||||
| import json | ||||
|  | ||||
| from loguru import logger | ||||
| from playwright.async_api import async_playwright | ||||
| from playwright.async_api import Playwright | ||||
| from aio_pika import Message, connect, DeliveryMode | ||||
| @@ -38,12 +39,13 @@ async def run(playwright: Playwright): | ||||
|                 routing_key='hello', | ||||
|             ) | ||||
|  | ||||
|             print(f" [x] Sent '{body}'") | ||||
|             logger.info(f" [x] Sent '{body}'") | ||||
|             await page.keyboard.press("ArrowDown") | ||||
|  | ||||
|             while title == await page.title(): | ||||
|                 await page.title() | ||||
|  | ||||
|  | ||||
| async def main(): | ||||
|     async with async_playwright() as playwright: | ||||
|         await run(playwright) | ||||
|   | ||||
| @@ -4,12 +4,13 @@ from functools import partial | ||||
|  | ||||
| from aio_pika import connect, Message, DeliveryMode | ||||
| from aio_pika.abc import AbstractIncomingMessage | ||||
| from loguru import logger | ||||
|  | ||||
|  | ||||
| async def on_message(message: AbstractIncomingMessage, queue) -> None: | ||||
|     async with message.process(): | ||||
|         await queue.put(json.loads(message.body)) | ||||
|         print(f"     Message body is: {message.body!r}") | ||||
|         logger.info(f"     Message body is: {message.body!r}") | ||||
|  | ||||
|  | ||||
| async def get_messages(inner_queue) -> None: | ||||
| @@ -23,8 +24,7 @@ async def get_messages(inner_queue) -> None: | ||||
|         ) | ||||
|  | ||||
|         await queue.consume(partial(on_message, queue=inner_queue)) | ||||
|  | ||||
|         print(" [*] Waiting for messages. To exit press CTRL+C") | ||||
|         logger.info("[*] Waiting for messages. To exit press CTRL+C") | ||||
|         await asyncio.Future() | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 nikili0n
					nikili0n