Skip to content

Commit b1f92cc

Browse files
committed
Once again revert subclassing logging.Logger (setLoggerClass is not working / ignored by the plugins)
1 parent f978e6b commit b1f92cc

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

bidscoin/__init__.py

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from pathlib import Path
2929
from importlib import metadata
3030
from typing import Tuple, Union, List
31-
import logging
31+
from logging import getLogger
3232
from .due import due, Doi
3333
try:
3434
import tomllib
@@ -61,8 +61,9 @@
6161
bidscoinroot = Path(__file__).parent
6262
schemafolder = bidscoinroot/'schema'
6363

64-
# Get the BIDSCOIN_DEBUG environment variable to set the log-messages and logging level, etc
65-
DEBUG = os.getenv('BIDSCOIN_DEBUG','').upper() in ('1', 'TRUE', 'Y', 'YES')
64+
# Get the LOGGER and BIDSCOIN_DEBUG environment variable to set the log-messages and logging level, etc
65+
LOGGER = getLogger(__name__)
66+
DEBUG = os.getenv('BIDSCOIN_DEBUG','').upper() in ('1', 'TRUE', 'Y', 'YES')
6667

6768
# Create a BIDScoin user configuration directory if needed
6869
configdir = Path(os.getenv('BIDSCOIN_CONFIGDIR') or (Path.home() if os.access(Path.home(),os.W_OK) else Path(tempfile.gettempdir()))/'.bidscoin')/__version__
@@ -99,40 +100,6 @@
99100
path='bidscoin', version=__version__, cite_module=True, tags=['reference-implementation'])
100101

101102

102-
class CustomLogger(logging.Logger):
103-
"""Extend the Logger class to add custom methods for the new levels"""
104-
105-
# Define custom logging levels
106-
BCDEBUG, BCDEBUG_LEVEL = 'BCDEBUG', 11 # NB: using the standard debug mode will generate may debug messages from imports
107-
VERBOSE, VERBOSE_LEVEL = 'VERBOSE', 15
108-
SUCCESS, SUCCESS_LEVEL = 'SUCCESS', 25
109-
110-
# Add custom log levels to logging
111-
logging.addLevelName(BCDEBUG_LEVEL, BCDEBUG)
112-
logging.addLevelName(VERBOSE_LEVEL, VERBOSE)
113-
logging.addLevelName(SUCCESS_LEVEL, SUCCESS)
114-
115-
def bcdebug(self, message, *args, **kwargs):
116-
"""Custom BIDSCOIN DEBUG messages"""
117-
if self.isEnabledFor(self.BCDEBUG_LEVEL):
118-
self._log(self.BCDEBUG_LEVEL, message, args, **kwargs)
119-
120-
def verbose(self, message, *args, **kwargs):
121-
"""Custom BIDSCOIN VERBOSE messages"""
122-
if self.isEnabledFor(self.VERBOSE_LEVEL):
123-
self._log(self.VERBOSE_LEVEL, message, args, **kwargs)
124-
125-
def success(self, message, *args, **kwargs):
126-
"""Custom BIDSCOIN SUCCESS messages"""
127-
if self.isEnabledFor(self.SUCCESS_LEVEL):
128-
self._log(self.SUCCESS_LEVEL, message, args, **kwargs)
129-
130-
131-
# Get a logger from the custom logger class
132-
logging.setLoggerClass(CustomLogger)
133-
LOGGER = logging.getLogger(__name__)
134-
135-
136103
def check_version() -> Tuple[str, Union[bool, None], str]:
137104
"""
138105
Compares the BIDSCOIN version from the local metadata to the remote pypi repository

bidscoin/bcoin.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,16 @@ def synchronize(pbatch, jobids: list, wait: int=15):
113113
def setup_logging(logfile: Path=Path()):
114114
"""
115115
Set up the logging framework:
116-
- Add a console stream handler for generating terminal output.
117-
- Optionally add file handlers for normal log and warning/error log if logfile is provided.
116+
1) Extend the Logger class with custom 'bcdebug', 'verbose' and 'success' logging levels / methods
117+
2) Add a console streamhandler
118+
3) If given a logfile, then add a regular log + a warning/error filehandler
118119
119-
:param logfile: Path to the logfile. If none, logging is console-only
120+
NB: Defining a `CustomLogger(logging.Logger)` class + setting `logging.setLoggerClass(CustomLogger)`
121+
(as in https://github.com/xolox/python-verboselogs/blob/master/verboselogs/__init__.py)
122+
does not seem to work / is ignored by the plugins. Extending `logging.getLoggerClass()` works robustly
123+
124+
:param logfile: Name of the logfile
125+
:return:
120126
"""
121127

122128
# Set the default formats
@@ -128,7 +134,28 @@ def setup_logging(logfile: Path=Path()):
128134
cfmt = '%(levelname)s | %(message)s'
129135
datefmt = '%Y-%m-%d %H:%M:%S'
130136

131-
# Get the root logger and set the appropriate level
137+
# Register custom log levels
138+
for name, level in {'BCDEBUG': 11, 'VERBOSE': 15, 'SUCCESS': 25}.items():
139+
logging.addLevelName(level, name)
140+
setattr(logging, name, level)
141+
142+
# Register custom log methods
143+
def bcdebug(self, message, *args, **kws):
144+
if self.isEnabledFor(logging.BCDEBUG):
145+
self._log(logging.BCDEBUG, message, args, **kws)
146+
logging.getLoggerClass().bcdebug = bcdebug
147+
148+
def verbose(self, message, *args, **kws):
149+
if self.isEnabledFor(logging.VERBOSE):
150+
self._log(logging.VERBOSE, message, args, **kws)
151+
logging.getLoggerClass().verbose = verbose
152+
153+
def success(self, message, *args, **kws):
154+
if self.isEnabledFor(logging.SUCCESS):
155+
self._log(logging.SUCCESS, message, args, **kws)
156+
logging.getLoggerClass().success = success
157+
158+
# Set the root logging level
132159
logger = logging.getLogger()
133160
logger.setLevel('BCDEBUG' if DEBUG else 'VERBOSE')
134161

0 commit comments

Comments
 (0)