Skip to content

Instantly share code, notes, and snippets.

@TotallyNotAHaxxer
Created November 9, 2025 05:39
Show Gist options
  • Select an option

  • Save TotallyNotAHaxxer/01a0d009adbf0cf0ae32ce431bf65d01 to your computer and use it in GitHub Desktop.

Select an option

Save TotallyNotAHaxxer/01a0d009adbf0cf0ae32ce431bf65d01 to your computer and use it in GitHub Desktop.
Dynamic Patching with Python for Record Keeping of ALL Library Output
"""
This was a weird and niche case scenario. I had a problem where basically, I wanted to capture as much output from the script as possible and
display it on a web UI as a sort of live feed. However, this was only possible to the extent of writing the logs to a logger, or controlling
the code I wrote at the time.
Then I thought - simply; almost every library at the end of the day, even if it calls a logger class, will call print or
some form of sys.stdout. If we override print(), we handle most of the general cases where output gets printed from other
libraries, and can have a better result when trying to detect errors during the runtime.
"""
import builtins
original_print = builtins.print
captured_outputs = []
def __pre__init__intercept():
### @Proxy
def capturing_print(*args, **kwargs):
sep = kwargs.get('sep', ' ')
end = kwargs.get('end', '\n')
output = sep.join(str(arg) for arg in args) + end
captured_outputs.append(output.rstrip('\n'))
original_print(*args, **kwargs)
### @InjectIntercept
builtins.print = capturing_print
return captured_outputs
output_log = __pre__init__intercept()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment