Skip to content

putting formatter creation logic in a class #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions supervisor/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,34 +178,37 @@ def format(self, record):
log_record = self.process_log_record(log_record)
return self.serialize_log_record(log_record)

def _formatter_factory(name=None, fmt=None, style=None):
if name is None:
name = 'plaintext'

if fmt is None:
fmt = '%(asctime)s %(levelname)s %(message)s'

if style is None:
style = None
# determine the style based on the logging format.
for style in _STYLES:
_style = _STYLES[style][0](fmt)
try:
_style.validate()
break # exit the loop if fmt passes style validation
except ValueError:
style = None

if style is None:
raise ValueError('Invalid logging format: %s' % fmt)

if name == 'plaintext':
return PlainTextFormatter(fmt, style=style)
elif name == 'json':
return CustomJsonFormatter(fmt, style=style)
else:
raise ValueError('Invalid formatter name: %s' % name)

class FormatterFacotry:
def get_formatter(self, name=None, fmt=None, style=None):
if name is None:
name = 'plaintext'

if fmt is None:
fmt = '%(asctime)s %(levelname)s %(message)s'

if style is None:
style = None
# determine the style based on the logging format.
for style in _STYLES:
_style = _STYLES[style][0](fmt)
try:
_style.validate()
break # exit the loop if fmt passes style validation
except ValueError:
style = None

if style is None:
raise ValueError('Invalid logging format: %s' % fmt)

if name == 'plaintext':
return PlainTextFormatter(fmt, style=style)
elif name == 'json':
return CustomJsonFormatter(fmt, style=style)
else:
raise ValueError('Invalid formatter name: %s' % name)

_formatter_factory = FormatterFacotry().get_formatter
BASIC_FORMATTER = _formatter_factory(name='plaintext', fmt=BASIC_FORMAT)

class Handler:
Expand Down