Python logging calls result in Error in LogPython

My python script looks like this:

import logging
import unreal as ue

logging.basicConfig(level=logging.DEBUG)

# Print some project info
projSettings = ue.GeneralProjectSettings()
logging.info(projSettings.get_editor_property('company_name'))

As innocent as it is, the Output Log gives me:

LogPython: Error: INFO:root:MyCompany

Is it that the Python stdlibs are completely discouraged here or is this simply a bug?

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.

Have you tried creating a handler that pushes to unreal.log with the formatting you want? This was my next task…

The logging StreamHandler defaults to sys.stderr. You can switch it with:

logger = logging.getLogger(__name__)
logger.handlers.clear()
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch = logging.StreamHandler(stream=sys.stdout)
ch.setFormatter(formatter)
logger.addHandler(ch)

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")