Created
January 30, 2016 14:59
-
-
Save yatt/a9d1d0b14bcd74210d74 to your computer and use it in GitHub Desktop.
simple error logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python2.7 | |
# coding: utf-8 | |
import os | |
from peewee import * | |
DATABASE_PATH = os.path.join(os.path.dirname(__file__), 'database.sqlite3') | |
db = SqliteDatabase(DATABASE_PATH) | |
class LogLevel(object): | |
DEBUG = 'debug' | |
INFO = 'info' | |
WARNING = 'warning' | |
CRITICAL = 'critical' | |
class EventLog(Model): | |
event_id = PrimaryKeyField() | |
log_level = CharField() | |
event_dt = DateTimeField() | |
module_name = CharField() | |
function_name = CharField() | |
message = TextField() | |
cmn_create_pg_nm = TextField(null=True) | |
cmn_create_dt = DateTimeField(null=True) | |
cmn_update_pg_nm = TextField(null=True) | |
cmn_update_dt = DateTimeField(null=True) | |
cmn_version = CharField(null=True) | |
class Meta: | |
database = db | |
db_table = 't_event_log' | |
def error_trap(message_success): | |
""" | |
処理完了時、例外発生時にDBにログを残すデコレータ | |
:param message_success: | |
:return: | |
""" | |
def _error_trap(fn): | |
def __error_trap(*args, **kwargs): | |
try: | |
fn(*args, **kwargs) | |
log(LogLevel.INFO, fn.__module__, fn.__name__, message_success) | |
except Exception, e: | |
log(LogLevel.CRITICAL, fn.__module__, fn.__name__, unicode(e.__class__) + u': ' + e.message) | |
raise e | |
return __error_trap | |
return _error_trap | |
@error_trap(u'処理を完了しました') | |
def add_two_integers(x, y): | |
assert x > 0, 'argument `x` must be positive' | |
assert y > 0, 'argument `y` must be positive' | |
return x + y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment