def redirect_stdio_to_logs() -> None: streams = [("stdout", LogLevel.info), ("stderr", LogLevel.error)] for (stream, level) in streams: oldStream = getattr(sys, stream) loggingFile = LoggingFile( logger=twisted.logger.Logger(namespace=stream), level=level, encoding=getattr(oldStream, "encoding", None), ) setattr(sys, stream, loggingFile) print("Redirected stdout/stderr to logs")
""" def discardLogs(): """ Discard messages logged via the global C{logfile} object. """ global logfile logfile = NullFile() # Prevent logfile from being erased on reload. This only works in cpython. if "logfile" not in globals(): logfile = LoggingFile( logger=NewLogger(), level=NewLogLevel.info, encoding=getattr(sys.stdout, "encoding", None), ) logerr = LoggingFile( logger=NewLogger(), level=NewLogLevel.error, encoding=getattr(sys.stderr, "encoding", None), ) class DefaultObserver(_GlobalStartStopObserver): """ Default observer. Will ignore all non-error messages and send error messages to sys.stderr. Will be removed when startLogging() is called for the first time.