Commit ee854daa authored by Michal Čihař's avatar Michal Čihař

Standard way of logging

The logs will now be prefixed with slug of current object, so that every
log can be linked to the object and it is identified in a standard way.
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 3fa64d72
......@@ -119,7 +119,31 @@ class URLMixin(object):
return self.reverse_url('unlock')
class PathMixin(object):
class LoggerMixin(object):
"""
Mixin with logging.
"""
@property
def log_prefix(self):
return 'default: '
def log_info(self, msg, *args):
return weblate.logger.info(
self.log_prefix + msg, *args
)
def log_warning(self, msg, *args):
return weblate.logger.warning(
self.log_prefix + msg, *args
)
def log_error(self, msg, *args):
return weblate.logger.error(
self.log_prefix + msg, *args
)
class PathMixin(LoggerMixin):
"""
Mixin for path manipulations.
"""
......@@ -152,12 +176,12 @@ class PathMixin(object):
# Invalidate cache
self._dir_path = None
new_path = self.get_path()
weblate.logger.info(
'Path changed from %s to %s', old_path, new_path
self.log_info(
'path changed from %s to %s', old_path, new_path
)
if os.path.exists(old_path) and not os.path.exists(new_path):
weblate.logger.info(
'Renaming "%s" to "%s"', old_path, new_path
self.log_info(
'tenaming "%s" to "%s"', old_path, new_path
)
os.rename(old_path, new_path)
......
......@@ -264,6 +264,10 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
self._linked_subproject = None
self._repository = None
@property
def log_prefix(self):
return '{0}/{1}: '.format(self.project.slug, self.slug)
def has_acl(self, user):
'''
Checks whether current user is allowed to access this
......@@ -462,12 +466,12 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return self.linked_subproject.update_remote_branch(validate)
# Update
weblate.logger.info('updating repo %s', self.__unicode__())
self.log_info('updating repository')
try:
self.repository.update_remote()
except RepositoryException as error:
error_text = str(error)
weblate.logger.error('Failed to update repository: %s', error_text)
self.log_error('failed to update repository: %s', error_text)
if validate:
if 'Host key verification failed' in error_text:
raise ValidationError(_(
......@@ -569,17 +573,11 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
# Do actual push
try:
weblate.logger.info(
'pushing to remote repo %s',
self.__unicode__()
)
self.log_info('pushing to remote repo')
self.repository.push(self.branch)
return True
except RepositoryException as error:
weblate.logger.warning(
'failed push on repo %s',
self.__unicode__()
)
self.log_error('failed to push on repo')
msg = 'Error:\n%s' % str(error)
mail_admins(
'failed push on repo %s' % self.__unicode__(),
......@@ -606,16 +604,10 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
# Do actual reset
with self.git_lock:
try:
weblate.logger.info(
'reseting to remote repo %s',
self.__unicode__()
)
self.log_info('reseting to remote repo')
self.repository.reset(self.branch)
except RepositoryException as error:
weblate.logger.warning(
'failed reset on repo %s',
self.__unicode__()
)
self.log_error('failed to reset on repo')
msg = 'Error:\n%s' % str(error)
mail_admins(
'failed reset on repo %s' % self.__unicode__(),
......@@ -684,10 +676,9 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
try:
# Try to merge it
method(self.branch)
weblate.logger.info(
'%s remote into repo %s',
self.log_info(
'%s remote into repo',
self.project.merge_style,
self.__unicode__()
)
return True
except Exception as error:
......@@ -697,10 +688,9 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
method(abort=True)
# Log error
weblate.logger.warning(
'failed %s on repo %s',
self.log_error(
'failed %s on repo',
self.project.merge_style,
self.__unicode__()
)
# Notify subscribers and admins
......@@ -736,14 +726,12 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
for pos, path in enumerate(matches):
code = self.get_lang_code(path)
if langs is not None and code not in langs:
weblate.logger.info('skipping %s', path)
self.log_info('skipping %s', path)
continue
weblate.logger.info(
'checking %s in %s/%s [%d/%d]',
self.log_info(
'checking %s [%d/%d]',
path,
self.project.slug,
self.slug,
pos + 1,
len(matches)
)
......@@ -756,7 +744,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
if langs is None:
todelete = self.translation_set.exclude(id__in=translations)
if todelete.exists():
weblate.logger.info(
self.log_info(
'removing stale translations: %s',
','.join([trans.language.code for trans in todelete])
)
......@@ -764,13 +752,13 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
# Process linked repos
for subproject in self.get_linked_childs():
weblate.logger.info(
self.log_info(
'updating linked project %s',
subproject
)
subproject.create_translations(force, langs, request=request)
weblate.logger.info('updating of %s completed', self)
self.log_info('updating completed')
def get_lang_code(self, path):
'''
......
......@@ -45,7 +45,7 @@ from weblate.trans.util import (
)
from weblate.trans.vcs import RepositoryException
from weblate.accounts.avatar import get_user_display
from weblate.trans.mixins import URLMixin, PercentMixin
from weblate.trans.mixins import URLMixin, PercentMixin, LoggerMixin
from weblate.trans.boolean_sum import BooleanSum
from weblate.accounts.models import notify_new_string
from weblate.trans.models.changes import Change
......@@ -114,7 +114,7 @@ class TranslationManager(models.Manager):
return tuple([translation_percent(value, total) for value in result])
class Translation(models.Model, URLMixin, PercentMixin):
class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
subproject = models.ForeignKey('SubProject')
language = models.ForeignKey(Language)
revision = models.CharField(max_length=100, default='', blank=True)
......@@ -166,6 +166,14 @@ class Translation(models.Model, URLMixin, PercentMixin):
super(Translation, self).__init__(*args, **kwargs)
self._store = None
@property
def log_prefix(self):
return '{0}/{1}/{2}: '.format(
self.subproject.project.slug,
self.subproject.slug,
self.language_code,
)
def has_acl(self, user):
'''
Checks whether current user is allowed to access this
......@@ -434,9 +442,8 @@ class Translation(models.Model, URLMixin, PercentMixin):
try:
self._store = self.load_store()
except Exception as exc:
weblate.logger.warning(
'failed parsing store %s: %s',
self.__unicode__(),
self.log_warning(
'failed parsing store: %s',
str(exc)
)
self.subproject.notify_merge_failure(
......@@ -516,11 +523,9 @@ class Translation(models.Model, URLMixin, PercentMixin):
else:
return
weblate.logger.info(
'processing %s in %s/%s, %s',
self.log_info(
'processing %s, %s',
self.filename,
self.subproject.project.slug,
self.subproject.slug,
reason,
)
......@@ -556,9 +561,8 @@ class Translation(models.Model, URLMixin, PercentMixin):
# Check for possible duplicate units
if newunit.id in created_units:
weblate.logger.error(
'Duplicate string to translate in %s: %s (%s)',
self,
self.log_error(
'duplicate string to translate: %s (%s)',
newunit,
repr(newunit.source)
)
......@@ -806,8 +810,8 @@ class Translation(models.Model, URLMixin, PercentMixin):
env=get_clean_env(),
)
except (OSError, subprocess.CalledProcessError) as err:
weblate.logger.error(
'Failed to run pre commit script %s: %s',
self.log_error(
'failed to run pre commit script %s: %s',
self.subproject.pre_commit_script,
err
)
......@@ -862,19 +866,17 @@ class Translation(models.Model, URLMixin, PercentMixin):
# Can we delay commit?
if not force_commit and appsettings.LAZY_COMMITS:
weblate.logger.info(
'Delaying commiting %s in %s as %s',
self.log_info(
'delaying commiting %s as %s',
self.filename,
self,
author
)
return False
# Do actual commit with git lock
weblate.logger.info(
'Commiting %s in %s as %s',
self.log_info(
'commiting %s as %s',
self.filename,
self,
author
)
with self.subproject.git_lock:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment