Skip to content

Commit

Permalink
Changes the creation of log files. Amends the uncertainty wizard test…
Browse files Browse the repository at this point in the history
… to correct for use of logging over the standard console. Adds the .logs to .gitignore.
  • Loading branch information
Zoophobus committed Aug 16, 2023
1 parent e02a747 commit 9fcd1f7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ activity_browser/ABSettings.json
*.bak
/.idea
/.vscode
.coverage
.coverage
.logs
test/.logs
33 changes: 27 additions & 6 deletions activity_browser/logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from PySide2.QtCore import Slot
import logging
import os
import os, time
import random, string


from .signals import signals
Expand All @@ -12,22 +13,42 @@ class Logger(logging.Logger):
Returns the logging device
"""
file_handler = None
def __init__(self, name: str):
super().__init__(name)
self.setLevel(logging.INFO)
file_path = os.getcwd() + name + '.log'

self.stream_handler = logging.StreamHandler()
self.stream_handler.setLevel(logging.INFO)
self.file_handler = logging.FileHandler(file_path)
self.file_handler.setLevel(logging.WARNING)
# Threads have their own Logger, but these refer to the sessions log file
if Logger.file_handler is None:
dir_path = os.getcwd() + "/.logs"
os.makedirs(dir_path, exist_ok=True)
Logger.cleanDirectory(dir_path)
name = name + "_" + Logger.uniqueString(8) + '.log'
file_path = dir_path + "/" + name
Logger.file_handler = logging.FileHandler(file_path)
Logger.file_handler.setLevel(logging.WARNING)

self.log_format = logging.Formatter("%(module)s - %(levelname)s - %(asctime)s - %(message)s")
self.stream_handler.setFormatter(self.log_format)
self.file_handler.setFormatter(self.log_format)
Logger.file_handler.setFormatter(self.log_format)

self.addHandler(self.stream_handler)
self.addHandler(self.file_handler)
self.addHandler(Logger.file_handler)

@staticmethod
def uniqueString(n: int) -> str:
"""Returns a random string of length n, to avoid issues with non-unique log files"""
return ''.join(random.choice(string.ascii_letters) for i in range(n))

@staticmethod
def cleanDirectory(dirpath: str) -> None:
time_limit = time.time() - 24*3600*7
for file in os.listdir(dirpath):
filepath = dirpath + '/' + file
if os.stat(filepath).st_mtime < time_limit:
os.remove(filepath)

@classmethod
def getLogger(cls, name: str = None):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_uncertainty_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_wizard_fail(qtbot):


@pytest.mark.skipif(sys.platform=='darwin', reason="tests segfaults on osx")
def test_uncertainty_wizard_simple(qtbot, bw2test, capsys):
def test_uncertainty_wizard_simple(qtbot, bw2test, caplog):
"""Use extremely simple text to open the wizard and go to all the pages."""
param = ProjectParameter.create(name="test1", amount=3)
wizard = UncertaintyWizard(param, None)
Expand All @@ -42,7 +42,7 @@ def test_uncertainty_wizard_simple(qtbot, bw2test, capsys):
wizard.type.pedigree.click()

# Pedigree is empty, so complaint is issued.
captured = capsys.readouterr()
captured = caplog.text()
assert "Could not extract pedigree data" in captured.out

# Now go back for giggles.
Expand Down

0 comments on commit 9fcd1f7

Please sign in to comment.