Skip to content

Commit 7d8396c

Browse files
authored
Logging is now scoped to package name and does not overwrite any end-user configuration (#40)
Ref: https://docs.python.org/3.11/howto/logging.html#configuring-logging-for-a-library
1 parent 26fdcaa commit 7d8396c

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

chepy/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from pathlib import Path
99
from configparser import ConfigParser
1010

11+
LOGGER = logging.getLogger("chepy")
12+
1113

1214
class ChepyConfig(object):
1315
def __init__(self):
@@ -142,5 +144,5 @@ def load_plugins(self): # pragma: no cover
142144
loaded = getattr(plugin, klass)
143145
plugins.append(loaded)
144146
except:
145-
logging.warning(f"Error loading {plugin.__name__}")
147+
LOGGER.warning(f"Error loading {plugin.__name__}")
146148
return plugins

chepy/core.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
from .modules.internal.colors import blue, cyan, green, magenta, red, yellow
2828

29+
LOGGER = logging.getLogger("chepy")
30+
2931

3032
class ChepyDecorators(object):
3133
"""A class to house all the decorators for Chepy"""
@@ -56,7 +58,7 @@ def call_stack(func, *args, **kwargs):
5658
def is_stdout(func, *args, **kwargs): # pragma: no cover
5759
"""Detect if method is being called from the cli"""
5860
if sys.stdout.isatty():
59-
logging.warning(f"{func.__name__} may not work as expected on the cli")
61+
LOGGER.warning(f"{func.__name__} may not work as expected on the cli")
6062
return func(*args, **kwargs)
6163

6264

@@ -99,15 +101,8 @@ def __init__(self, *data):
99101
self._stack = list()
100102
#: Holds register values
101103
self._registers = dict()
102-
103-
#: Log level
104-
self.log_level = logging.INFO
105-
#: Log format message
106-
self.log_format = "%(levelname)-2s - %(message)s"
107-
logging.getLogger().setLevel(self.log_level)
108-
logging.basicConfig(format=self.log_format)
109104
# logger
110-
self._log = logging
105+
self._log = LOGGER
111106

112107
@property
113108
def recipe(self) -> List[Dict[str, Union[str, Dict[str, Any]]]]:
@@ -136,7 +131,7 @@ def __str__(self):
136131
except UnicodeDecodeError: # pragma: no cover
137132
return "Could not convert to str, but the data exists in the states. Use o, output or out() to access the values"
138133
except: # pragma: no cover
139-
logging.exception(
134+
self._log.exception(
140135
"\n\nCannot print current state. Either chain with "
141136
"another method, or use one of the output methods "
142137
"Example: .o, .out, or .state\n\n"
@@ -186,7 +181,7 @@ def _info_logger(self, data: str) -> None:
186181
Returns:
187182
Chepy: The Chepy object.
188183
"""
189-
logging.info(blue(data))
184+
self._log.info(blue(data))
190185
return None
191186

192187
def _warning_logger(self, data: str) -> None: # pragma: no cover
@@ -198,7 +193,7 @@ def _warning_logger(self, data: str) -> None: # pragma: no cover
198193
Returns:
199194
Chepy: The Chepy object.
200195
"""
201-
logging.warning(yellow(data))
196+
self._log.warning(yellow(data))
202197
return None
203198

204199
def _error_logger(self, data: str) -> None: # pragma: no cover
@@ -210,7 +205,7 @@ def _error_logger(self, data: str) -> None: # pragma: no cover
210205
Returns:
211206
Chepy: The Chepy object.
212207
"""
213-
logging.error(red(data))
208+
self._log.error(red(data))
214209
return None
215210

216211
def subsection(
@@ -463,7 +458,7 @@ def delete_state(self, index: int):
463458
try:
464459
del self.states[index]
465460
except KeyError: # pragma: no cover
466-
logging.warning("{} does not exist".format(index))
461+
self._log.warning("{} does not exist".format(index))
467462
return self
468463

469464
@ChepyDecorators.call_stack
@@ -541,7 +536,7 @@ def delete_buffer(self, index: int):
541536
try:
542537
del self.buffers[index]
543538
except KeyError: # pragma: no cover
544-
logging.warning("{} does not exist".format(index))
539+
self._log.warning("{} does not exist".format(index))
545540
return self
546541

547542
@ChepyDecorators.call_stack
@@ -1520,7 +1515,7 @@ def cb(data):
15201515
"""
15211516
# dont run if from cli
15221517
if sys.stdout.isatty(): # pragma: no cover
1523-
logging.warning("callback cannot be used via the cli")
1518+
self._log.warning("callback cannot be used via the cli")
15241519
return self
15251520
self.state = callback_function(self.state)
15261521
return self
@@ -1702,7 +1697,7 @@ def search_dir(self, pattern: Union[bytes, str]):
17021697
rgx = re.compile(self._str_to_bytes(pattern), flags=re.I)
17031698
hold = []
17041699
for path in Path(paths).glob("**/*"):
1705-
if path.is_dir(): # pragma: no cover
1700+
if path.is_dir(): # pragma: no cover
17061701
continue
17071702
data = path.read_bytes()
17081703
matched = rgx.findall(data)

0 commit comments

Comments
 (0)