Skip to content

Examples LoggingFormat

Truong Giang Vu edited this page Feb 14, 2026 · 2 revisions

Example: Logging & Syntax Highlighting

Demo: Test log formatting, colors, and structured output
Script: logging_format_script.py
Time: 1-2 minutes


What It Demonstrates

  • Log level detection via keywords
  • Syntax highlighting for different data types
  • JSON pretty-printing
  • Exception stack traces with formatting

Key Pattern: Log Level Detection

Prefix Detection (Priority 1)

print("[INFO] This should be Information level")
print("[WARN] This should be Warning level")
print("[ERROR] This should be Error level")
print("[FATAL] This should be Critical level")

Output: Each line appears in Trace Panel with appropriate color.

Keyword Detection (Priority 2)

print("Operation completed successfully")  # β†’ Info (contains "completed")
print("Warning: Memory usage is high")     # β†’ Warning (contains "warning")
print("Error occurred during processing")  # β†’ Error (contains "error")

Output: Automatic color coding based on detected keywords.


Key Pattern: Pretty JSON

import json

data = {
    "project": "Office Building",
    "walls": 127,
    "floors": 8,
    "families": ["Walls", "Doors", "Windows"]
}

print(json.dumps(data, indent=2))

Output in Trace Panel:

{
  "project": "Office Building",
  "walls": 127,
  "floors": 8,
  "families": [
    "Walls",
    "Doors",
    "Windows"
  ]
}
  • Keys in purple
  • Strings in yellow
  • Numbers in cyan
  • Proper indentation preserved

Key Pattern: Exception Formatting

try:
    result = 10 / 0
except Exception as e:
    print(f"ERROR: {e}")
    import traceback
    traceback.print_exc()

Output:

ERROR: division by zero
Traceback (most recent call last):
  File "logging_format_script.py", line 85, in test_exceptions
    result = 10 / 0
ZeroDivisionError: division by zero
  • Stack trace with syntax highlighting
  • File paths clickable (if configured)
  • Line numbers visible

Try It Yourself

  1. Load folder: source/RevitDevTool.PythonDemo/commands/
  2. Execute logging_format_script.py
  3. Watch Trace Panel for colored output
  4. Observe different log levels and formatting

Full source: logging_format_script.py


Related Examples

Clone this wiki locally