Skip to content

Commit

Permalink
Merge pull request #484 from kinverarity1/logging
Browse files Browse the repository at this point in the history
Add TRACE_LASIO logging level
  • Loading branch information
kinverarity1 authored Jul 16, 2021
2 parents 00dea2e + 30a933b commit 36bc774
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 40 deletions.
52 changes: 52 additions & 0 deletions lasio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
import logging
import os

def add_logging_level(levelName, levelNum, methodName=None):
"""
Comprehensively adds a new logging level to the `logging` module and the
currently configured logging class.
`levelName` becomes an attribute of the `logging` module with the value
`levelNum`. `methodName` becomes a convenience method for both `logging`
itself and the class returned by `logging.getLoggerClass()` (usually just
`logging.Logger`). If `methodName` is not specified, `levelName.lower()` is
used.
To avoid accidental clobberings of existing attributes, this method will
raise an `AttributeError` if the level name is already an attribute of the
`logging` module or if the method name is already present
Example
-------
>>> add_logging_level('TRACE', logging.DEBUG - 5)
>>> logging.getLogger(__name__).setLevel("TRACE")
>>> logging.getLogger(__name__).trace('that worked')
>>> logging.trace('so did this')
>>> logging.TRACE
5
"""
if not methodName:
methodName = levelName.lower()

if hasattr(logging, levelName):
raise AttributeError('{} already defined in logging module'.format(levelName))
if hasattr(logging, methodName):
raise AttributeError('{} already defined in logging module'.format(methodName))
if hasattr(logging.getLoggerClass(), methodName):
raise AttributeError('{} already defined in logger class'.format(methodName))

# This method was inspired by the answers to Stack Overflow post
# http://stackoverflow.com/q/2183233/2988730, especially
# http://stackoverflow.com/a/13638084/2988730
def logForLevel(self, message, *args, **kwargs):
if self.isEnabledFor(levelNum):
self._log(levelNum, message, args, **kwargs)
def logToRoot(message, *args, **kwargs):
logging.log(levelNum, message, *args, **kwargs)

logging.addLevelName(levelNum, levelName)
setattr(logging, levelName, levelNum)
setattr(logging.getLoggerClass(), methodName, logForLevel)
setattr(logging, methodName, logToRoot)

add_logging_level("TRACE_LASIO", logging.DEBUG - 5, "trace_lasio")

from .las_version import version
from .las import LASFile, JSONEncoder
from .las_items import CurveItem, HeaderItem, SectionItems
Expand Down
2 changes: 1 addition & 1 deletion lasio/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def inspect_data_section(file_obj, line_nos, regexp_subs, ignore_comments="#"):
line = re.sub(pattern, sub_str, line)
# split line and count number of elements
n_items = len(["".join(t) for t in sow_regex.findall(line)])
logger.debug(
logger.trace_lasio(
"Line {}: {} items counted in '{}'".format(line_no + 1, n_items, line)
)
item_counts.append(n_items)
Expand Down
Loading

0 comments on commit 36bc774

Please sign in to comment.