Repository: reata/sqllineage

Issue:
Replace print to stderr with logging

Discussion:

Patch:
diff --git a/docs/conf.py b/docs/conf.py
index 6123867..6bfaa58 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -21,14 +21,14 @@ import sys
 
 sys.path.insert(0, os.path.abspath(".."))
 
-from sqllineage import name, version  # noqa
+from sqllineage import NAME, VERSION  # noqa
 
-project = name
+project = NAME
 copyright = "2019, Reata"  # noqa
 author = "Reata"
 
 # The full version, including alpha/beta/rc tags
-release = version
+release = VERSION
 
 # -- General configuration ---------------------------------------------------
 
diff --git a/setup.py b/setup.py
index 7e0720c..ff627bb 100644
--- a/setup.py
+++ b/setup.py
@@ -1,6 +1,6 @@
 from setuptools import find_packages, setup
 
-from sqllineage import name, version
+from sqllineage import NAME, VERSION
 
 with open("README.md", "r") as f:
     long_description = f.read()
@@ -21,8 +21,8 @@ ci_requires = [
 ]
 
 setup(
-    name=name,
-    version=version,
+    name=NAME,
+    version=VERSION,
     author="Reata",
     author_email="reddevil.hjw@gmail.com",
     description="SQL Lineage Analysis Tool powered by Python",
diff --git a/sqllineage/__init__.py b/sqllineage/__init__.py
index 896ffc8..e969c0e 100644
--- a/sqllineage/__init__.py
+++ b/sqllineage/__init__.py
@@ -1,2 +1,25 @@
-name = "sqllineage"
-version = "0.3.0"
+import logging.config
+
+NAME = "sqllineage"
+VERSION = "0.3.0"
+DEFAULT_LOGGING = {
+    "version": 1,
+    "disable_existing_loggers": False,
+    "formatters": {"default": {"format": "%(levelname)s: %(message)s"}},
+    "handlers": {
+        "console": {
+            "level": "WARNING",
+            "class": "logging.StreamHandler",
+            "formatter": "default",
+        }
+    },
+    "loggers": {
+        "": {
+            "handlers": ["console"],
+            "level": "WARNING",
+            "propagate": False,
+            "filters": [],
+        }
+    },
+}
+logging.config.dictConfig(DEFAULT_LOGGING)
diff --git a/sqllineage/runner.py b/sqllineage/runner.py
index d8fe8c2..abe8280 100644
--- a/sqllineage/runner.py
+++ b/sqllineage/runner.py
@@ -1,6 +1,6 @@
 import argparse
 import importlib
-import sys
+import logging
 from typing import List, Type
 
 import sqlparse
@@ -11,6 +11,9 @@ from sqllineage.core import LineageAnalyzer
 from sqllineage.models import Table
 
 
+logger = logging.getLogger(__name__)
+
+
 class LineageRunner(object):
     def __init__(
         self,
@@ -111,20 +114,16 @@ def main(args=None) -> None:
             combiner_module, combiner_name = args.c.rsplit(".", maxsplit=1)
             combiner = getattr(importlib.import_module(combiner_module), combiner_name)
         except ValueError:
-            print(
-                "ERROR: Combiner should be named as module.class, got {} instead".format(
-                    args.c
-                ),
-                file=sys.stderr,
+            logger.exception(
+                "Combiner should be named as module.class, got %s instead", args.c
             )
-            raise
+            exit(1)
         except (ImportError, AttributeError):
-            print("ERROR: No such combiner: {}".format(args.c), file=sys.stderr)
-            raise
+            logger.exception("No such combiner: %s", args.c)
+            exit(1)
     if args.e and args.f:
-        print(
-            "WARNING: Both -e and -f options are specified. -e option will be ignored",
-            file=sys.stderr,
+        logging.warning(
+            "Both -e and -f options are specified. -e option will be ignored"
         )
     if args.f:
         try:
@@ -132,19 +131,14 @@ def main(args=None) -> None:
                 sql = f.read()
             print(LineageRunner(sql, combiner=combiner, verbose=args.verbose))
         except IsADirectoryError:
-            print(
-                "ERROR: {} is a directory".format(args.f), file=sys.stderr,
-            )
-            raise
+            logger.exception("%s is a directory", args.f)
+            exit(1)
         except FileNotFoundError:
-            print("ERROR: No such file: {}".format(args.f), file=sys.stderr)
-            raise
+            logger.exception("No such file: %s", args.f)
+            exit(1)
         except PermissionError:
-            print(
-                "ERROR: Permission denied when reading file '{}'".format(args.f),
-                file=sys.stderr,
-            )
-            raise
+            logger.exception("Permission denied when reading file '%s'", args.f)
+            exit(1)
     elif args.e:
         print(LineageRunner(args.e, combiner=combiner, verbose=args.verbose))
     else:


Test patch:
diff --git a/tests/test_runner.py b/tests/test_runner.py
index 388105e..08e14d5 100644
--- a/tests/test_runner.py
+++ b/tests/test_runner.py
@@ -20,16 +20,18 @@ insert overwrite table tab3 select * from tab2""",
 
 
 def test_combiner_exception():
-    with pytest.raises(ValueError):
-        main(["-c", "malformatcombiner"])
-    with pytest.raises(ImportError):
-        main(["-c", "nonexist_package.nonexist_combiner"])
-    with pytest.raises(AttributeError):
-        main(["-c", "sqllineage.combiners.nonexist_combiner"])
+    for args in (
+        ["-c", "malformatcombiner"],
+        ["-c", "nonexist_package.nonexist_combiner"],
+        ["-c", "sqllineage.combiners.nonexist_combiner"],
+    ):
+        with pytest.raises(SystemExit) as e:
+            main(args)
+        assert e.value.code == 1
 
 
 def test_file_exception():
-    with pytest.raises(IsADirectoryError):
-        main(["-f", str(pathlib.Path().absolute())])
-    with pytest.raises(FileNotFoundError):
-        main(["-f", "nonexist_file"])
+    for args in (["-f", str(pathlib.Path().absolute())], ["-f", "nonexist_file"]):
+        with pytest.raises(SystemExit) as e:
+            main(args)
+        assert e.value.code == 1