Initial commit
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| .idea/ | ||||
| venv/ | ||||
							
								
								
									
										7
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| # logag-python-handler | ||||
|  | ||||
| ## using | ||||
|  | ||||
| ``` | ||||
| git+https://gitlab.com/logag/logag-python-handler.git | ||||
| ``` | ||||
							
								
								
									
										67
									
								
								logag_handler/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								logag_handler/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,67 @@ | ||||
| import json | ||||
| import logging | ||||
| import socket | ||||
| import sys | ||||
| import traceback | ||||
|  | ||||
| from xid import Xid | ||||
|  | ||||
| LEVELS = { | ||||
|     "DEBUG": 0, | ||||
|     "INFO": 1, | ||||
|     "WARN": 2, | ||||
|     "WARNING": 2, | ||||
|     "ERROR": 3 | ||||
| } | ||||
|  | ||||
|  | ||||
| def new_xid(): | ||||
|     return Xid().string() | ||||
|  | ||||
|  | ||||
| class LogagHandler(logging.Handler): | ||||
|     def __init__(self, service_name: str, pod_name: str, ip: str, port: int): | ||||
|         super().__init__() | ||||
|         self.service_name = service_name | ||||
|         self.pod_name = pod_name | ||||
|         self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||||
|         self.connection = (ip, port) | ||||
|  | ||||
|         self.backup_handler = logging.StreamHandler(sys.stdout) | ||||
|         self.backup_handler.setFormatter( | ||||
|             logging.Formatter(u'[%(levelname)-8s][%(asctime)s][%(filename)s:%(lineno)d] %(message)s')) | ||||
|         self.backup_handler.setLevel(logging.INFO) | ||||
|  | ||||
|     def __send_to_udp(self, data: dict): | ||||
|         data = json.dumps(data).encode('utf-8') | ||||
|         self.sock.sendto(data, self.connection) | ||||
|  | ||||
|     def emit(self, record): | ||||
|         trace = None | ||||
|         if record.exc_info: | ||||
|             if isinstance(record.exc_info, tuple) and len(record.exc_info) == 3: | ||||
|                 trace = ''.join(traceback.format_exception(*record.exc_info)) | ||||
|  | ||||
|             record.exc_info = None | ||||
|  | ||||
|         # get xid | ||||
|         xid = getattr(record, "xid", None) | ||||
|         if xid is None or len(xid) != 20: | ||||
|             xid = new_xid() | ||||
|  | ||||
|         msg = self.format(record) | ||||
|         try: | ||||
|             json_ = { | ||||
|                 "timestamp": int(record.created * 1000), | ||||
|                 "svc": self.service_name, | ||||
|                 "pod": self.pod_name, | ||||
|                 "xid": xid, | ||||
|                 "lvl": LEVELS[record.levelname], | ||||
|                 "msg": msg, | ||||
|                 "trace": trace, | ||||
|                 "requestId": getattr(record, "request_id", None), | ||||
|             } | ||||
|  | ||||
|             self.__send_to_udp(json_) | ||||
|         except Exception as e: | ||||
|             self.backup_handler.emit(record) | ||||
							
								
								
									
										1
									
								
								requirements.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								requirements.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| xid==1.0.4 | ||||
							
								
								
									
										15
									
								
								setup.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								setup.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| from setuptools import setup | ||||
|  | ||||
| setup( | ||||
|     name='logag-python-handler', | ||||
|     version='0.1.0', | ||||
|     packages=['logag_handler'], | ||||
|     url='https://gitlab.com/logag/logag-python-handler', | ||||
|     license='MIT', | ||||
|     author='jar3b', | ||||
|     author_email='jar3b@outlook.com', | ||||
|     description='Python handler for logag', | ||||
|     install_requires=[ | ||||
|         'xid==1.0.4', | ||||
|     ] | ||||
| ) | ||||
		Reference in New Issue
	
	Block a user