Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions satpy/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
# satpy. If not, see <http://www.gnu.org/licenses/>.
"""Testing of utils."""

import logging
import unittest
import warnings
from unittest import mock

import pytest
from numpy import sqrt

from satpy.utils import angle2xyz, lonlat2xyz, xyz2angle, xyz2lonlat, proj_units_to_meters, get_satpos


Expand Down Expand Up @@ -307,3 +312,26 @@ def test_specific_check_satpy(self):
checked_fake = True
self.assertTrue(checked_fake, "Did not find __fake module "
"mentioned in checks")


def test_debug_on(caplog):
"""Test that debug_on is working as expected."""
from satpy.utils import debug_on

def depwarn():
logger = logging.getLogger("satpy.silly")
logger.debug("But now it's just got SILLY.")
warnings.warn("Stop that! It's SILLY.", DeprecationWarning)
warnings.filterwarnings("ignore", category=DeprecationWarning)
debug_on(False)
filts_before = warnings.filters.copy()
# test that logging on, but deprecation warnings still off
with caplog.at_level(logging.DEBUG):
depwarn()
assert warnings.filters == filts_before
assert "But now it's just got SILLY." in caplog.text
debug_on(True)
# test that logging on and deprecation warnings on
with pytest.warns(DeprecationWarning):
depwarn()
assert warnings.filters != filts_before
27 changes: 25 additions & 2 deletions satpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,39 @@ def ensure_dir(filename):
os.makedirs(directory)


def debug_on():
"""Turn debugging logging on."""
def debug_on(dep_warnings=True):
"""Turn debugging logging on.

Sets up a StreamHandler to to `sys.stderr` at debug level for all
loggers, such that all debug messages (and log messages with higher
severity) are logged to the standard error stream.

By default, since Satpy 0.26, this also enables the global visibility
of deprecation warnings. This can be suppressed by passing a false
value.

Args:
dep_warnings (Optional[bool]): Switch on deprecation warnings.
Defaults to True.

Returns:
None
"""
logging_on(logging.DEBUG)
if dep_warnings:
dep_warnings_on()


def trace_on():
"""Turn trace logging on."""
logging_on(TRACE_LEVEL)


def dep_warnings_on():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dep can be missleading (I was thinking dependency, as in warnings from other libraries), maybe just rename to deprecation_warnings_on for clarity?

"""Switch on deprecation warnings."""
warnings.filterwarnings("default", category=DeprecationWarning)


def logging_on(level=logging.WARNING):
"""Turn logging on."""
global _is_logging_on
Expand Down