32 lines
802 B
Python
32 lines
802 B
Python
|
# -*- 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)
|