mylog, tasks, wxmp优化

This commit is contained in:
caoqianming 2023-03-02 16:57:23 +08:00
parent ce4febd9c6
commit 029459ce5e
3 changed files with 9 additions and 4 deletions

View File

@ -1,18 +1,22 @@
import logging
from functools import wraps
from apps.utils.tasks import send_mail_task
import traceback
myLogger = logging.getLogger('log')
def auto_log(name='', raise_exception=False):
def auto_log(name='', raise_exception=True, send_mail=False):
def decorate(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
real_func = func(*args, **kwargs)
return real_func
except Exception as e:
except Exception:
myLogger.error(name, exc_info=True)
if send_mail:
send_mail_task.delay(message=traceback.format_exc())
if raise_exception:
raise
return wrapper

View File

@ -11,7 +11,7 @@ myLogger = logging.getLogger('log')
@shared_task
def send_mail_task(**args):
from django.core.mail import send_mail
args['subject'] = args.get('subject', 'ehs_500')
args['subject'] = args.get('subject', '{}:{}_{}_500'.format(settings.SYS_NAME, settings.SYS_VERSION, settings.PROJECT_NAME))
args['from_email'] = args.get('from_email', settings.EMAIL_HOST_USER)
args['recipient_list'] = args.get('recipient_list', [settings.EMAIL_HOST_USER])
send_mail(**args)
@ -25,5 +25,5 @@ class CustomTask(Task):
def on_failure(self, exc, task_id, args, kwargs, einfo):
detail = '{0!r} failed: {1!r}'.format(task_id, exc)
myLogger.error(detail)
send_mail_task.delay(subject='ehs_task_error', message=detail)
send_mail_task.delay(subject='{}:{}_{}_task_error'.format(settings.SYS_NAME, settings.SYS_VERSION, settings.PROJECT_NAME), message=detail)
return super().on_failure(exc, task_id, args, kwargs, einfo)

View File

@ -22,6 +22,7 @@ class WxmpClient:
def __init__(self, app_id=settings.WXMP_APPID,
app_secret=settings.WXMP_APPSECRET) -> None:
self.app_id, self.app_secret = None, None
if settings.WXMP_ENABLED:
self.app_id = app_id
self.app_secret = app_secret