Skip to content

Commit

Permalink
propagate ugrid logger (#4351)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjlittle committed Oct 5, 2021
1 parent 1b29d17 commit a45200f
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 41 deletions.
43 changes: 26 additions & 17 deletions lib/iris/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import warnings


def get_logger(name, datefmt=None, fmt=None, level=None, propagate=None):
def get_logger(
name, datefmt=None, fmt=None, level=None, propagate=None, handler=True
):
"""
Create a :class:`logging.Logger` with a :class:`logging.StreamHandler`
and custom :class:`logging.Formatter`.
Expand Down Expand Up @@ -68,16 +70,14 @@ def get_logger(name, datefmt=None, fmt=None, level=None, propagate=None):
passed to the handlers of higher level loggers. Defaults to
``False``.
"""
if datefmt is None:
# Default date format string.
datefmt = "%d-%m-%Y %H:%M:%S"
* handler:
Create and attach a :class:`logging.StreamHandler` to the
logger. Defaults to ``True``.
# Default format string.
_fmt = "%(asctime)s %(name)s %(levelname)s - %(message)s"
# Append additional format string, if appropriate.
fmt = _fmt if fmt is None else f"{_fmt} {fmt}"
Returns:
A :class:`logging.Logger`.
"""
if level is None:
# Default logging level.
level = "INFO"
Expand All @@ -91,16 +91,25 @@ def get_logger(name, datefmt=None, fmt=None, level=None, propagate=None):
logger.setLevel(level)
logger.propagate = propagate

# Create a formatter.
formatter = logging.Formatter(fmt=fmt, datefmt=datefmt)
# Create and add the handler to the logger, if required.
if handler:
if datefmt is None:
# Default date format string.
datefmt = "%d-%m-%Y %H:%M:%S"

# Default format string.
_fmt = "%(asctime)s %(name)s %(levelname)s - %(message)s"
# Append additional format string, if appropriate.
fmt = _fmt if fmt is None else f"{_fmt} {fmt}"

# Create a formatter.
formatter = logging.Formatter(fmt=fmt, datefmt=datefmt)

# Create a logging handler.
handler = logging.StreamHandler()
# handler.setLevel(level)
handler.setFormatter(formatter)
# Create a logging handler.
handler = logging.StreamHandler()
handler.setFormatter(formatter)

# Add the handler to the logger.
logger.addHandler(handler)
logger.addHandler(handler)

return logger

Expand Down
15 changes: 6 additions & 9 deletions lib/iris/experimental/ugrid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@
definition at :const:`iris.experimental.ugrid.load.PARSE_UGRID_ON_LOAD`.
"""
# Configure the logger.
from ...config import get_logger

logger = get_logger(__name__, fmt="[%(cls)s.%(funcName)s]")

# Lazy imports to avoid circularity.
from .load import load_mesh, load_meshes, PARSE_UGRID_ON_LOAD # isort:skip
from .mesh import Connectivity, Mesh, MeshCoord # isort:skip
from .save import save_mesh # isort:skip

from .load import PARSE_UGRID_ON_LOAD, load_mesh, load_meshes
from .mesh import Connectivity, Mesh, MeshCoord
from .save import save_mesh

__all__ = [
"Connectivity",
Expand All @@ -34,3 +28,6 @@
"load_meshes",
"save_mesh",
]

# Configure the logger as a root logger.
logger = get_logger(__name__, fmt="[%(cls)s.%(funcName)s]", level="NOTSET")
5 changes: 4 additions & 1 deletion lib/iris/experimental/ugrid/cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
"""
import logging

from . import logger
from ...config import get_logger
from ...fileformats import cf
from .mesh import Connectivity

# Configure the logger.
logger = get_logger(__name__, propagate=True, handler=False)


class CFUGridConnectivityVariable(cf.CFVariable):
"""
Expand Down
5 changes: 4 additions & 1 deletion lib/iris/experimental/ugrid/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pathlib import Path
import threading

from . import logger
from ...config import get_logger
from ...coords import AuxCoord
from ...fileformats import netcdf
from ...fileformats._nc_load_rules.helpers import get_attr_units, get_names
Expand All @@ -31,6 +31,9 @@
)
from .mesh import Connectivity, Mesh

# Configure the logger.
logger = get_logger(__name__, propagate=True, handler=False)


class ParseUGridOnLoad(threading.local):
def __init__(self):
Expand Down
5 changes: 4 additions & 1 deletion lib/iris/experimental/ugrid/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@
from dask import array as da
import numpy as np

from . import logger
from ... import _lazy_data as _lazy
from ...common import (
CFVariableMixin,
metadata_filter,
metadata_manager_factory,
)
from ...config import get_logger
from ...coords import AuxCoord, _DimensionalMetadata
from ...exceptions import ConnectivityNotFoundError, CoordinateNotFoundError
from ...util import guess_coord_axis
from .metadata import ConnectivityMetadata, MeshCoordMetadata, MeshMetadata

# Configure the logger.
logger = get_logger(__name__, propagate=True, handler=False)

#: Numpy "threshold" printoptions default argument.
NP_PRINTOPTIONS_THRESHOLD = 10
#: Numpy "edgeitems" printoptions default argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@

import numpy as np

from iris.experimental.ugrid import logger
from iris.experimental.ugrid.cf import CFUGridAuxiliaryCoordinateVariable
from iris.experimental.ugrid.cf import (
CFUGridAuxiliaryCoordinateVariable,
logger,
)
from iris.tests.unit.experimental.ugrid.cf.test_CFUGridReader import (
netcdf_ugrid_variable,
)


def named_variable(name):
# Don't need to worry about dimensions or dtype for these tests.
return netcdf_ugrid_variable(name, "", np.int)
return netcdf_ugrid_variable(name, "", int)


class TestIdentify(tests.IrisTest):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

import numpy as np

from iris.experimental.ugrid import logger
from iris.experimental.ugrid.cf import CFUGridConnectivityVariable
from iris.experimental.ugrid.cf import CFUGridConnectivityVariable, logger
from iris.experimental.ugrid.mesh import Connectivity
from iris.tests.unit.experimental.ugrid.cf.test_CFUGridReader import (
netcdf_ugrid_variable,
Expand All @@ -26,7 +25,7 @@

def named_variable(name):
# Don't need to worry about dimensions or dtype for these tests.
return netcdf_ugrid_variable(name, "", np.int)
return netcdf_ugrid_variable(name, "", int)


class TestIdentify(tests.IrisTest):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

import numpy as np

from iris.experimental.ugrid import logger
from iris.experimental.ugrid.cf import CFUGridMeshVariable
from iris.experimental.ugrid.cf import CFUGridMeshVariable, logger
from iris.tests.unit.experimental.ugrid.cf.test_CFUGridReader import (
netcdf_ugrid_variable,
)


def named_variable(name):
# Don't need to worry about dimensions or dtype for these tests.
return netcdf_ugrid_variable(name, "", np.int)
return netcdf_ugrid_variable(name, "", int)


class TestIdentify(tests.IrisTest):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import tempfile
from uuid import uuid4

from iris.experimental.ugrid import logger
from iris.experimental.ugrid.load import PARSE_UGRID_ON_LOAD, load_meshes
from iris.experimental.ugrid.load import (
PARSE_UGRID_ON_LOAD,
load_meshes,
logger,
)


def setUpModule():
Expand Down
3 changes: 2 additions & 1 deletion lib/iris/tests/unit/experimental/ugrid/mesh/test_Mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

from iris.coords import AuxCoord
from iris.exceptions import ConnectivityNotFoundError, CoordinateNotFoundError
from iris.experimental.ugrid import logger, mesh, metadata
from iris.experimental.ugrid import mesh, metadata
from iris.experimental.ugrid.mesh import logger


class TestMeshCommon(tests.IrisTest):
Expand Down

0 comments on commit a45200f

Please sign in to comment.