diff --git a/scripts/Tools/case.build b/scripts/Tools/case.build index fa71e1b98a5..c8ac8916259 100755 --- a/scripts/Tools/case.build +++ b/scripts/Tools/case.build @@ -36,7 +36,7 @@ OR "that we have a case.test_build script. If you are in a test that requires" " multiple builds, provide this flag.") - parser.add_argument("-s", "--sharedlib-only", action="store_true", + parser.add_argument("--sharedlib-only", action="store_true", help="Only build sharedlibs") parser.add_argument("-m", "--model-only", action="store_true", diff --git a/scripts/Tools/case.test_build b/scripts/Tools/case.test_build index 0ac6ba29133..208ed688e1a 100755 --- a/scripts/Tools/case.test_build +++ b/scripts/Tools/case.test_build @@ -44,7 +44,7 @@ OR parser.add_argument("--caseroot", default=os.getcwd(), help="Case directory to build") - parser.add_argument("-s", "--sharedlib-only", action="store_true", + parser.add_argument("--sharedlib-only", action="store_true", help="Only build sharedlibs") parser.add_argument("-m", "--model-only", action="store_true", diff --git a/scripts/Tools/standard_script_setup.py b/scripts/Tools/standard_script_setup.py index 0d44e4f9e65..9789b09fde9 100644 --- a/scripts/Tools/standard_script_setup.py +++ b/scripts/Tools/standard_script_setup.py @@ -3,7 +3,8 @@ that every script should do. """ -import sys, os, logging, doctest, argparse +import sys, os, logging, doctest, argparse, logging.config +import __main__ as main _CIMEROOT = os.environ.get("CIMEROOT") if(_CIMEROOT is None): _CIMEROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..","..") @@ -15,5 +16,5 @@ import CIME.utils CIME.utils.check_minimum_python_version(2, 7) CIME.utils.stop_buffering_output() - -logging.basicConfig() +# set up logging to file +logging.basicConfig(level=logging.INFO) diff --git a/utils/python/CIME/XML/component.py b/utils/python/CIME/XML/component.py index 99be8b92f85..cb799b23243 100644 --- a/utils/python/CIME/XML/component.py +++ b/utils/python/CIME/XML/component.py @@ -6,6 +6,8 @@ from entry_id import EntryID from CIME.utils import expect, get_cime_root, get_model +logger = logging.getLogger(__name__) + class Component(EntryID): def __init__(self,infile): """ diff --git a/utils/python/CIME/XML/entry_id.py b/utils/python/CIME/XML/entry_id.py index 3b98d6e9357..3baf9d80d97 100644 --- a/utils/python/CIME/XML/entry_id.py +++ b/utils/python/CIME/XML/entry_id.py @@ -7,6 +7,8 @@ from CIME.utils import expect from generic_xml import GenericXML +logger = logging.getLogger(__name__) + class EntryID(GenericXML): def __init__(self, infile=None): @@ -36,7 +38,7 @@ def set_default_value(self, vid, attributes=None): if (att.key in attributes): if (re.search(attributes[att.key],att.text)): value = valnode.text - logging.info("id %s value %s" % (vid, valnode.text)) + logger.info("id %s value %s" % (vid, valnode.text)) if (value is None): value = self.get_node("default_value", root=node) @@ -164,7 +166,7 @@ def add_elements_by_group(self, srcobj, attlist, infile): self.set_default_value(node, attlist) node = self.cleanupnode(node) self.groups[gname].append(node) - logging.info ("Adding to group "+gname) + logger.debug ("Adding to group "+gname) return nodelist def cleanupnode(self,node): diff --git a/utils/python/CIME/XML/env_archive.py b/utils/python/CIME/XML/env_archive.py index cae0acb24b7..bf81aa6c78e 100644 --- a/utils/python/CIME/XML/env_archive.py +++ b/utils/python/CIME/XML/env_archive.py @@ -5,6 +5,8 @@ from env_base import EnvBase +logger = logging.getLogger(__name__) + class EnvArchive(EnvBase): def __init__(self, case_root=os.getcwd(), infile="env_archive.xml"): """ diff --git a/utils/python/CIME/XML/env_base.py b/utils/python/CIME/XML/env_base.py index 48eb72d630d..eed04a6140c 100644 --- a/utils/python/CIME/XML/env_base.py +++ b/utils/python/CIME/XML/env_base.py @@ -6,6 +6,8 @@ from entry_id import EntryID from headers import Headers +logger = logging.getLogger(__name__) + class EnvBase(EntryID): def __init__(self, case_root, infile): if(os.path.isabs(infile)): diff --git a/utils/python/CIME/XML/env_batch.py b/utils/python/CIME/XML/env_batch.py index ac20e4dcc2e..41858b99c43 100644 --- a/utils/python/CIME/XML/env_batch.py +++ b/utils/python/CIME/XML/env_batch.py @@ -5,6 +5,8 @@ from env_base import EnvBase +logger = logging.getLogger(__name__) + class EnvBatch(EnvBase): def __init__(self, case_root=os.getcwd(), infile="env_batch.xml"): """ diff --git a/utils/python/CIME/XML/env_build.py b/utils/python/CIME/XML/env_build.py index 70cc49f7900..7a57d45bec6 100644 --- a/utils/python/CIME/XML/env_build.py +++ b/utils/python/CIME/XML/env_build.py @@ -5,6 +5,8 @@ from env_base import EnvBase +logger = logging.getLogger(__name__) + class EnvBuild(EnvBase): def __init__(self, case_root=os.getcwd(), infile="env_build.xml"): """ diff --git a/utils/python/CIME/XML/env_case.py b/utils/python/CIME/XML/env_case.py index b02ed913aac..d381d8d0573 100644 --- a/utils/python/CIME/XML/env_case.py +++ b/utils/python/CIME/XML/env_case.py @@ -5,6 +5,8 @@ from env_base import EnvBase +logger = logging.getLogger(__name__) + class EnvCase(EnvBase): def __init__(self, case_root=os.getcwd(), infile="env_case.xml"): """ diff --git a/utils/python/CIME/XML/env_mach_pes.py b/utils/python/CIME/XML/env_mach_pes.py index 39b4f71fa4b..a4f84707a81 100644 --- a/utils/python/CIME/XML/env_mach_pes.py +++ b/utils/python/CIME/XML/env_mach_pes.py @@ -5,6 +5,8 @@ from env_base import EnvBase +logger = logging.getLogger(__name__) + class EnvMachPes(EnvBase): def __init__(self, case_root=os.getcwd(), infile="env_mach_pes.xml"): """ diff --git a/utils/python/CIME/XML/env_mach_specific.py b/utils/python/CIME/XML/env_mach_specific.py index bfe2e84189a..26702db1100 100644 --- a/utils/python/CIME/XML/env_mach_specific.py +++ b/utils/python/CIME/XML/env_mach_specific.py @@ -5,6 +5,8 @@ from generic_xml import GenericXML +logger = logging.getLogger(__name__) + class EnvMachSpecific(GenericXML): def __init__(self, caseroot, infile="env_mach_specific.xml"): diff --git a/utils/python/CIME/XML/env_run.py b/utils/python/CIME/XML/env_run.py index f78dd829b3a..8819b5aea10 100644 --- a/utils/python/CIME/XML/env_run.py +++ b/utils/python/CIME/XML/env_run.py @@ -5,6 +5,8 @@ from env_base import EnvBase +logger = logging.getLogger(__name__) + class EnvRun(EnvBase): def __init__(self, case_root=os.getcwd(), infile="env_run.xml"): """ diff --git a/utils/python/CIME/XML/env_test.py b/utils/python/CIME/XML/env_test.py index 8e465b9e10f..ba66f3f9671 100644 --- a/utils/python/CIME/XML/env_test.py +++ b/utils/python/CIME/XML/env_test.py @@ -5,6 +5,8 @@ from env_base import EnvBase +logger = logging.getLogger(__name__) + class EnvTest(EnvBase): def __init__(self, case_root=os.getcwd(), infile="env_test.xml"): """ diff --git a/utils/python/CIME/XML/files.py b/utils/python/CIME/XML/files.py index e986eb2bf2c..e578c02b19b 100644 --- a/utils/python/CIME/XML/files.py +++ b/utils/python/CIME/XML/files.py @@ -6,6 +6,8 @@ from entry_id import EntryID from CIME.utils import expect, get_cime_root, get_model +logger = logging.getLogger(__name__) + class Files(EntryID): def __init__(self): """ diff --git a/utils/python/CIME/XML/generic_xml.py b/utils/python/CIME/XML/generic_xml.py index f6e6d4bcf2d..18bd3a98b8f 100644 --- a/utils/python/CIME/XML/generic_xml.py +++ b/utils/python/CIME/XML/generic_xml.py @@ -6,6 +6,8 @@ from xml.dom import minidom from CIME.utils import expect, get_cime_root +logger = logging.getLogger(__name__) + class GenericXML(object): def __init__(self, infile=None): @@ -37,14 +39,14 @@ def read(self, infile): """ Read and parse an xml file into the object """ - logging.info("read: "+infile) + logger.debug("read: "+infile) self.tree = ET.parse(infile) self.root = self.tree.getroot() if ("version" in self.root.attrib): self.version = self.root.attrib["version"] else: self.version = "1.0" - logging.info("File version is "+self.version) + logger.debug("File version is "+self.version) def write(self, outfile=None): @@ -53,7 +55,7 @@ def write(self, outfile=None): """ if(outfile is None): outfile = self.filename - logging.info("write: "+ outfile) + logger.debug("write: "+ outfile) xmlstr = ET.tostring(self.root) doc = minidom.parseString(xmlstr) with open(outfile,'w') as xmlout: @@ -101,7 +103,7 @@ def get_value(self, item,resolved=True): """ get_value is expected to be defined by the derived classes, if you get here it is an error. """ - logging.debug("Get Value for "+item) + logger.debug("Get Value for "+item) result = None if item in self.lookups.keys(): result = self.lookups[item] @@ -109,7 +111,7 @@ def get_value(self, item,resolved=True): result = os.environ.get(item) if (result is None): - logging.info("No value available for item '%s'" % item) + logger.debug("No value available for item '%s'" % item) elif(resolved): result = self.get_resolved_value(result) @@ -135,7 +137,7 @@ def get_resolved_value(self, raw_value): >>> obj.get_resolved_value("one $ENV{FOO} two $ENV{BAZ} three") 'one BAR two BARF three' """ - logging.debug("raw_value %s" % raw_value) + logger.debug("raw_value %s" % raw_value) reference_re = re.compile(r'\$(\w+)') env_ref_re = re.compile(r'\$ENV\{(\w+)\}') item_data = raw_value @@ -144,17 +146,17 @@ def get_resolved_value(self, raw_value): return None for m in env_ref_re.finditer(item_data): - logging.debug("look for "+item_data+ " in env") + logger.debug("look for "+item_data+ " in env") env_var = m.groups()[0] expect(env_var in os.environ, "Undefined env var '%s'" % env_var) item_data = item_data.replace(m.group(), os.environ[env_var]) for m in reference_re.finditer(item_data): var = m.groups()[0] - logging.debug("find: "+var) + logger.debug("find: "+var) ref = self.get_value(var) if(ref is not None): - logging.debug("resolve: "+ref) + logger.debug("resolve: "+ref) item_data = item_data.replace(m.group(), self.get_resolved_value(ref)) return item_data diff --git a/utils/python/CIME/XML/headers.py b/utils/python/CIME/XML/headers.py index 89b1fbde130..5dc9cf897af 100644 --- a/utils/python/CIME/XML/headers.py +++ b/utils/python/CIME/XML/headers.py @@ -7,6 +7,8 @@ from files import Files from CIME.utils import expect, get_cime_root, get_model +logger = logging.getLogger(__name__) + class Headers(EntryID): def __init__(self,infile=None): """ diff --git a/utils/python/CIME/XML/machines.py b/utils/python/CIME/XML/machines.py index aa078efb436..68e5b11ac45 100644 --- a/utils/python/CIME/XML/machines.py +++ b/utils/python/CIME/XML/machines.py @@ -7,6 +7,8 @@ from files import Files from CIME.utils import expect +logger = logging.getLogger(__name__) + class Machines(GenericXML): def __init__(self, infile=None, files=None, machine=None): @@ -24,7 +26,6 @@ def __init__(self, infile=None, files=None, machine=None): files = Files() infile = files.get_value("MACHINES_SPEC_FILE") - logging.info("Open file " + infile) GenericXML.__init__(self, infile) if (machine is None): @@ -67,7 +68,7 @@ def probe_machine_name(self): for node in nodes: machtocheck = node.get("MACH") - logging.debug("machine is " + machtocheck) + logger.debug("machine is " + machtocheck) self.set_machine(machtocheck) regex_str_nodes = self.get_node("NODENAME_REGEX", root=self.machine) @@ -77,15 +78,15 @@ def probe_machine_name(self): regex_str = machtocheck if (regex_str is not None): - logging.debug("machine regex string is " + regex_str) + logger.debug("machine regex string is " + regex_str) regex = re.compile(regex_str) if (regex.match(nametomatch)): - logging.info("Found machine: %s matches %s" % (machtocheck, nametomatch)) + logger.info("Found machine: %s matches %s" % (machtocheck, nametomatch)) machine = machtocheck break if (machine is None): - logging.warning("Could not probe machine for hostname '%s'" % nametomatch) + logger.warning("Could not probe machine for hostname '%s'" % nametomatch) return machine @@ -217,7 +218,7 @@ def has_batch_system(self): batch_system = self.get_node("batch_system") if (batch_system): result = (batch_system[0].get("type") != "none") - logging.debug("Machine %s has batch: %s" % (self.name, result)) + logger.debug("Machine %s has batch: %s" % (self.name, result)) return result def get_batch_system_type(self): diff --git a/utils/python/CIME/XML/testlist.py b/utils/python/CIME/XML/testlist.py index 508122a636e..3de12d2e778 100644 --- a/utils/python/CIME/XML/testlist.py +++ b/utils/python/CIME/XML/testlist.py @@ -7,6 +7,8 @@ from generic_xml import GenericXML from CIME.utils import expect, get_cime_root, get_model +logger = logging.getLogger(__name__) + class Testlist(GenericXML): def __init__(self,infile): """ @@ -81,6 +83,6 @@ def get_tests(self, machine=None, category=None, compiler=None): elif (self.version == "2.0"): return self._get_testsv2(machine,category,compiler) else: - logging.critical("Did not recognize testlist file version %s for file %s" + logger.critical("Did not recognize testlist file version %s for file %s" % (self.version,self.filename)) diff --git a/utils/python/CIME/XML/testspec.py b/utils/python/CIME/XML/testspec.py index 401a96eeac8..870b47c783f 100644 --- a/utils/python/CIME/XML/testspec.py +++ b/utils/python/CIME/XML/testspec.py @@ -8,6 +8,8 @@ _VERSION = "1.0" +logger = logging.getLogger(__name__) + class TestSpec(GenericXML): def __init__(self,infile): """ diff --git a/utils/python/CIME/case.py b/utils/python/CIME/case.py index 511253c8b4f..335f2029956 100644 --- a/utils/python/CIME/case.py +++ b/utils/python/CIME/case.py @@ -19,6 +19,8 @@ from CIME.XML.env_archive import EnvArchive from CIME.XML.env_batch import EnvBatch +logger = logging.getLogger(__name__) + class Case(object): def __init__(self, case_root=os.getcwd()): diff --git a/utils/python/CIME/env_module.py b/utils/python/CIME/env_module.py index 7aa2f22d087..904cbd7b213 100644 --- a/utils/python/CIME/env_module.py +++ b/utils/python/CIME/env_module.py @@ -8,6 +8,8 @@ from CIME.XML.machines import Machines from CIME.XML.env_mach_specific import EnvMachSpecific +logger = logging.getLogger(__name__) + class EnvModule(object): # TODO - write env_mach_specific files into case diff --git a/utils/python/CIME/system_test.py b/utils/python/CIME/system_test.py index 130bab3552d..9887e4039f3 100644 --- a/utils/python/CIME/system_test.py +++ b/utils/python/CIME/system_test.py @@ -28,6 +28,8 @@ CONTINUE = [TEST_PASS_STATUS, NAMELIST_FAIL_STATUS] ############################################################################### +logger = logging.getLogger(__name__) + class SystemTest(object): ############################################################################### @@ -352,7 +354,7 @@ def _create_newcase_phase(self, test): return False create_newcase_cmd += " -user_mods_dir %s" % test_mod_file - logging.info("Calling create_newcase: "+create_newcase_cmd) + logger.debug("Calling create_newcase: "+create_newcase_cmd) return self._shell_cmd_for_phase(test, create_newcase_cmd, CREATE_NEWCASE_PHASE) ########################################################################### @@ -364,7 +366,7 @@ def _xml_phase(self, test): files = Files() drv_config_file = files.get_value("CONFIG_DRV_FILE") - logging.info("Found drv_config_file %s" % drv_config_file) + logger.debug("Found drv_config_file %s" % drv_config_file) drv_comp = Component(drv_config_file) envtest.add_elements_by_group(drv_comp, {}, "env_test.xml") @@ -518,7 +520,7 @@ def _run_catch_exceptions(self, test, phase, run): exc_tb = sys.exc_info()[2] errput = "Test '%s' failed in phase '%s' with exception '%s'" % (test, phase, str(e)) self._log_output(test, errput) - logging.warning("Caught exception: %s" % str(e)) + logger.warning("Caught exception: %s" % str(e)) traceback.print_tb(exc_tb) return False @@ -568,7 +570,7 @@ def _handle_test_status_file(self, test, test_phase, success): # TODO: What to do here? This failure is very severe because the # only way for test results to be communicated is by the TestStatus # file. - logging.critical("VERY BAD! Could not handle TestStatus file '%s': '%s'" % + logger.critical("VERY BAD! Could not handle TestStatus file '%s': '%s'" % (os.path.join(self._get_test_dir(test), TEST_STATUS_FILENAME), str(e))) thread.interrupt_main() @@ -602,16 +604,16 @@ def _consumer(self, test, test_phase, phase_method): self._update_test_status(test, test_phase, status) self._handle_test_status_file(test, test_phase, success) - status_str = "Finished %s for test %s in %f seconds (%s)\n" %\ + status_str = "Finished %s for test %s in %f seconds (%s)" %\ (test_phase, test, elapsed_time, status) if not success: - status_str += " Case dir: %s\n" % self._get_test_dir(test) - sys.stdout.write(status_str) + status_str += " Case dir: %s" % self._get_test_dir(test) + logger.info(status_str) # On batch systems, we want to immediately submit to the queue, because # it's very cheap to submit and will get us a better spot in line if (success and not self._no_run and not self._no_batch and test_phase == MODEL_BUILD_PHASE): - sys.stdout.write("Starting %s for test %s with %d procs\n" % (RUN_PHASE, test, 1)) + logger.info("Starting %s for test %s with %d procs" % (RUN_PHASE, test, 1)) self._update_test_status(test, RUN_PHASE, TEST_PENDING_STATUS) self._consumer(test, RUN_PHASE, self._run_phase) @@ -623,7 +625,7 @@ def _producer(self): work_to_do = False num_threads_launched_this_iteration = 0 for test in self._tests: - logging.info("test_name: " + test) + logger.debug("test_name: " + test) # If we have no workers available, immediately wait if len(threads_in_flight) == self._parallel_jobs: self._wait_for_something_to_finish(threads_in_flight) @@ -640,8 +642,8 @@ def _producer(self): self._procs_avail -= procs_needed # Necessary to print this way when multiple threads printing - sys.stdout.write("Starting %s for test %s with %d procs\n" % - (next_phase, test, procs_needed)) + logger.info("Starting %s for test %s with %d procs" % + (next_phase, test, procs_needed)) self._update_test_status(test, next_phase, TEST_PENDING_STATUS) new_thread = threading.Thread(target=self._consumer, @@ -693,7 +695,7 @@ def _setup_cs_files(self): os.stat(cs_submit_file).st_mode | stat.S_IXUSR | stat.S_IXGRP) except Exception as e: - logging.warning("FAILED to set up cs files: %s" % str(e)) + logger.warning("FAILED to set up cs files: %s" % str(e)) ########################################################################### def system_test(self): @@ -706,9 +708,9 @@ def system_test(self): start_time = time.time() # Tell user what will be run - print "RUNNING TESTS:" + logger.info( "RUNNING TESTS:") for test in self._tests: - print " ", test + logger.info( " %s"% test) # TODO - documentation @@ -720,11 +722,11 @@ def system_test(self): self._setup_cs_files() # Return True if all tests passed - print "At system_test close, state is:" + logger.info( "At system_test close, state is:") rv = True for test in self._tests: phase, status, nl_fail = self._get_test_data(test) - logging.debug("phase %s status %s" % (phase, status)) + logger.debug("phase %s status %s" % (phase, status)) if status == TEST_PASS_STATUS and phase == RUN_PHASE: # Be cautious about telling the user that the test passed. This # status should match what they would see on the dashboard. Our @@ -734,18 +736,18 @@ def system_test(self): status = wait_for_tests.interpret_status_file(test_status_file)[1] if status not in [TEST_PASS_STATUS, TEST_PENDING_STATUS]: - print "%s %s (phase %s)" % (status, test, phase) + logger.info( "%s %s (phase %s)" % (status, test, phase)) rv = False elif nl_fail: - print "%s %s (but otherwise OK)" % (NAMELIST_FAIL_STATUS, test) + logger.info( "%s %s (but otherwise OK)" % (NAMELIST_FAIL_STATUS, test)) rv = False else: - print status, test, phase + logger.info("status=%s test=%s phase=%s"%( status, test, phase)) - print " Case dir: %s" % self._get_test_dir(test) + logger.info( " Case dir: %s" % self._get_test_dir(test)) - print "system_test took", time.time() - start_time, "seconds" + logger.warn( "system_test took %s seconds"% (time.time() - start_time)) return rv diff --git a/utils/python/CIME/utils.py b/utils/python/CIME/utils.py index 904859d05da..ead64aee870 100644 --- a/utils/python/CIME/utils.py +++ b/utils/python/CIME/utils.py @@ -14,6 +14,7 @@ # Return this error code if the scripts worked but tests failed TESTS_FAILED_ERR_CODE = 165 +logger = logging.getLogger(__name__) def expect(condition, error_msg): """ @@ -47,7 +48,7 @@ def _read_cime_config_file(): if(os.path.isfile(cime_config_file)): cime_config.read(cime_config_file) else: - logging.warning("File %s not found" % cime_config_file) + logger.debug("File %s not found" % cime_config_file) cime_config.add_section('main') return cime_config @@ -84,7 +85,7 @@ def get_cime_root(): assert script_absdir.endswith(get_python_libs_location_within_cime()), script_absdir cimeroot = os.path.abspath(os.path.join(script_absdir,"..","..")) cime_config.set('main','CIMEROOT',cimeroot) - logging.info( "CIMEROOT is " + cimeroot) + logger.debug( "CIMEROOT is " + cimeroot) return cimeroot def set_model(model): @@ -149,7 +150,8 @@ def run_cmd(cmd, ok_to_fail=False, input_str=None, from_dir=None, verbose=None, if (arg_stderr is _hack): arg_stderr = subprocess.PIPE - logging.info("RUN: %s" % cmd) + if (verbose or logger.level == logging.DEBUG): + logger.info("RUN: %s" % cmd) if (input_str is not None): stdin = subprocess.PIPE @@ -168,9 +170,10 @@ def run_cmd(cmd, ok_to_fail=False, input_str=None, from_dir=None, verbose=None, errput = errput.strip() if errput is not None else errput stat = proc.wait() - logging.info(" stat: %d\n" % stat) - logging.info(" output: %s\n" % output) - logging.info(" errput: %s\n" % errput) + if (verbose or logger.level == logging.DEBUG): + logger.info(" stat: %d\n" % stat) + logger.info(" output: %s\n" % output) + logger.info(" errput: %s\n" % errput) if (ok_to_fail): return stat, output, errput @@ -479,18 +482,18 @@ def get_project(): """ project = os.environ.get("PROJECT") if (project is not None): - logging.warn("project from env PROJECT "+project) + logger.info("Using project from env PROJECT "+project) return project project = os.environ.get("ACCOUNT") if (project is not None): - logging.warn("project from env ACCOUNT "+project) + logger.info("Using project from env ACCOUNT "+project) return project cime_config = get_cime_config() if (cime_config.has_option('main','PROJECT')): project = cime_config.get('main','PROJECT') if (project is not None): - logging.warn("project from .cime/config "+project) + logger.info("Using project from .cime/config "+project) return project projectfile = os.path.abspath(os.path.join(os.path.expanduser("~"), ".cesm_proj")) @@ -504,18 +507,59 @@ def get_project(): def setup_standard_logging_options(parser): parser.add_argument("-v", "--verbose", action="store_true", help="Print extra information") - parser.add_argument("-d", "--debug", action="store_true", help="Print debug information (very verbose)") + parser.add_argument("-s", "--silent", action="store_true", + help="Print only warnings and error messages") def handle_standard_logging_options(args): - root_logger = logging.getLogger() - - if (args.verbose == True): - root_logger.setLevel(logging.INFO) - # DEBUG trumps INFO + LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'propagate': True, + 'formatters': { + 'verbose': { + 'format': '%(name)-12s %(levelname)-8s %(message)s', + }, + 'debug': { + 'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s', + }, + 'default':{ + 'format': '%(message)s', + }, + }, + 'handlers': { + 'console1': { + 'class': 'logging.StreamHandler', + 'formatter': 'default', + 'level' : logging.INFO, + }, + 'file1' : { + 'class': 'logging.FileHandler', + 'mode':'w', + 'formatter':'debug', + 'filename' : '%s.log'%sys.argv[0], + 'level' : logging.DEBUG, + }, + }, + 'root': { + 'handlers': ['console1'], + } + } + root_logger=logging.getLogger() + # DEBUG trumps INFO trumps Silent (WARN) if (args.debug == True): + LOGGING['root']['handlers'].append('file1') + LOGGING['handlers']['console1']['formatter'] = 'debug' root_logger.setLevel(logging.DEBUG) + logger.warn("Log level set to DEBUG") + elif (args.verbose == True): + LOGGING['handlers']['console1']['formatter'] = 'verbose' + logger.warn("Log level set to VERBOSE") + elif (args.silent == True): + root_logger.setLevel(logging.WARN) + + logging.config.dictConfig(LOGGING) def get_logging_options(): """ @@ -528,5 +572,7 @@ def get_logging_options(): return "--verbose" elif (root_logger.level == logging.DEBUG): return "--debug" + elif (root_logger.level == logging.WARN): + return "--silent" else: return ""