Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Python Decorators: Custom Logging for Function Calls
Forum Updated to NodeBB v4.3 + New Features

Python Decorators: Custom Logging for Function Calls

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 314 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sachin Bhatt
    wrote on 7 Sept 2023, 05:38 last edited by
    #1

    I'm working on a Python project where I have multiple functions, and I want to log each function call along with its arguments and return value for debugging purposes. I've heard that decorators can help achieve this. Could someone guide me on how to create a custom decorator for logging function calls?

    Here's what I have in mind:

    def log_function_call(func):
        def wrapper(*args, **kwargs):
            # Log function call, arguments, and return value
            result = func(*args, **kwargs)
            # Log the result
            return result
        return wrapper
    
    @log_function_call
    def add(a, b):
        return a + b
    
    @log_function_call
    def subtract(a, b):
        return a - b
    
    # Example function calls
    result1 = add(5, 3)
    result2 = subtract(10, 4)
    
    

    I'd like to log the function calls and their results in a clean and organized manner. How can I modify the log_function_call decorator to achieve this? Additionally, what's the best practice for configuring the logger in Python to capture these logs effectively? Any code examples or recommendations would be greatly appreciated. Thank you!

    J 1 Reply Last reply 7 Sept 2023, 06:45
    0
    • S Sachin Bhatt
      7 Sept 2023, 05:38

      I'm working on a Python project where I have multiple functions, and I want to log each function call along with its arguments and return value for debugging purposes. I've heard that decorators can help achieve this. Could someone guide me on how to create a custom decorator for logging function calls?

      Here's what I have in mind:

      def log_function_call(func):
          def wrapper(*args, **kwargs):
              # Log function call, arguments, and return value
              result = func(*args, **kwargs)
              # Log the result
              return result
          return wrapper
      
      @log_function_call
      def add(a, b):
          return a + b
      
      @log_function_call
      def subtract(a, b):
          return a - b
      
      # Example function calls
      result1 = add(5, 3)
      result2 = subtract(10, 4)
      
      

      I'd like to log the function calls and their results in a clean and organized manner. How can I modify the log_function_call decorator to achieve this? Additionally, what's the best practice for configuring the logger in Python to capture these logs effectively? Any code examples or recommendations would be greatly appreciated. Thank you!

      J Offline
      J Offline
      JonB
      wrote on 7 Sept 2023, 06:45 last edited by
      #2

      @Sachin-Bhatt Since this is a Qt forum you may get better answers by search for/going to a Python forum, for logging.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 7 Sept 2023, 19:45 last edited by
        #3

        Hi,

        This article contains exactly what you are looking for.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        1/3

        7 Sept 2023, 05:38

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved