Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# This is the local handler
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(daiquiri.formatter.ColorExtrasFormatter(fmt=FORMAT_STRING, keywords=[]))
logger.logger.addHandler(handler)
# This is the Sentry handler
if "data_refinery_api" in name:
raven_dsn = get_env_variable_gracefully("RAVEN_DSN_API", False)
else:
raven_dsn = get_env_variable_gracefully("RAVEN_DSN", False)
if raven_dsn:
from raven.contrib.django.handlers import SentryHandler
handler = SentryHandler()
handler.setFormatter(
daiquiri.formatter.ColorExtrasFormatter(fmt=FORMAT_STRING, keywords=[])
)
handler.setLevel(logging.WARNING)
logger.logger.addHandler(handler)
return logger
def utcnow():
return datetime.datetime.now(tz=datetime.timezone.utc)
def unicode_truncate(s, length, encoding="utf-8"):
"""Truncate a string to length in bytes.
:param s: The string to truncate.
:param length: The length in number of bytes — not characters."""
return s.encode(encoding)[:length].decode(encoding, errors="ignore")
class CustomFormatter(
daiquiri.formatter.ColorExtrasFormatter, celery.app.log.TaskFormatter
):
pass
CELERY_EXTRAS_FORMAT = (
"%(asctime)s [%(process)d] %(color)s%(levelname)-8.8s "
"[%(task_id)s] "
"%(name)s%(extras)s: %(message)s%(color_stop)s"
)
def GithubPullRequestLog(self):
return daiquiri.getLogger(
__name__,
gh_owner=self.base.user.login,
gh_repo=self.base.repo.name,
s = super(ExtrasFormatter, self).format(record)
self.remove_extras(record)
return s
class ColorExtrasFormatter(ColorFormatter, ExtrasFormatter):
"""Combines functionality of ColorFormatter and ExtrasFormatter."""
def format(self, record):
self.add_color(record)
s = ExtrasFormatter.format(self, record)
self.remove_color(record)
return s
TEXT_FORMATTER = ColorExtrasFormatter(fmt=DEFAULT_EXTRAS_FORMAT)
if jsonlogger:
JSON_FORMATTER = jsonlogger.JsonFormatter()