Skip to content

Commit

Permalink
Fixes #92
Browse files Browse the repository at this point in the history
- Make logfile path configurable
- Timestamp log
- Gracefully bail if logfile cannot be written
  • Loading branch information
bennahugo committed Mar 28, 2024
1 parent adc5c78 commit 3aff237
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions tricolour/apps/tricolour/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from tricolour.window_statistics import (window_stats,
combine_window_stats,
summarise_stats)
from datetime import datetime

##############################################################
# Initialize Post Mortem debugger
Expand All @@ -61,18 +62,30 @@ def create_logger():
log = logging.getLogger("tricolour")
cfmt = logging.Formatter(u'%(name)s - %(asctime)s '
'%(levelname)s - %(message)s')
log.setLevel(logging.DEBUG)
filehandler = logging.FileHandler("tricolour.log")
filehandler.setFormatter(cfmt)
log.addHandler(filehandler)
log.setLevel(logging.INFO)

console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(cfmt)

log.addHandler(console)

# add an optional file handler
logger_path = os.environ.get("TRICOLOUR_LOGPATH", os.getcwd())
nowT = int(np.ceil(datetime.timestamp(datetime.now())))
logfile = os.path.join(logger_path,
f"tricolour.{nowT}.log")
try:
with open(logfile, "w") as f:
f.write("")
filehandler = logging.FileHandler(logfile)
filehandler.setFormatter(cfmt)
log.addHandler(filehandler)
if logger_path != os.getcwd():
log.info(f"A copy of this log is available at {logfile}")
except PermissionError:
log.warning(f"Failed to initialize logfile for this run. "
f"Check your permissions and available space on "
f"'{logger_path}'. Proceeding without writing "
f"a logfile.")
return log


Expand Down

0 comments on commit 3aff237

Please sign in to comment.