Add the PYSAWSIM_LOG_LEVEL environmental variable.
[sawsim.git] / pysawsim / __init__.py
index 9cb3a97a5311b8ccc2f6dc31c730c27e4c0e6f39..19cfcba86e65b2e71da07ac3525fa11f1b491f33 100644 (file)
@@ -47,7 +47,8 @@ Just go crazy with doctests and unittests; nose_ will find them.
 
 import logging
 import logging.handlers
-import sys
+import sys as _sys
+import os as _os
 
 
 _multiprocess_shared_ = True
@@ -59,10 +60,24 @@ This module cannot be split because _log setup is not re-entrant.
 __version__ = '0.10'  # match sawsim version
 
 
+PYSAWSIM_LOG_LEVEL_MSG = """
+You can control the log verbosity with the `PYSAWSIM_LOG_LEVEL`
+environmental variable.  Set it to one of the level names in Python's
+`logging` module (e.g. `DEBUG`).
+"""
+
 def log():
     return logging.getLogger('pysawsim')
 
-def add_stderr_log_handler(level=logging.DEBUG):
+def add_stderr_log_handler(level=None):
+    if level == None:
+        level_string = _os.environ.get('PYSAWSIM_LOG_LEVEL', 'WARNING')
+        try:
+            level = getattr(logging, level_string)
+        except AttributeError:
+            _sys.stderr.write(
+                'unrecognized PYSAWSIM_LOG_LEVEL: %s\n' % level_string)
+            raise
     _log = log()
     _log.setLevel(level)
     console = logging.StreamHandler()
@@ -70,4 +85,4 @@ def add_stderr_log_handler(level=logging.DEBUG):
     console.setFormatter(formatter)
     _log.addHandler(console)
 
-add_stderr_log_handler(logging.WARNING)
+add_stderr_log_handler()