1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-16 15:19:06 +00:00

format log (#7441)

* format log

* update

* optimize

---------

Co-authored-by: 孙永强 <11704063+s-yongqiang@user.noreply.gitee.com>
This commit is contained in:
awu0403
2025-02-08 18:04:53 +08:00
committed by GitHub
parent cb3af6e66a
commit c81d79b8bc
3 changed files with 74 additions and 23 deletions

View File

@@ -24,8 +24,7 @@ from seahub.utils import (get_site_name, get_user_activities_by_timestamp,
from seahub.utils.timeutils import utc_to_local
# Get an instance of a logger
logger = logging.getLogger(__name__)
logger = logging.getLogger('file_updates_sender')
# Utility Functions
def td(con):
@@ -65,10 +64,11 @@ class Command(BaseCommand):
label = "notifications_send_file_updates"
def handle(self, *args, **options):
logger.debug('Start sending file updates emails...')
logger.info('Start sending file updates emails...')
self.stdout.write('[%s] Start sending file updates emails...' % str(datetime.now()))
self.do_action()
logger.debug('Finish sending file updates emails.\n')
logger.info('Finish sending file updates emails.\n')
self.stdout.write('[%s] Finish sending file updates emails.\n\n' % str(datetime.now()))
def get_avatar(self, username):
@@ -192,7 +192,7 @@ class Command(BaseCommand):
emails.append(ele.email)
except Exception as e:
logger.error(e)
self.stderr.write('[%s]: %s' % (str(datetime.now()), e))
self.stderr.write('[%s] [INFO]: %s' % (str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')), e))
continue
user_last_emailed_time_dict = {}
@@ -204,7 +204,7 @@ class Command(BaseCommand):
ele.option_val, "%Y-%m-%d %H:%M:%S")
except Exception as e:
logger.error(e)
self.stderr.write('[%s]: %s' % (str(datetime.now()), e))
self.stderr.write('[%s] [ERROR]: %s' % (str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')), e))
continue
for (username, interval_val) in user_file_updates_email_intervals:
@@ -214,10 +214,11 @@ class Command(BaseCommand):
# get and active user language
user_language = self.get_user_language(username)
translation.activate(user_language)
logger.debug('Set language code to %s for user: %s' % (
logger.info('Set language code to %s for user: %s' % (
user_language, username))
self.stdout.write('[%s] Set language code to %s for user: %s' % (
str(datetime.now()), user_language, username))
self.stdout.write('[%s] [INFO] Set language code to %s for user: %s' % (
str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')), user_language, username))
# get last_emailed_time if any, defaults to today 00:00:00
last_emailed_time = user_last_emailed_time_dict.get(username, None)
@@ -255,13 +256,14 @@ class Command(BaseCommand):
ele.op_user_link = a_tag(email2nickname(ele.op_user),
user_info_url(ele.op_user))
ele.operation, ele.op_details = self.format_file_operation(ele)
raise
except Exception as e:
logger.error('Failed to format mail content for user: %s' %
username)
logger.error(e, exc_info=True)
self.stderr.write('[%s] Failed to format mail content for user: %s' %
(str(datetime.now()), username))
self.stderr.write('[%s]: %s' % (str(datetime.now()), e))
self.stderr.write('[%s] [ERROR] Failed to format mail content for user: %s' %
(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')), username))
self.stderr.write('[%s] [ERROR]: %s' % (str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')), e))
continue
nickname = email2nickname(username)
@@ -279,13 +281,13 @@ class Command(BaseCommand):
None, [contact_email])
# set new last_emailed_time
UserOptions.objects.set_file_updates_last_emailed_time(username, now)
self.stdout.write('[%s] Successful to send email to %s' %
(str(datetime.now()), contact_email))
self.stdout.write('[%s] [INFO] Successful to send email to %s' %
(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')), contact_email))
except Exception as e:
logger.error('Failed to send email to %s, error detail: %s' %
(contact_email, e))
self.stderr.write('[%s] Failed to send email to %s, error '
'detail: %s' % (str(datetime.now()), contact_email, e))
self.stderr.write('[%s] [ERROR] Failed to send email to %s, error '
'detail: %s' % (str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')), contact_email, e))
finally:
# reset lang
translation.activate(cur_language)

View File

@@ -31,8 +31,7 @@ from seahub.notifications.utils import gen_sdoc_smart_link
from seahub.utils.auth import VIRTUAL_ID_EMAIL_DOMAIN
# Get an instance of a logger
logger = logging.getLogger(__name__)
logger = logging.getLogger('seahub_email_sender')
class Command(BaseCommand):
help = 'Send Email notifications to user if he/she has an unread notices every period of seconds .'
@@ -377,10 +376,10 @@ class Command(BaseCommand):
# get and active user language
user_language = self.get_user_language(to_user)
translation.activate(user_language)
logger.debug('Set language code to %s for user: %s' % (
logger.info('Set language code to %s for user: %s' % (
user_language, to_user))
self.stdout.write('[%s] Set language code to %s for user: %s' % (
str(datetime.datetime.now()), user_language, to_user))
self.stdout.write('[%s] [INFO] Set language code to %s for user: %s' % (
str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')), user_language, to_user))
# format mail content and send
notices = []
@@ -465,10 +464,10 @@ class Command(BaseCommand):
# set new last_emailed_time
UserOptions.objects.set_collaborate_last_emailed_time(to_user, now)
logger.info('Successfully sent email to %s' % contact_email)
self.stdout.write('[%s] Successfully sent email to %s' % (str(datetime.datetime.now()), contact_email))
self.stdout.write('[%s] [INFO] Successfully sent email to %s' % (str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')), contact_email))
except Exception as e:
logger.error('Failed to send email to %s, error detail: %s' % (contact_email, e))
self.stderr.write('[%s] Failed to send email to %s, error detail: %s' % (str(datetime.datetime.now()), contact_email, e))
self.stderr.write('[%s] [ERROR] Failed to send email to %s, error detail: %s' % (str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')), contact_email, e))
# restore current language
translation.activate(cur_language)

View File

@@ -694,6 +694,20 @@ if seafile_log_to_stdout:
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
'file_updates_sender_handler': {
'level': 'INFO',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
'formatter': 'standard',
'stream': sys.stdout,
},
'seahub_email_sender_handler': {
'level': 'INFO',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
'formatter': 'standard',
'stream': sys.stdout,
}
},
'loggers': {
@@ -717,6 +731,16 @@ if seafile_log_to_stdout:
'level': 'INFO',
'propagate': False
},
'file_updates_sender': {
'handlers': ['file_updates_sender_handler', ],
'level': 'INFO',
'propagate': False
},
'seahub_email_sender': {
'handlers': ['seahub_email_sender_handler', ],
'level': 'INFO',
'propagate': False
}
}
}
else:
@@ -769,6 +793,22 @@ else:
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
'file_updates_sender_handler': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(LOG_DIR, 'file_updates_sender.log'),
'maxBytes': 1024*1024*100, # 100 MB
'backupCount': 5,
'formatter': 'standard',
},
'seahub_email_sender_handler': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(LOG_DIR, 'seahub_email_sender.log'),
'maxBytes': 1024*1024*100, # 100 MB
'backupCount': 5,
'formatter': 'standard',
}
},
'loggers': {
@@ -792,6 +832,16 @@ else:
'level': 'INFO',
'propagate': False
},
'file_updates_sender': {
'handlers': ['file_updates_sender_handler', ],
'level': 'INFO',
'propagate': False
},
'seahub_email_sender': {
'handlers': ['seahub_email_sender_handler', ],
'level': 'INFO',
'propagate': False
}
}
}