Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swell logger extends logging.Logger #450

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions src/swell/deployment/create_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from swell.swell_path import get_swell_path
from swell.utilities.dictionary import add_comments_to_dictionary, dict_get
from swell.utilities.jinja2 import template_string_jinja2
from swell.utilities.logger import Logger
from swell.utilities.logger import Logger, get_logger
from swell.utilities.slurm import prepare_scheduling_dict


Expand All @@ -36,7 +36,7 @@ def clone_config(
advanced: bool
) -> str:
# Create a logger
logger = Logger('SwellCloneExperiment')
logger = get_logger('SwellCloneExperiment')

# Check that configuration exists and is a YAML file
if not os.path.isfile(configuration):
Expand Down Expand Up @@ -76,7 +76,7 @@ def prepare_config(

# Create a logger
# ---------------
logger = Logger('SwellPrepSuiteConfig')
logger = get_logger('SwellPrepSuiteConfig')

# Assert valid method
# -------------------
Expand Down Expand Up @@ -167,7 +167,7 @@ def create_experiment_directory(

# Create a logger
# ---------------
logger = Logger('SwellCreateExperiment')
logger = get_logger('SwellCreateExperiment')

# Call the experiment config and suite generation
# ------------------------------------------------
Expand Down Expand Up @@ -277,7 +277,7 @@ def copy_platform_files(
src_path_file = os.path.join(platform_path, os.path.split(s)[0], src_file)
dst_path_file = os.path.join(exp_suite_path, '{}'.format(src_file))
if os.path.exists(src_path_file):
logger.trace('Copying {} to {}'.format(src_path_file, dst_path_file))
logger.debug('Copying {} to {}'.format(src_path_file, dst_path_file))
shutil.copy(src_path_file, dst_path_file)


Expand Down
7 changes: 4 additions & 3 deletions src/swell/deployment/launch_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import os

# local imports
from swell.utilities.logger import Logger
from swell.utilities.logger import get_logger
from swell.utilities.shell_commands import run_subprocess

# --------------------------------------------------------------------------------------------------
Expand All @@ -27,7 +27,7 @@ def __init__(
log_path: str
) -> None:

self.logger = Logger('DeployWorkflow')
self.logger = get_logger('DeployWorkflow')
self.suite_path = suite_path
self.experiment_name = experiment_name
self.no_detach = no_detach
Expand Down Expand Up @@ -88,7 +88,8 @@ def cylc_run_experiment(self) -> None: # NB: Could be a factory based on workfl
self.logger.info(' ', False)

# Launch the job monitor
self.logger.input('Launching the TUI, press \'q\' at any time to exit the TUI')
self.logger.critical('Launching the TUI, press \'q\' at any time to exit the TUI')
input()
self.logger.info(' ', False)
self.logger.info('TUI can be relaunched with:')
self.logger.info(' ', False)
Expand Down
4 changes: 2 additions & 2 deletions src/swell/tasks/base/task_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from swell.utilities.config import Config
from swell.utilities.data_assimilation_window_params import DataAssimilationWindowParams
from swell.utilities.datetime_util import Datetime
from swell.utilities.logger import Logger
from swell.utilities.logger import get_logger
from swell.utilities.render_jedi_interface_files import JediConfigRendering
from swell.utilities.geos import Geos

Expand All @@ -46,7 +46,7 @@ def __init__(

# Create message logger
# ---------------------
self.logger = Logger(task_name)
self.logger = get_logger(task_name)

# Write out the initialization info
# ---------------------------------
Expand Down
12 changes: 6 additions & 6 deletions src/swell/test/code_tests/code_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import os
import unittest

from swell.utilities.logger import Logger
from swell.utilities.logger import get_logger
from swell.test.code_tests.slurm_test import SLURMConfigTest
from swell.test.code_tests.test_pinned_versions import PinnedVersionsTest
from swell.test.code_tests.unused_variables_test import UnusedVariablesTest
Expand All @@ -24,12 +24,12 @@
def code_tests() -> None:

# Create a logger
logger = Logger('TestSuite')
logger.test('Running Swell Test Suite')
logger = get_logger('TestSuite')
logger.info('Running Swell Test Suite')

# Default log_info testing to false
os.environ.setdefault("LOG_INFO", "0")
# Set to 1 when errors are being debugged
# Default log_level minimum to warning
os.environ.setdefault("LOGLEVEL", "WARNING")
# Set to INFO or DEBUG when errors are being debugged

# Create a test suite
test_suite = unittest.TestSuite()
Expand Down
4 changes: 2 additions & 2 deletions src/swell/test/code_tests/missing_obs_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from swell.utilities.datetime_util import Datetime
from swell.utilities.run_jedi_executables import check_obs
from swell.utilities.render_jedi_interface_files import JediConfigRendering
from swell.utilities.logger import Logger
from swell.utilities.logger import get_logger
from datetime import datetime as dt

'''
Expand All @@ -14,7 +14,7 @@
# Set fields
path_to_observing_sys_yamls = 'missing_obs_test/experiment/run/20211212T000000Z/' + \
'geos_atmosphere/observing_system_records'
logger = Logger('Missing Obs Test')
logger = get_logger('Missing Obs Test')
cycle_time_str = '20211212T000000Z'
cycle_time = dt.strptime(cycle_time_str, '%Y%m%dT%H%M%SZ')
experiment_root = 'missing_obs_test'
Expand Down
4 changes: 2 additions & 2 deletions src/swell/test/code_tests/slurm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

# --------------------------------------------------------------------------------------------------

import logging
import unittest

from swell.utilities.slurm import prepare_scheduling_dict
from swell.utilities.logger import get_logger
from unittest.mock import patch, Mock

# --------------------------------------------------------------------------------------------------
Expand All @@ -24,7 +24,7 @@ class SLURMConfigTest(unittest.TestCase):
@patch("platform.platform")
def test_slurm_config(self, platform_mocked: Mock, mock_global_defaults: Mock) -> None:

logger = logging.getLogger()
logger = get_logger()

# Fake user-specified global values (for consistent unit tests)
mock_global_defaults.return_value = {"qos": "dastest"}
Expand Down
9 changes: 7 additions & 2 deletions src/swell/test/code_tests/test_generate_observing_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest
import subprocess
from datetime import datetime as dt
from swell.utilities.logger import Logger
from swell.utilities.logger import get_logger
from swell.utilities.get_channels import get_channels
from swell.test.code_tests.testing_utilities import suppress_stdout
from swell.utilities.observing_system_records import ObservingSystemRecords
Expand All @@ -27,7 +27,7 @@ class GenerateObservingSystemTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.logger = Logger("GenerateObservingSystemTest")
cls.logger = get_logger("GenerateObservingSystemTest")
cls.observing_system_records_path = "./output/"
cls.dt_cycle_time = dt.strptime("20211212T000000Z", "%Y%m%dT%H%M%SZ")
cls.path_to_gsi_records = os.path.join("GEOS_mksi/", "sidb")
Expand All @@ -48,10 +48,15 @@ def test_geos_mksi_broken(self):
abort_message = "\nHERE IS THE TRACEBACK: \n----------------------\n\n" + \
"Missing active channels for cris-fsr_npp, " + \
"Confirm that you are using the right version of GEOSmksi"

with self.assertRaises(SystemExit) as abort, suppress_stdout():
# Suppress logging output for test
log_level = self.logger.level
self.logger.setLevel(60)
get_channels(self.observing_system_records_path, observations[0],
self.dt_cycle_time, self.logger)
self.assertEqual(abort.exception, abort_message)
self.logger.setLevel(log_level)

def test_geos_mksi_develop(self):

Expand Down
8 changes: 6 additions & 2 deletions src/swell/test/code_tests/test_pinned_versions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
import unittest
import subprocess
from swell.utilities.logger import Logger
from swell.utilities.logger import get_logger
from swell.test.code_tests.testing_utilities import suppress_stdout
from swell.utilities.pinned_versions.check_hashes import check_hashes


class PinnedVersionsTest(unittest.TestCase):

def test_wrong_hash(self) -> None:
logger = Logger("PinnedVersionsTest")
logger = get_logger("PinnedVersionsTest")
jedi_bundle_dir = "jedi_bundle/"
if not os.path.exists(jedi_bundle_dir):
os.makedirs(jedi_bundle_dir)
Expand All @@ -25,5 +25,9 @@ def test_wrong_hash(self) -> None:
abort_message = "Wrong commit hashes found for these repositories in jedi_bundle: [oops]"
# Run check hash (expect abort)
with self.assertRaises(SystemExit) as abort, suppress_stdout():
# Suppress logging output for test
log_level = logger.level
logger.setLevel(60)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Magic number. Can you add a comment to what 60 means here?

check_hashes(jedi_bundle_dir, logger)
self.assertEqual(abort.exception, abort_message)
logger.setLevel(log_level)
4 changes: 2 additions & 2 deletions src/swell/test/platform_tests/check_hashes_discover.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from swell.utilities.logger import Logger
from swell.utilities.logger import get_logger
import swell.utilities.pinned_versions.check_hashes import check_hashes

logger = Logger("CheckHashesTest")
logger = get_logger("CheckHashesTest")
bundle = "/discover/nobackup/projects/gmao/advda/jedi_bundles/current_pinned_jedi_bundle/source/"
check_hashes(bundle, logger)
4 changes: 4 additions & 0 deletions src/swell/test/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# --------------------------------------------------------------------------------------------------

import importlib
import os

# --------------------------------------------------------------------------------------------------

Expand All @@ -20,6 +21,9 @@ def test_wrapper(test: str) -> None:
# Test script
test_script_file = 'swell.test.'+test+'.'+test

# Suppress warning from NUMEXPR
os.environ['NUMEXPR_MAX_THREADS'] = str(8)

# Import the correct method
test_method = getattr(importlib.import_module(test_script_file), test)

Expand Down
4 changes: 2 additions & 2 deletions src/swell/utilities/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import shutil
from typing import Tuple

from swell.utilities.logger import Logger
from swell.utilities.logger import get_logger
from jedi_bundle.utils.yaml import load_yaml
from jedi_bundle.config.config import check_platform
from jedi_bundle.bin.jedi_bundle import get_default_config
Expand Down Expand Up @@ -93,7 +93,7 @@ def set_jedi_bundle_config(

# Add pinned_versions to jedi_bundle_config if applicable
if use_pinned:
logger = Logger("LoadPinnedVersions")
logger = get_logger("LoadPinnedVersions")
pinned_config_file = get_pinned_vers_path()
pinned_versions_dict = load_yaml(logger, pinned_config_file)
jedi_bundle_config['pinned_versions'] = pinned_versions_dict
Expand Down
Loading
Loading