Skip to content

Commit

Permalink
adding options for timedfh
Browse files Browse the repository at this point in the history
  • Loading branch information
havok2063 committed Nov 9, 2023
1 parent 67a19c7 commit 7a1b8b5
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/sdsstools/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ def start_file_logger(
mode: str = "a",
rotating: bool = True,
rollover: bool = False,
when: str = 'midnight',
utc: bool = True,
at_time: Union[str, datetime.time] = None,
):
"""Start file logging.
Expand All @@ -336,6 +339,14 @@ def start_file_logger(
rollover
If `True` and ``rotating=True` and the log file already exists, does a
rollover before starting to log.
when
The type of time interval. Allowed values are those from
`TimedRotatingFileHandler`.
utc
If `True`, times in UTC will be used; otherwise local time is used.
at_time
The time of day when rollover occurs, when rollover is set to occur
at “midnight” or “on a particular weekday”.
"""

log_file_path = os.path.realpath(os.path.expanduser(path))
Expand All @@ -352,15 +363,19 @@ def start_file_logger(
dst = str(log_file_path) + "." + now.strftime(SUFFIX)
shutil.move(str(log_file_path), dst)

# convert at_time to a datetime.time
if at_time and isinstance(at_time, str):
at_time = datetime.time.fromisoformat(at_time)

if rotating:
self.fh = TimedRotatingFileHandler(
str(log_file_path), when="midnight", utc=True
str(log_file_path), when=when, utc=utc, atTime=at_time
)
self.fh.suffix = SUFFIX # type: ignore
else:
self.fh = logging.FileHandler(str(log_file_path), mode=mode)

except (IOError, OSError) as ee:
except (IOError, OSError, ValueError) as ee:
warnings.warn(
"log file {0!r} could not be opened for "
"writing: {1}".format(log_file_path, ee),
Expand Down

0 comments on commit 7a1b8b5

Please sign in to comment.