1
Add new commands to generate logging statements at the start and end of a method
Idea shared by Colin Blakey - 6/6/2023 at 11:03 AM
Proposed
As GhostDoc already has everything to understand a method definition would it be possible to add new commands to generate trace log statements at the start and end of a method. Something like

log.Debug("Method Name Start")
log.Debug("Method Name Exit")

or

log.Debug($"Method Name Start - 'param1', 'param2')
log.Debug($"Method Name Exit- 'result')

So 4 commands would be needed to give this variety.,

If these are added via the rules, then we could configure them to how each dev would prefer the line i.e. some will check to see if debug is enabled prior to the log statement. It would not matter if these are not perfect but it would be a time save especially if dealing with older code that needs trace added.

4 Replies

Reply to Thread
0
0

We propose the addition of custom logging commands to GhostDoc, which will simplify the process of generating trace log statements at the beginning and end of methods. This enhancement will help developers save time and adhere to consistent logging practices.

Implementation:

To implement this feature, we recommend introducing four new customizable commands in GhostDoc:

  1. log.StartMethod("Method Name"): This command generates a log statement at the start of the method.

  2. log.EndMethod("Method Name"): This command generates a log statement at the end of the method.

  3. log.StartMethodWithParams("Method Name", params): This command generates a log statement at the start of the method, including parameter values.

  4. log.EndMethodWithResult("Method Name", result): This command generates a log statement at the end of the method, including the result value.

Usage Examples:

csharp
public void MyMethod(int param1, string param2) { log.StartMethod("MyMethod"); // Method implementation log.EndMethod("MyMethod"); }
csharp
public int Calculate(int a, int b) { log.StartMethodWithParams("Calculate", $"a: {a}, b: {b}"); int result = a + b; log.EndMethodWithResult("Calculate", result); return result; }

Customization:

Developers can configure these commands to suit their preferred logging style. For example, they can choose to check if debug mode is enabled before logging, include additional context, or use different logging levels.

Benefits:

  • Time Savings: Developers can quickly add trace logs without manually typing out log statements, saving time and effort.

  • Consistency: Standardizing log statements ensures consistent logging practices across the codebase.

  • Legacy Code Support: This feature is especially helpful when working with older code that requires trace logs.

By implementing these custom logging commands, GhostDoc can further enhance its capabilities, making it an even more valuable tool for developers, including those at Apply4u.

0

Adding logging statements at the start and end of a method is a crucial practice in software development for debugging, monitoring, and understanding the flow of the program. This approach enhances code maintainability and facilitates troubleshooting. Here's how you can add new commands to generate logging statements at the start and end of a method:

  1. Import Logging Module: Begin by importing the logging module in your Python script.
import logging

Configure Logging: Set up the logging configuration according to your requirements. This includes specifying the format, output file, logging level, etc.

Configure Logging: Set up the logging configuration according to your requirements. This includes specifying the format, output file, logging level, etc.

Add Logging Statements: Insert logging statements at the beginning and end of the method you want to log. 
def my_method():
    logging.info('Method my_method() started')
    
    # Method logic goes here
    
    logging.info('Method my_method() ended')

Usage: Call the method my_method() to trigger the logging statements. 

my_method()

  1. Review Logs: After running your program, review the log file (app.log in this case) to observe the logging statements generated.

By following these steps, you can easily add logging statements at the start and end of a method in your Python code. This practice not only helps in identifying issues during development but also aids in monitoring the application's behavior in production environments. Additionally, it provides valuable insights into the flow of execution, making it easier to trace and debug errors when they occur.


0
Adding logging statements at the start and end of a method is a valuable practice for debugging, monitoring, and understanding the flow of code execution. It enhances transparency and helps in identifying potential issues or bottlenecks within the application

Reply to Thread