22import logging
33import os
44import atexit
5- from datetime import datetime
6- from typing import Any , TextIO
7- from agents import Span
5+ from typing import Any
86
97_original_print = builtins .print
108
119LOGFILE_NAME = "agentops-tmp.log"
1210
13- ## Instrument loggers and print function to log to a file
11+ # Instrument loggers and print function to log to a file
12+
13+
1414def setup_print_logger () -> None :
1515 """
1616 ~Monkeypatches~ *Instruments the built-in print function and configures logging to also log to a file.
1717 Preserves existing logging configuration and console output behavior.
1818 """
1919 log_file = os .path .join (os .getcwd (), LOGFILE_NAME )
20-
20+
2121 file_logger = logging .getLogger ('agentops_file_logger' )
2222 file_logger .setLevel (logging .DEBUG )
23-
23+
2424 file_handler = logging .FileHandler (log_file )
2525 file_handler .setFormatter (logging .Formatter ('%(asctime)s - %(levelname)s - %(message)s' ))
2626 file_handler .setLevel (logging .DEBUG )
2727 file_logger .addHandler (file_handler )
28-
28+
2929 # Ensure the new logger doesn't propagate to root
3030 file_logger .propagate = False
31-
31+
3232 def print_logger (* args : Any , ** kwargs : Any ) -> None :
3333 """
3434 Custom print function that logs to file and console.
35-
35+
3636 Args:
3737 *args: Arguments to print
3838 **kwargs: Keyword arguments to print
3939 """
4040 message = " " .join (str (arg ) for arg in args )
4141 file_logger .info (message )
42-
42+
4343 # print to console using original print
4444 _original_print (* args , ** kwargs )
45-
45+
4646 # replace the built-in print with ours
4747 builtins .print = print_logger
4848
@@ -56,15 +56,15 @@ def cleanup():
5656 for handler in file_logger .handlers [:]:
5757 handler .close ()
5858 file_logger .removeHandler (handler )
59-
59+
6060 # Restore the original print function
6161 builtins .print = _original_print
6262 except Exception as e :
6363 # If something goes wrong during cleanup, just print the error
6464 _original_print (f"Error during cleanup: { e } " )
6565
6666 # Register the cleanup function to run when the process exits
67- atexit .register (cleanup )
67+ atexit .register (cleanup )
6868
6969
7070def upload_logfile (trace_id : int ) -> None :
@@ -83,4 +83,3 @@ def upload_logfile(trace_id: int) -> None:
8383 client .api .v4 .upload_logfile (log_content , trace_id )
8484
8585 os .remove (log_file )
86-
0 commit comments