Repository: pytorch/TensorRT

Issue:
✨[Feature] Enable debug logging with a context 
**Is your feature request related to a problem? Please describe.**
<!--A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]-->

Right now seems like users don't know how or don't enable debug logging. We can probably add some syntax to make this easier.

**Describe the solution you'd like**
<!--A clear and concise description of what you want to happen.-->

I would love to see something like: 

```py
import torch_tensorrt as torchtrt 

with torchtrt.debug:
    torchtrt.ts.compile(....)

```

under the hood this would be equivalent to:

 ```py
import torch_tensorrt as torchtrt 

torchtrt.logging.set_reportable_log_level(torchtrt.logging.Level.Debug)
torchtrt.ts.compile(....)
torchtrt.logging.set_reportable_log_level(torchtrt.logging.Level.Error)
```

**Describe alternatives you've considered**
<!--A clear and concise description of any alternative solutions or features you've considered.-->

**Additional context**
<!--Add any other context or screenshots about the feature request here.-->



Discussion:
In some other libraries, I've seen python functions accepting verbose=True as argument. Maybe one other option is to have 
torchtrt.ts.compile(....,, log_level = 'debug') defaulting it to info. 
Agreed with the above... as `compile` is the primary entry point into torchtrt for most use cases, it would be highly ergonomic to enable verbosity through the same interface via an optional argument.
Yeah I would agree except that, the logger covers not just the compilation step but also the execution step. Especially for the execution step there isn't a great access point to provide this argument. No reason we cant look at adding both.

Resolving patch:
diff --git a/py/torch_tensorrt/logging.py b/py/torch_tensorrt/logging.py
--- a/py/torch_tensorrt/logging.py
+++ b/py/torch_tensorrt/logging.py
@@ -96,3 +96,113 @@ def log(level: Level, msg: str):
         msg (str): Actual message text
     """
     _log(Level._to_internal_level(level), msg)
+
+    InternalError = LogLevel.INTERNAL_ERROR
+    Error = LogLevel.ERROR
+    Warning = LogLevel.WARNING
+    Info = LogLevel.INFO
+    Debug = LogLevel.DEBUG
+    Graph = LogLevel.GRAPH
+
+
+class internal_errors:
+    """Context-manager to limit displayed log messages to just internal errors
+
+    Example::
+
+    with torch_tensorrt.logging.internal_errors():
+        outputs = model_torchtrt(inputs)
+    """
+
+    def __enter__(self):
+        self.external_lvl = get_reportable_log_level()
+        set_reportable_log_level(Level.InternalError)
+
+    def __exit__(self, exc_type, exc_value, exc_tb):
+        set_reportable_log_level(self.external_lvl)
+
+
+class errors:
+    """Context-manager to limit displayed log messages to just errors and above
+
+    Example::
+
+    with torch_tensorrt.logging.errors():
+        outputs = model_torchtrt(inputs)
+    """
+
+    def __enter__(self):
+        self.external_lvl = get_reportable_log_level()
+        set_reportable_log_level(Level.Error)
+
+    def __exit__(self, exc_type, exc_value, exc_tb):
+        set_reportable_log_level(self.external_lvl)
+
+
+class warnings:
+    """Context-manager to limit displayed log messages to just warnings and above
+
+    Example::
+
+    with torch_tensorrt.logging.warnings():
+        model_trt = torch_tensorrt.compile(model, **spec)
+    """
+
+    def __enter__(self):
+        self.external_lvl = get_reportable_log_level()
+        set_reportable_log_level(Level.Warning)
+
+    def __exit__(self, exc_type, exc_value, exc_tb):
+        set_reportable_log_level(self.external_lvl)
+
+
+class info:
+    """Context-manager to display all info and greater severity messages
+
+    Example::
+
+    with torch_tensorrt.logging.info():
+        model_trt = torch_tensorrt.compile(model, **spec)
+    """
+
+    def __enter__(self):
+        self.external_lvl = get_reportable_log_level()
+        set_reportable_log_level(Level.Info)
+
+    def __exit__(self, exc_type, exc_value, exc_tb):
+        set_reportable_log_level(self.external_lvl)
+
+
+class debug:
+    """Context-manager to display full debug information through the logger
+
+    Example::
+
+    with torch_tensorrt.logging.debug():
+        model_trt = torch_tensorrt.compile(model, **spec)
+    """
+
+    def __enter__(self):
+        self.external_lvl = get_reportable_log_level()
+        set_reportable_log_level(Level.Debug)
+
+    def __exit__(self, exc_type, exc_value, exc_tb):
+        set_reportable_log_level(self.external_lvl)
+
+
+class graphs:
+    """Context-manager to display the results of intermediate lowering passes
+    as well as full debug information through the logger
+
+    Example::
+
+    with torch_tensorrt.logging.graphs():
+        model_trt = torch_tensorrt.compile(model, **spec)
+    """
+
+    def __enter__(self):
+        self.external_lvl = get_reportable_log_level()
+        set_reportable_log_level(Level.Graph)
+
+    def __exit__(self, exc_type, exc_value, exc_tb):
+        set_reportable_log_level(self.external_lvl)


Test patch:
diff --git a/tests/py/test_api.py b/tests/py/test_api.py
--- a/tests/py/test_api.py
+++ b/tests/py/test_api.py
@@ -323,6 +323,50 @@ def test_is_colored_output_on(self):
         color = torchtrt.logging.get_is_colored_output_on()
         self.assertTrue(color)
 
+    def test_context_managers(self):
+        base_lvl = torchtrt.logging.get_reportable_log_level()
+        with torchtrt.logging.internal_errors():
+            lvl = torchtrt.logging.get_reportable_log_level()
+            self.assertEqual(torchtrt.logging.Level.InternalError, lvl)
+
+        lvl = torchtrt.logging.get_reportable_log_level()
+        self.assertEqual(base_lvl, lvl)
+
+        with torchtrt.logging.errors():
+            lvl = torchtrt.logging.get_reportable_log_level()
+            self.assertEqual(torchtrt.logging.Level.Error, lvl)
+
+        lvl = torchtrt.logging.get_reportable_log_level()
+        self.assertEqual(base_lvl, lvl)
+
+        with torchtrt.logging.warnings():
+            lvl = torchtrt.logging.get_reportable_log_level()
+            self.assertEqual(torchtrt.logging.Level.Warning, lvl)
+
+        lvl = torchtrt.logging.get_reportable_log_level()
+        self.assertEqual(base_lvl, lvl)
+
+        with torchtrt.logging.info():
+            lvl = torchtrt.logging.get_reportable_log_level()
+            self.assertEqual(torchtrt.logging.Level.Info, lvl)
+
+        lvl = torchtrt.logging.get_reportable_log_level()
+        self.assertEqual(base_lvl, lvl)
+
+        with torchtrt.logging.debug():
+            lvl = torchtrt.logging.get_reportable_log_level()
+            self.assertEqual(torchtrt.logging.Level.Debug, lvl)
+
+        lvl = torchtrt.logging.get_reportable_log_level()
+        self.assertEqual(base_lvl, lvl)
+
+        with torchtrt.logging.graphs():
+            lvl = torchtrt.logging.get_reportable_log_level()
+            self.assertEqual(torchtrt.logging.Level.Graph, lvl)
+
+        lvl = torchtrt.logging.get_reportable_log_level()
+        self.assertEqual(base_lvl, lvl)
+
 
 class TestDevice(unittest.TestCase):