prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN 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.-->
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `py/torch_tensorrt/logging.py`
  Content:
  ```
  1 from enum import Enum
  2 from torch_tensorrt._C import _get_logging_prefix, _set_logging_prefix, \
  3                               _get_reportable_log_level, _set_reportable_log_level, \
  4                               _get_is_colored_output_on,  _set_is_colored_output_on, \
  5                                _log, LogLevel
  6 
  7 
  8 class Level(Enum):
  9     """Enum to set the minimum required logging level to print a message to stdout
  10     """
  11     InternalError = LogLevel.INTERNAL_ERROR
  12     Error = LogLevel.ERROR
  13     Warning = LogLevel.WARNING
  14     Info = LogLevel.INFO
  15     Debug = LogLevel.DEBUG
  16     Graph = LogLevel.GRAPH
  17 
  18     @staticmethod
  19     def _to_internal_level(external) -> LogLevel:
  20         if external == Level.InternalError:
  21             return LogLevel.INTERNAL_ERROR
  22         if external == Level.Error:
  23             return LogLevel.ERROR
  24         if external == Level.Warning:
  25             return LogLevel.WARNING
  26         if external == Level.Info:
  27             return LogLevel.INFO
  28         if external == Level.Debug:
  29             return LogLevel.DEBUG
  30         if external == Level.Graph:
  31             return LogLevel.GRAPH
  32 
  33 
  34 def get_logging_prefix() -> str:
  35     """Get the prefix set for logging messages
  36 
  37     Returns:
  38         str: Prefix used for logger
  39     """
  40     return _get_logging_prefix()
  41 
  42 
  43 def set_logging_prefix(prefix: str):
  44     """Set the prefix used when logging messages
  45 
  46     Args:
  47         prefix (str): Prefix to use for logging messages
  48     """
  49     _set_logging_prefix(prefix)
  50 
  51 
  52 def get_reportable_log_level() -> Level:
  53     """Get the level required for a message to be printed in the log
  54 
  55     Returns:
  56         torch_tensorrt.logging.Level: The enum representing the level required to print
  57     """
  58     return Level(_get_reportable_log_level())
  59 
  60 
  61 def set_reportable_log_level(level: Level):
  62     """Set the level required for a message to be printed to the log
  63 
  64     Args:
  65         level (torch_tensorrt.logging.Level): The enum representing the level required to print
  66     """
  67     _set_reportable_log_level(Level._to_internal_level(level))
  68 
  69 
  70 def get_is_colored_output_on() -> bool:
  71     """Get if colored output is enabled for logging
  72 
  73     Returns:
  74         bool: If colored output is one
  75     """
  76     return _get_is_colored_output_on()
  77 
  78 
  79 def set_is_colored_output_on(colored_output_on: bool):
  80     """Enable or disable color in the log output
  81 
  82     Args:
  83         colored_output_on (bool): If colored output should be enabled or not
  84     """
  85     _set_is_colored_output_on(colored_output_on)
  86 
  87 
  88 def log(level: Level, msg: str):
  89     """Add a new message to the log
  90 
  91     Adds a new message to the log at a specified level. The message
  92     will only get printed out if Level > reportable_log_level
  93 
  94     Args:
  95         level (torch_tensorrt.logging.Level): Severity of the message
  96         msg (str): Actual message text
  97     """
  98     _log(Level._to_internal_level(level), msg)
  99
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: 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 @@
           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)
  
verification_info: {"golden_diff": "diff --git a/py/torch_tensorrt/logging.py b/py/torch_tensorrt/logging.py\n--- a/py/torch_tensorrt/logging.py\n+++ b/py/torch_tensorrt/logging.py\n@@ -96,3 +96,113 @@\n         msg (str): Actual message text\n     \"\"\"\n     _log(Level._to_internal_level(level), msg)\n+\n+    InternalError = LogLevel.INTERNAL_ERROR\n+    Error = LogLevel.ERROR\n+    Warning = LogLevel.WARNING\n+    Info = LogLevel.INFO\n+    Debug = LogLevel.DEBUG\n+    Graph = LogLevel.GRAPH\n+\n+\n+class internal_errors:\n+    \"\"\"Context-manager to limit displayed log messages to just internal errors\n+\n+    Example::\n+\n+    with torch_tensorrt.logging.internal_errors():\n+        outputs = model_torchtrt(inputs)\n+    \"\"\"\n+\n+    def __enter__(self):\n+        self.external_lvl = get_reportable_log_level()\n+        set_reportable_log_level(Level.InternalError)\n+\n+    def __exit__(self, exc_type, exc_value, exc_tb):\n+        set_reportable_log_level(self.external_lvl)\n+\n+\n+class errors:\n+    \"\"\"Context-manager to limit displayed log messages to just errors and above\n+\n+    Example::\n+\n+    with torch_tensorrt.logging.errors():\n+        outputs = model_torchtrt(inputs)\n+    \"\"\"\n+\n+    def __enter__(self):\n+        self.external_lvl = get_reportable_log_level()\n+        set_reportable_log_level(Level.Error)\n+\n+    def __exit__(self, exc_type, exc_value, exc_tb):\n+        set_reportable_log_level(self.external_lvl)\n+\n+\n+class warnings:\n+    \"\"\"Context-manager to limit displayed log messages to just warnings and above\n+\n+    Example::\n+\n+    with torch_tensorrt.logging.warnings():\n+        model_trt = torch_tensorrt.compile(model, **spec)\n+    \"\"\"\n+\n+    def __enter__(self):\n+        self.external_lvl = get_reportable_log_level()\n+        set_reportable_log_level(Level.Warning)\n+\n+    def __exit__(self, exc_type, exc_value, exc_tb):\n+        set_reportable_log_level(self.external_lvl)\n+\n+\n+class info:\n+    \"\"\"Context-manager to display all info and greater severity messages\n+\n+    Example::\n+\n+    with torch_tensorrt.logging.info():\n+        model_trt = torch_tensorrt.compile(model, **spec)\n+    \"\"\"\n+\n+    def __enter__(self):\n+        self.external_lvl = get_reportable_log_level()\n+        set_reportable_log_level(Level.Info)\n+\n+    def __exit__(self, exc_type, exc_value, exc_tb):\n+        set_reportable_log_level(self.external_lvl)\n+\n+\n+class debug:\n+    \"\"\"Context-manager to display full debug information through the logger\n+\n+    Example::\n+\n+    with torch_tensorrt.logging.debug():\n+        model_trt = torch_tensorrt.compile(model, **spec)\n+    \"\"\"\n+\n+    def __enter__(self):\n+        self.external_lvl = get_reportable_log_level()\n+        set_reportable_log_level(Level.Debug)\n+\n+    def __exit__(self, exc_type, exc_value, exc_tb):\n+        set_reportable_log_level(self.external_lvl)\n+\n+\n+class graphs:\n+    \"\"\"Context-manager to display the results of intermediate lowering passes\n+    as well as full debug information through the logger\n+\n+    Example::\n+\n+    with torch_tensorrt.logging.graphs():\n+        model_trt = torch_tensorrt.compile(model, **spec)\n+    \"\"\"\n+\n+    def __enter__(self):\n+        self.external_lvl = get_reportable_log_level()\n+        set_reportable_log_level(Level.Graph)\n+\n+    def __exit__(self, exc_type, exc_value, exc_tb):\n+        set_reportable_log_level(self.external_lvl)\n", "issue": "\u2728[Feature] Enable debug logging with a context \n**Is your feature request related to a problem? Please describe.**\r\n<!--A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]-->\r\n\r\nRight now seems like users don't know how or don't enable debug logging. We can probably add some syntax to make this easier.\r\n\r\n**Describe the solution you'd like**\r\n<!--A clear and concise description of what you want to happen.-->\r\n\r\nI would love to see something like: \r\n\r\n```py\r\nimport torch_tensorrt as torchtrt \r\n\r\nwith torchtrt.debug:\r\n    torchtrt.ts.compile(....)\r\n\r\n```\r\n\r\nunder the hood this would be equivalent to:\r\n\r\n ```py\r\nimport torch_tensorrt as torchtrt \r\n\r\ntorchtrt.logging.set_reportable_log_level(torchtrt.logging.Level.Debug)\r\ntorchtrt.ts.compile(....)\r\ntorchtrt.logging.set_reportable_log_level(torchtrt.logging.Level.Error)\r\n```\r\n\r\n**Describe alternatives you've considered**\r\n<!--A clear and concise description of any alternative solutions or features you've considered.-->\r\n\r\n**Additional context**\r\n<!--Add any other context or screenshots about the feature request here.-->\r\n\n", "before_files": [{"content": "from enum import Enum\nfrom torch_tensorrt._C import _get_logging_prefix, _set_logging_prefix, \\\n                              _get_reportable_log_level, _set_reportable_log_level, \\\n                              _get_is_colored_output_on,  _set_is_colored_output_on, \\\n                               _log, LogLevel\n\n\nclass Level(Enum):\n    \"\"\"Enum to set the minimum required logging level to print a message to stdout\n    \"\"\"\n    InternalError = LogLevel.INTERNAL_ERROR\n    Error = LogLevel.ERROR\n    Warning = LogLevel.WARNING\n    Info = LogLevel.INFO\n    Debug = LogLevel.DEBUG\n    Graph = LogLevel.GRAPH\n\n    @staticmethod\n    def _to_internal_level(external) -> LogLevel:\n        if external == Level.InternalError:\n            return LogLevel.INTERNAL_ERROR\n        if external == Level.Error:\n            return LogLevel.ERROR\n        if external == Level.Warning:\n            return LogLevel.WARNING\n        if external == Level.Info:\n            return LogLevel.INFO\n        if external == Level.Debug:\n            return LogLevel.DEBUG\n        if external == Level.Graph:\n            return LogLevel.GRAPH\n\n\ndef get_logging_prefix() -> str:\n    \"\"\"Get the prefix set for logging messages\n\n    Returns:\n        str: Prefix used for logger\n    \"\"\"\n    return _get_logging_prefix()\n\n\ndef set_logging_prefix(prefix: str):\n    \"\"\"Set the prefix used when logging messages\n\n    Args:\n        prefix (str): Prefix to use for logging messages\n    \"\"\"\n    _set_logging_prefix(prefix)\n\n\ndef get_reportable_log_level() -> Level:\n    \"\"\"Get the level required for a message to be printed in the log\n\n    Returns:\n        torch_tensorrt.logging.Level: The enum representing the level required to print\n    \"\"\"\n    return Level(_get_reportable_log_level())\n\n\ndef set_reportable_log_level(level: Level):\n    \"\"\"Set the level required for a message to be printed to the log\n\n    Args:\n        level (torch_tensorrt.logging.Level): The enum representing the level required to print\n    \"\"\"\n    _set_reportable_log_level(Level._to_internal_level(level))\n\n\ndef get_is_colored_output_on() -> bool:\n    \"\"\"Get if colored output is enabled for logging\n\n    Returns:\n        bool: If colored output is one\n    \"\"\"\n    return _get_is_colored_output_on()\n\n\ndef set_is_colored_output_on(colored_output_on: bool):\n    \"\"\"Enable or disable color in the log output\n\n    Args:\n        colored_output_on (bool): If colored output should be enabled or not\n    \"\"\"\n    _set_is_colored_output_on(colored_output_on)\n\n\ndef log(level: Level, msg: str):\n    \"\"\"Add a new message to the log\n\n    Adds a new message to the log at a specified level. The message\n    will only get printed out if Level > reportable_log_level\n\n    Args:\n        level (torch_tensorrt.logging.Level): Severity of the message\n        msg (str): Actual message text\n    \"\"\"\n    _log(Level._to_internal_level(level), msg)\n", "path": "py/torch_tensorrt/logging.py"}], "after_files": [{"content": "from enum import Enum\nfrom torch_tensorrt._C import _get_logging_prefix, _set_logging_prefix, \\\n                              _get_reportable_log_level, _set_reportable_log_level, \\\n                              _get_is_colored_output_on,  _set_is_colored_output_on, \\\n                               _log, LogLevel\n\n\nclass Level(Enum):\n    \"\"\"Enum to set the minimum required logging level to print a message to stdout\n    \"\"\"\n    InternalError = LogLevel.INTERNAL_ERROR\n    Error = LogLevel.ERROR\n    Warning = LogLevel.WARNING\n    Info = LogLevel.INFO\n    Debug = LogLevel.DEBUG\n    Graph = LogLevel.GRAPH\n\n    @staticmethod\n    def _to_internal_level(external) -> LogLevel:\n        if external == Level.InternalError:\n            return LogLevel.INTERNAL_ERROR\n        if external == Level.Error:\n            return LogLevel.ERROR\n        if external == Level.Warning:\n            return LogLevel.WARNING\n        if external == Level.Info:\n            return LogLevel.INFO\n        if external == Level.Debug:\n            return LogLevel.DEBUG\n        if external == Level.Graph:\n            return LogLevel.GRAPH\n\n\ndef get_logging_prefix() -> str:\n    \"\"\"Get the prefix set for logging messages\n\n    Returns:\n        str: Prefix used for logger\n    \"\"\"\n    return _get_logging_prefix()\n\n\ndef set_logging_prefix(prefix: str):\n    \"\"\"Set the prefix used when logging messages\n\n    Args:\n        prefix (str): Prefix to use for logging messages\n    \"\"\"\n    _set_logging_prefix(prefix)\n\n\ndef get_reportable_log_level() -> Level:\n    \"\"\"Get the level required for a message to be printed in the log\n\n    Returns:\n        torch_tensorrt.logging.Level: The enum representing the level required to print\n    \"\"\"\n    return Level(_get_reportable_log_level())\n\n\ndef set_reportable_log_level(level: Level):\n    \"\"\"Set the level required for a message to be printed to the log\n\n    Args:\n        level (torch_tensorrt.logging.Level): The enum representing the level required to print\n    \"\"\"\n    _set_reportable_log_level(Level._to_internal_level(level))\n\n\ndef get_is_colored_output_on() -> bool:\n    \"\"\"Get if colored output is enabled for logging\n\n    Returns:\n        bool: If colored output is one\n    \"\"\"\n    return _get_is_colored_output_on()\n\n\ndef set_is_colored_output_on(colored_output_on: bool):\n    \"\"\"Enable or disable color in the log output\n\n    Args:\n        colored_output_on (bool): If colored output should be enabled or not\n    \"\"\"\n    _set_is_colored_output_on(colored_output_on)\n\n\ndef log(level: Level, msg: str):\n    \"\"\"Add a new message to the log\n\n    Adds a new message to the log at a specified level. The message\n    will only get printed out if Level > reportable_log_level\n\n    Args:\n        level (torch_tensorrt.logging.Level): Severity of the message\n        msg (str): Actual message text\n    \"\"\"\n    _log(Level._to_internal_level(level), msg)\n\n    InternalError = LogLevel.INTERNAL_ERROR\n    Error = LogLevel.ERROR\n    Warning = LogLevel.WARNING\n    Info = LogLevel.INFO\n    Debug = LogLevel.DEBUG\n    Graph = LogLevel.GRAPH\n\n\nclass internal_errors:\n    \"\"\"Context-manager to limit displayed log messages to just internal errors\n\n    Example::\n\n    with torch_tensorrt.logging.internal_errors():\n        outputs = model_torchtrt(inputs)\n    \"\"\"\n\n    def __enter__(self):\n        self.external_lvl = get_reportable_log_level()\n        set_reportable_log_level(Level.InternalError)\n\n    def __exit__(self, exc_type, exc_value, exc_tb):\n        set_reportable_log_level(self.external_lvl)\n\n\nclass errors:\n    \"\"\"Context-manager to limit displayed log messages to just errors and above\n\n    Example::\n\n    with torch_tensorrt.logging.errors():\n        outputs = model_torchtrt(inputs)\n    \"\"\"\n\n    def __enter__(self):\n        self.external_lvl = get_reportable_log_level()\n        set_reportable_log_level(Level.Error)\n\n    def __exit__(self, exc_type, exc_value, exc_tb):\n        set_reportable_log_level(self.external_lvl)\n\n\nclass warnings:\n    \"\"\"Context-manager to limit displayed log messages to just warnings and above\n\n    Example::\n\n    with torch_tensorrt.logging.warnings():\n        model_trt = torch_tensorrt.compile(model, **spec)\n    \"\"\"\n\n    def __enter__(self):\n        self.external_lvl = get_reportable_log_level()\n        set_reportable_log_level(Level.Warning)\n\n    def __exit__(self, exc_type, exc_value, exc_tb):\n        set_reportable_log_level(self.external_lvl)\n\n\nclass info:\n    \"\"\"Context-manager to display all info and greater severity messages\n\n    Example::\n\n    with torch_tensorrt.logging.info():\n        model_trt = torch_tensorrt.compile(model, **spec)\n    \"\"\"\n\n    def __enter__(self):\n        self.external_lvl = get_reportable_log_level()\n        set_reportable_log_level(Level.Info)\n\n    def __exit__(self, exc_type, exc_value, exc_tb):\n        set_reportable_log_level(self.external_lvl)\n\n\nclass debug:\n    \"\"\"Context-manager to display full debug information through the logger\n\n    Example::\n\n    with torch_tensorrt.logging.debug():\n        model_trt = torch_tensorrt.compile(model, **spec)\n    \"\"\"\n\n    def __enter__(self):\n        self.external_lvl = get_reportable_log_level()\n        set_reportable_log_level(Level.Debug)\n\n    def __exit__(self, exc_type, exc_value, exc_tb):\n        set_reportable_log_level(self.external_lvl)\n\n\nclass graphs:\n    \"\"\"Context-manager to display the results of intermediate lowering passes\n    as well as full debug information through the logger\n\n    Example::\n\n    with torch_tensorrt.logging.graphs():\n        model_trt = torch_tensorrt.compile(model, **spec)\n    \"\"\"\n\n    def __enter__(self):\n        self.external_lvl = get_reportable_log_level()\n        set_reportable_log_level(Level.Graph)\n\n    def __exit__(self, exc_type, exc_value, exc_tb):\n        set_reportable_log_level(self.external_lvl)\n", "path": "py/torch_tensorrt/logging.py"}]}