Last active
September 13, 2024 19:29
-
-
Save tabedzki/47a1ab9c2cc6fb1e0bb321bd70a77e86 to your computer and use it in GitHub Desktop.
Proposed alternative to current Kilosort Logging and interplay with module level logging
This file contains hidden or 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
outer_logger - DEBUG - This is a debug message from the outer logger before calling run_kilosort. | |
outer_logger - INFO - This is an info message from the outer logger before calling run_kilosort. | |
outer_logger - ERROR - Kilosort4 failed | |
Traceback (most recent call last): | |
File "/Users/ct5868/testing_logging_levels.py", line 35, in <module> | |
run_kilosort(settings={}, results_dir='.') | |
File "/Users/ct5868/test_logging.py", line 26, in run_kilosort | |
raise e | |
File "/Users/ct5868/test_logging.py", line 21, in run_kilosort | |
will_fail() | |
File "/Users/ct5868/test_logging.py", line 29, in will_fail | |
0/0 | |
~^~ | |
ZeroDivisionError: division by zero | |
NOTE: See ./kilosort4.log for detailed info. | |
outer_logger - DEBUG - This is a debug message from the outer logger after calling run_kilosort. | |
outer_logger - INFO - This is an info message from the outer logger after calling run_kilosort. |
This file contains hidden or 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
09-13 12:46 test_logging DEBUG Debug message | |
09-13 12:46 test_logging INFO Kilosort version 4 | |
09-13 12:46 test_logging INFO Sorting None | |
09-13 12:46 test_logging INFO ---------------------------------------- | |
09-13 12:46 test_logging ERROR Encountered error in `run_kilosort`: | |
Traceback (most recent call last): | |
File "/Users/ct5868/test_logging.py", line 21, in run_kilosort | |
will_fail() | |
File "/Users/ct5868/test_logging.py", line 29, in will_fail | |
0/0 | |
~^~ | |
ZeroDivisionError: division by zero |
This file contains hidden or 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
outer_logger - INFO - This is an info message from the outer logger before calling run_kilosort. | |
outer_logger - ERROR - Kilosort4 failed | |
Traceback (most recent call last): | |
File "/Users/ct5868/testing_logging_levels.py", line 35, in <module> | |
run_kilosort(settings={}, results_dir='.') | |
File "/Users/ct5868/test_logging.py", line 26, in run_kilosort | |
raise e | |
File "/Users/ct5868/test_logging.py", line 21, in run_kilosort | |
will_fail() | |
File "/Users/ct5868/test_logging.py", line 29, in will_fail | |
0/0 | |
~^~ | |
ZeroDivisionError: division by zero | |
NOTE: See ./kilosort4.log for detailed info. | |
outer_logger - INFO - This is an info message from the outer logger after calling run_kilosort. |
This file contains hidden or 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
# test_logging.py | |
from pathlib import Path | |
import logging | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.DEBUG) # Set the bug level of the package to be DEBUG by default. Allow user to override | |
logger.propagate = False | |
def run_kilosort(settings, results_dir=None, filename=None): | |
results_dir = Path(results_dir) | |
# setup_logger_current(results_dir) | |
setup_logger_proposed(results_dir) | |
try: | |
logger.debug("Debug message") | |
logger.info(f"Kilosort version 4") | |
logger.info(f"Sorting {filename}") | |
logger.info('-'*40) | |
will_fail() | |
except Exception as e: | |
# This makes sure the full traceback is written to log file. | |
logger.exception('Encountered error in `run_kilosort`:') | |
e.add_note(f'NOTE: See {results_dir}/kilosort4.log for detailed info.') | |
raise e | |
def will_fail(): | |
0/0 | |
def setup_logger_proposed(results_dir): | |
# Adapted from | |
# https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library | |
# Create handlers | |
file_handler = logging.FileHandler(results_dir / 'kilosort4.log', mode='w') | |
file_handler.setLevel(logging.DEBUG) | |
# Create formatters and add them to the handlers | |
file_formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M') | |
file_handler.setFormatter(file_formatter) | |
# Add handlers to the logger | |
logger.addHandler(file_handler) |
This file contains hidden or 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
# testing_logging_levels.py | |
import logging | |
import logging.config | |
from test_logging import run_kilosort | |
# Define the logging configuration | |
logging_config = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'default': { | |
'format': '%(name)s - %(levelname)s - %(message)s', | |
}, | |
}, | |
'handlers': { | |
'console': { | |
'class': 'logging.StreamHandler', | |
'level': 'DEBUG', | |
'formatter': 'default', | |
}, | |
'file': { | |
'class': 'logging.FileHandler', | |
'filename': 'my_custom.log', | |
'mode': 'w', | |
'level': 'INFO', | |
'formatter': 'default', | |
}, | |
}, | |
'root': { | |
'level': 'DEBUG', | |
'handlers': ['console', 'file'], | |
}, | |
} | |
# Apply the logging configuration | |
logging.config.dictConfig(logging_config) | |
# Get the outer logger | |
outer_logger = logging.getLogger('outer_logger') | |
outer_logger.setLevel(logging.DEBUG) | |
# Set logging levels | |
test_logging_logger = logging.getLogger('test_logging') | |
test_logging_logger.propagate = False # Also set false in `test_logging.py` | |
# Log a message before calling run_kilosort | |
outer_logger.debug('This is a debug message from the outer logger before calling run_kilosort.') | |
outer_logger.info('This is an info message from the outer logger before calling run_kilosort.') | |
# Call the function from test_logging.py | |
try: | |
run_kilosort(settings={}, results_dir='.') | |
except: | |
outer_logger.exception("Kilosort4 failed") | |
# Log a message after calling run_kilosort | |
outer_logger.debug('This is a debug message from the outer logger after calling run_kilosort.') | |
outer_logger.info('This is an info message from the outer logger after calling run_kilosort.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment