generated from ImperialCollegeLondon/pip-tools-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
151 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ dependencies = [ | |
"geopandas", | ||
"geopy", | ||
"GitPython", | ||
"loguru", | ||
"matplotlib", | ||
"netcdf4", | ||
"networkx", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Created on 2024-03-04. | ||
@author: Barney | ||
""" | ||
import os | ||
import sys | ||
|
||
import loguru | ||
|
||
|
||
def dynamic_filter(record): | ||
"""A dynamic filter.""" | ||
return os.getenv("SWMMANYWHERE_VERBOSE", "false").lower() == "true" | ||
|
||
def get_logger() -> loguru.logger: | ||
"""Get a logger.""" | ||
logger = loguru.logger | ||
logger.configure( | ||
handlers=[ | ||
{ | ||
"sink": sys.stdout, | ||
"filter" : dynamic_filter, | ||
"colorize": True, | ||
"format": " | ".join( | ||
[ | ||
"<cyan>{time:YYYY/MM/DD HH:mm:ss}</>", | ||
"{message}", | ||
] | ||
), | ||
} | ||
] | ||
) | ||
return logger | ||
|
||
# Get the logger | ||
logger = get_logger() | ||
|
||
# Add a test_logger method to the logger | ||
logger.test_logger = lambda : logger.info("This is a test message.") | ||
|
||
# Store the original add method | ||
original_add = logger.add | ||
|
||
# Define a new function that wraps the original add method | ||
def new_add(sink, **kwargs): | ||
"""A new add method to wrap existing one but with the filter.""" | ||
# Include the dynamic filter in the kwargs if not already specified | ||
if 'filter' not in kwargs: | ||
kwargs['filter'] = dynamic_filter | ||
# Call the original add method with the updated kwargs | ||
return original_add(sink, **kwargs) | ||
|
||
# Replace the logger's add method with new_add | ||
logger.add = new_add |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Created on 2024-01-26. | ||
@author: Barney | ||
""" | ||
import os | ||
from pathlib import Path | ||
from tempfile import NamedTemporaryFile | ||
|
||
from swmmanywhere.logging import logger | ||
|
||
|
||
def test_logger(): | ||
"""Test logger.""" | ||
os.environ["SWMMANYWHERE_VERBOSE"] = "true" | ||
assert logger is not None | ||
logger.test_logger() | ||
logger.debug("This is a debug message.") | ||
logger.info("This is an info message.") | ||
logger.warning("This is a warning message.") | ||
logger.error("This is an error message.") | ||
logger.critical("This is a critical message.") | ||
with NamedTemporaryFile(suffix='.log', | ||
mode = 'w+b', | ||
delete=False) as temp_file: | ||
fid = Path(temp_file.name) | ||
logger.add(fid) | ||
logger.test_logger() | ||
assert temp_file.read() != b"" | ||
logger.remove() | ||
fid.unlink() | ||
|
||
def test_logger_disable(): | ||
"""Test the disable function.""" | ||
with NamedTemporaryFile(suffix='.log', | ||
mode = 'w+b', | ||
delete=False) as temp_file: | ||
fid = Path(temp_file.name) | ||
os.environ["SWMMANYWHERE_VERBOSE"] = "false" | ||
logger.add(fid) | ||
logger.test_logger() | ||
assert temp_file.read() == b"" | ||
logger.remove() | ||
fid.unlink() | ||
|
||
def test_logger_reimport(): | ||
"""Reimport logger to check that changes from disable are persistent.""" | ||
from swmmanywhere.logging import logger | ||
with NamedTemporaryFile(suffix='.log', | ||
mode = 'w+b', | ||
delete=False) as temp_file: | ||
fid = Path(temp_file.name) | ||
logger.add(fid) | ||
logger.test_logger() | ||
assert temp_file.read() == b"" | ||
logger.remove() | ||
fid.unlink() | ||
|
||
def test_logger_again(): | ||
"""Test the logger after these changes to make sure still works.""" | ||
os.environ["SWMMANYWHERE_VERBOSE"] = "true" | ||
with NamedTemporaryFile(suffix='.log', | ||
mode = 'w+b', | ||
delete=False) as temp_file: | ||
fid = Path(temp_file.name) | ||
logger.add(fid) | ||
logger.test_logger() | ||
assert temp_file.read() != b"" | ||
logger.remove() | ||
fid.unlink() |