Skip to content

Commit 214ef38

Browse files
authored
✨ Use dedicated logger (#483)
1 parent bf9b495 commit 214ef38

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

bibtexparser/middlewares/fieldkeys.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
from .middleware import BlockMiddleware
1111

12+
logger = logging.getLogger(__name__)
13+
1214

1315
class NormalizeFieldKeys(BlockMiddleware):
1416
"""Normalize field keys to lowercase.
@@ -37,7 +39,7 @@ def transform_entry(self, entry: Entry, library: "Library") -> Entry:
3739
# if performance is a concern, we could emit a warning with only {entry.key}
3840
# to remove "seen_normalized_keys" and this if statement
3941
if normalized_key in seen_normalized_keys:
40-
logging.warning(
42+
logger.warning(
4143
f"NormalizeFieldKeys: in entry '{entry.key}': "
4244
+ f"duplicate normalized key '{normalized_key}' "
4345
+ f"(original '{field.key}'); overriding previous value"

bibtexparser/middlewares/latex_encoding.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from .middleware import BlockMiddleware
2323
from .names import NameParts
2424

25+
logger = logging.getLogger(__name__)
26+
2527

2628
class _PyStringTransformerMiddleware(BlockMiddleware, abc.ABC):
2729
"""Abstract utility class allowing to modify python-strings"""
@@ -59,7 +61,7 @@ def transform_entry(self, entry: Entry, library: Library) -> Block:
5961
field.value.von = self._transform_all_strings(field.value.von, errors)
6062
field.value.jr = self._transform_all_strings(field.value.jr, errors)
6163
else:
62-
logging.info(
64+
logger.info(
6365
f" [{self.metadata_key()}] Cannot python-str transform field {field.key}"
6466
f" with value type {type(field.value)}"
6567
)
@@ -76,7 +78,7 @@ def transform_string(self, string: String, library: "Library") -> Block:
7678
if isinstance(string.value, str):
7779
string.value = self._transform_python_value_string(string.value)
7880
else:
79-
logging.info(
81+
logger.info(
8082
f" [{self.metadata_key()}] Cannot python-str transform string {string.key}"
8183
f" with value type {type(string.value)}"
8284
)

bibtexparser/middlewares/middleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from bibtexparser.model import Preamble
1313
from bibtexparser.model import String
1414

15+
logger = logging.getLogger(__name__)
16+
1517

1618
class Middleware(abc.ABC):
1719
"""Implements a function to transform a block or library.
@@ -128,7 +130,7 @@ def transform_block(
128130
elif isinstance(block, ImplicitComment):
129131
return self.transform_implicit_comment(block, library)
130132

131-
logging.warning(f"Unknown block type {type(block)}")
133+
logger.warning(f"Unknown block type {type(block)}")
132134
return block
133135

134136
def transform_entry(

bibtexparser/splitter.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from .model import Preamble
2020
from .model import String
2121

22+
logger = logging.getLogger(__name__)
23+
2224

2325
class Splitter:
2426
"""Object responsible for splitting a BibTeX string into blocks.
@@ -254,7 +256,7 @@ def split(self, library: Optional[Library] = None) -> Library:
254256
if library is None:
255257
library = Library()
256258
else:
257-
logging.info("Adding blocks to existing library.")
259+
logger.info("Adding blocks to existing library.")
258260

259261
while True:
260262
m = self._next_mark(accept_eof=True)
@@ -283,12 +285,12 @@ def split(self, library: Optional[Library] = None) -> Library:
283285
library.add(self._handle_entry(m, m_val))
284286

285287
except BlockAbortedException as e:
286-
logging.warning(
288+
logger.warning(
287289
f"Parsing of `{m_val}` block (line {start_line}) "
288290
f"aborted on line {self._current_line} "
289291
f"due to syntactical error in bibtex:\n {e.abort_reason}"
290292
)
291-
logging.info(
293+
logger.info(
292294
"We will try to continue parsing, but this might lead to unexpected results."
293295
"The failed block will be stored in the `failed_blocks`of the library."
294296
)
@@ -302,14 +304,14 @@ def split(self, library: Optional[Library] = None) -> Library:
302304

303305
except ParserStateException as e:
304306
# This is a bug in the parser, not in the bibtex. We should not continue.
305-
logging.error(
307+
logger.error(
306308
"python-bibtexparser detected an invalid state. Please report this bug."
307309
)
308-
logging.error(e.message)
310+
logger.error(e.message)
309311
raise e
310312
except Exception as e:
311313
# For unknown exeptions, we want to fail hard and get the info in our issue tracker.
312-
logging.error(
314+
logger.error(
313315
f"Unexpected exception while parsing `{m_val}` block (line {start_line})"
314316
"Please report this bug."
315317
)

tests/middleware_tests/test_fieldkeys.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def test_normalize_fieldkeys_force_last(caplog):
6262
lib.add(Entry(entry_type=entry_type, key=f"entry{i}", fields=f))
6363

6464
lib = NormalizeFieldKeys().transform(lib)
65-
assert re.match(r"(WARNING\s*)(\w*\:\w*\.py\:[0-9]*\s*)(NormalizeFieldKeys)(.*)", caplog.text)
65+
assert re.match(
66+
r"(WARNING\s*)([\w\.]*\:\w*\.py\:[0-9]*\s*)(NormalizeFieldKeys)(.*)", caplog.text
67+
)
6668

6769
for key in lib.entries_dict:
6870
assert lib.entries_dict[key] == ref.entries_dict[key]

0 commit comments

Comments
 (0)