PracticeDev/study_python/tcp_relay-master/logger/__init__.py

32 lines
802 B
Python
Raw Permalink Normal View History

2022-12-20 17:31:11 +08:00
# -*- coding: utf-8 -*-
__author__ = 'Traphix'
import time
import logging
from .database import db
from .database.models import MRouterLogTable
class MRDatabaseHandler(logging.Handler):
def __init__(self, module_name, level=logging.NOTSET):
logging.Handler.__init__(self, level)
self.module_name = module_name
def emit(self, record):
try:
db.session.add(
MRouterLogTable(
module_name=self.module_name,
time=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(record.created)),
level=record.levelname,
content=record.getMessage()
)
)
db.session.commit()
except Exception:
self.handleError(record)