Alright, I read from the doc that the preferred logging approach is unreal.log() API series. However, the python logging module has much-needed features such as including line numbers and function names. If Epic soft-bans logging (out of sanitation concerns) with that red-coded Error tag. I guess I’ll just have to compromise.
Perhaps this could help you; try to override the emit method of your logger’s handler and use unreal.log, log_warning, unreal.log_error to handle the logging.
For requirements such as outputting to local storage or retaining online, you can add additional handlers to your logger to handle these separately.
import logging
import unreal
class UnrealLogHandler(logging.Handler):
def emit(self, record):
msg = self.format(record)
if record.levelno == logging.INFO:
unreal.log(msg) # Use Unreal's log function to output information
elif record.levelno == logging.WARNING:
unreal.log_warning(msg) # Use Unreal's log_warning function to output warnings
elif record.levelno == logging.ERROR:
unreal.log_error(msg) # Use Unreal's log_error function to output errors
# Create a logger
logger = logging.getLogger("new_tool")
handler = UnrealLogHandler()
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
# Use the logger to output messages
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")