# Python logging calls result in Error in LogPython

**URL:** <https://forums.unrealengine.com/t/python-logging-calls-result-in-error-in-logpython/446430>\
**Category:** Editor Scripting\
**Tags:** error, Python, logging, unreal-engine, editor, Scripting, question\
**Created:** [April 2, 2019, 3:02pm UTC](https://forums.unrealengine.com/t/python-logging-calls-result-in-error-in-logpython/446430 "2019-04-02T15:02:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kakyoism](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kakyoism](https://forums.unrealengine.com/u/kakyoism)\
**Post date:** [April 2, 2019, 3:02pm UTC](https://forums.unrealengine.com/t/python-logging-calls-result-in-error-in-logpython/446430/1 "2019-04-02T15:02:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kakyoism](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kakyoism](https://forums.unrealengine.com/u/kakyoism)\
**Post date:** [April 3, 2019, 4:25am UTC](https://forums.unrealengine.com/t/python-logging-calls-result-in-error-in-logpython/446430/2 "2019-04-03T04:25:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Claudio.Alvaro](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/claudio.alvaro/32/618031_2.png) [@Claudio.Alvaro](https://forums.unrealengine.com/u/Claudio.Alvaro)\
**Post date:** [July 11, 2019, 4:21pm UTC](https://forums.unrealengine.com/t/python-logging-calls-result-in-error-in-logpython/446430/3 "2019-07-11T16:21:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chad\_Vernon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Chad\_Vernon](https://forums.unrealengine.com/u/Chad_Vernon)\
**Post date:** [January 31, 2023, 5:36am UTC](https://forums.unrealengine.com/t/python-logging-calls-result-in-error-in-logpython/446430/4 "2023-01-31T05:36:10Z")

</div>

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![SumTB](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SumTB](https://forums.unrealengine.com/u/SumTB)\
**Post date:** [September 24, 2024, 1:47pm UTC](https://forums.unrealengine.com/t/python-logging-calls-result-in-error-in-logpython/446430/5 "2024-09-24T13:47:18Z")

</div>

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.

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

```
