Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
havok2063 committed Nov 12, 2023
1 parent f771762 commit dfec626
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/sdsstools/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,26 @@ def format(self, record):

return logging.Formatter.format(self, record_cp)


class CustomJsonFormatter(jsonlogger.JsonFormatter):
""" Custom `jsonlogger.JsonFormatter` for the JSON file handler """
"""Custom `jsonlogger.JsonFormatter` for the JSON file handler"""

def add_fields(self, log_record, record, message_dict):
""" Add custom fields to the JSON body """
"""Add custom fields to the JSON body"""
super().add_fields(log_record, record, message_dict)
if not log_record.get('timestamp'):
now = datetime.datetime.fromtimestamp(record.created).strftime('%Y-%m-%dT%H:%M:%S.%fZ') # noqa: E501
if not log_record.get("timestamp"):
now = datetime.datetime.fromtimestamp(record.created).strftime(
"%Y-%m-%dT%H:%M:%S.%fZ"
) # noqa: E501
log_record["timestamp"] = now
log_record["type"] = "log"
log_record["level"] = record.levelname
log_record.update(record.__dict__)
if record.exc_info:
log_record['error'] = {'type': record.exc_info[0].__name__,
'trace': message_dict['exc_info']}
log_record["error"] = {

Check warning on line 163 in src/sdsstools/logger.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/logger.py#L163

Added line #L163 was not covered by tests
"type": record.exc_info[0].__name__,
"trace": message_dict["exc_info"],
}


class CustomRichHandler(RichHandler):
Expand Down Expand Up @@ -336,11 +341,16 @@ def save_log(self, path: str):
assert self.json_log_filename, "JSON file logger not running."
shutil.copyfile(self.json_log_filename, os.path.expanduser(path))

Check warning on line 342 in src/sdsstools/logger.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/logger.py#L341-L342

Added lines #L341 - L342 were not covered by tests

def _set_file_handler(self, filepath: str, suffix: str, rotating: bool = True,
mode: str = 'a',
when: str = 'midnight',
utc: bool = True,
at_time: Union[str, datetime.time] = None):
def _set_file_handler(
self,
filepath: str,
suffix: str,
rotating: bool = True,
mode: str = "a",
when: str = "midnight",
utc: bool = True,
at_time: Union[str, datetime.time] = None,
):
if rotating:
fh = TimedRotatingFileHandler(
str(filepath), when=when, utc=utc, atTime=at_time
Expand Down Expand Up @@ -400,10 +410,10 @@ def start_file_logger(

# set the JSON file path name
suffix = pathlib.Path(log_file_path).suffix
if as_json and suffix != '.json':
log_file_path = log_file_path.replace(suffix, '.json')
if as_json and suffix != ".json":
log_file_path = log_file_path.replace(suffix, ".json")
elif with_json:
json_log_file_path = log_file_path.replace(suffix, '.json')
json_log_file_path = log_file_path.replace(suffix, ".json")

try:
if not os.path.exists(logdir):
Expand All @@ -419,14 +429,26 @@ def start_file_logger(
at_time = datetime.time.fromisoformat(at_time)

# get the file handler
self.fh = self._set_file_handler(log_file_path, SUFFIX, mode=mode,
rotating=rotating, when=when, utc=utc,
at_time=at_time)
self.fh = self._set_file_handler(
log_file_path,
SUFFIX,
mode=mode,
rotating=rotating,
when=when,
utc=utc,
at_time=at_time,
)

if with_json:
self.jh = self._set_file_handler(json_log_file_path, SUFFIX, mode=mode,
rotating=rotating, when=when, utc=utc,
at_time=at_time)
self.jh = self._set_file_handler(
json_log_file_path,
SUFFIX,
mode=mode,
rotating=rotating,
when=when,
utc=utc,
at_time=at_time,
)

except (IOError, OSError, ValueError) as ee:
warnings.warn(
Expand Down Expand Up @@ -456,7 +478,6 @@ def start_file_logger(
if self.warnings_logger:
self.warnings_logger.addHandler(self.jh)


def handle(self, record):
"""Handles a record but first stores it."""

Expand Down

0 comments on commit dfec626

Please sign in to comment.