| 
									
										
										
										
											2024-10-09 12:15:09 -07:00
										 |  |  | import json | 
					
						
							|  |  |  | import logging | 
					
						
							| 
									
										
										
										
											2024-12-03 14:45:10 -08:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2024-10-09 12:15:09 -07:00
										 |  |  | from datetime import datetime | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-05 08:07:49 -08:00
										 |  |  | from pydantic import BaseModel | 
					
						
							| 
									
										
										
										
											2024-10-09 12:15:09 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FileLogHandler(logging.Handler): | 
					
						
							|  |  |  |     def __init__(self, filename: str) -> None: | 
					
						
							|  |  |  |         super().__init__() | 
					
						
							|  |  |  |         self.filename = filename | 
					
						
							|  |  |  |         self.file_handler = logging.FileHandler(filename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def emit(self, record: logging.LogRecord) -> None: | 
					
						
							|  |  |  |         ts = datetime.fromtimestamp(record.created).isoformat() | 
					
						
							| 
									
										
										
										
											2024-11-05 08:07:49 -08:00
										 |  |  |         if isinstance(record.msg, BaseModel): | 
					
						
							|  |  |  |             record.msg = json.dumps( | 
					
						
							| 
									
										
										
										
											2024-10-09 12:15:09 -07:00
										 |  |  |                 { | 
					
						
							|  |  |  |                     "timestamp": ts, | 
					
						
							| 
									
										
										
										
											2024-11-05 08:07:49 -08:00
										 |  |  |                     "message": record.msg.model_dump(), | 
					
						
							| 
									
										
										
										
											2024-10-09 12:15:09 -07:00
										 |  |  |                     "type": record.msg.__class__.__name__, | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-11-05 08:07:49 -08:00
										 |  |  |         self.file_handler.emit(record) | 
					
						
							| 
									
										
										
										
											2024-12-03 14:45:10 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ConsoleLogHandler(logging.Handler): | 
					
						
							|  |  |  |     def emit(self, record: logging.LogRecord) -> None: | 
					
						
							|  |  |  |         ts = datetime.fromtimestamp(record.created).isoformat() | 
					
						
							|  |  |  |         if isinstance(record.msg, BaseModel): | 
					
						
							|  |  |  |             record.msg = json.dumps( | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     "timestamp": ts, | 
					
						
							|  |  |  |                     "message": record.msg.model_dump_json(indent=2), | 
					
						
							|  |  |  |                     "type": record.msg.__class__.__name__, | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         sys.stdout.write(f"{record.msg}\n") |