Skip to content

Commit

Permalink
Convert funcitonlogger to native null logger
Browse files Browse the repository at this point in the history
  • Loading branch information
JB Lovland committed Nov 17, 2023
1 parent 3613ee9 commit 00bc562
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/xtgeo/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
from xtgeo.common._xyz_enum import _AttrName, _AttrType, _XYZType
from xtgeo.common.exceptions import WellNotFoundError
from xtgeo.common.sys import _XTGeoFile, inherit_docstring
from xtgeo.common.log import null_logger

# flake8: noqa
from xtgeo.common.xtgeo_dialog import XTGDescription, XTGeoDialog, XTGShowProgress
from xtgeo.xyz._xyz_data import _XYZData

__all__ = [
"_XYZData",
"_AttrName",
"_AttrType",
"_XYZType",
"WellNotFoundError",
"_XTGeoFile",
"inherit_docstring",
"null_logger",
"WellNotFoundError",
"XTGDescription",
"XTGeoDialog",
"XTGShowProgress",
"_AttrName",
"_AttrType",
"_XTGeoFile",
"_XYZData",
"_XYZType",
]
6 changes: 2 additions & 4 deletions src/xtgeo/common/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
import numpy as np

from xtgeo import XTGeoCLibError
from xtgeo.common import XTGeoDialog
from xtgeo.common import null_logger
from xtgeo.cxtgeo import _cxtgeo

xtg = XTGeoDialog()

logger = xtg.functionlogger(__name__)
logger = null_logger(__name__)


def ib_to_ijk(
Expand Down
29 changes: 29 additions & 0 deletions src/xtgeo/common/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

import logging

def null_logger(name: str) -> logging.Logger:
"""
Create and return a logger with a NullHandler.
This function creates a logger for the specified name and attaches a NullHandler to it.
The NullHandler prevents logging messages from being automatically output to the console
or other default handlers. This is particularly useful in library modules where you want
to provide the users of the library the flexibility to configure their own logging behavior.
Args:
name (str): The name of the logger to be created. This is typically the name of the module
in which the logger is created (e.g., using __name__).
Returns:
logging.Logger: A logger object configured with a NullHandler.
Example:
# In a library module
logger = null_logger(__name__)
logger.info("This info won't be logged to the console by default.")
"""

logger = logging.getLogger(name)
logger.addHandler(logging.NullHandler())
return logger

0 comments on commit 00bc562

Please sign in to comment.