Configuration & logging
This page covers everything you can configure in FlaUI for Python: logging (both the library's own
Python messages and the C# messages from FlaUI itself), the FLAUI_* environment variables,
timeouts, and mouse speed.
Every Python snippet on this page runs
The snippets come from
examples/pytest/test_configuration.py
and the scripts in
examples/logging/:
Logging
How FlaUI logs
FlaUI for Python logs through the standard library logging module, using named loggers under
flaui:
| Logger | What it records |
|---|---|
flaui.lib.pythonnet_bridge |
Loading the FlaUI assemblies (DEBUG, one line per DLL), plus one INFO line when setup completes |
flaui.core.application |
Main-window lookups and cleanup problems (DEBUG) |
flaui.modules.automation |
Cleanup problems when an Automation exits (DEBUG) |
flaui.core.event_handlers |
Errors inside your UI Automation event callbacks |
flaui.csharp |
Messages from FlaUI's C# code, once you enable them |
The flaui logger has a NullHandler, so FlaUI prints nothing and never changes your logging
setup until you configure logging yourself. Because these are ordinary loggers, everything you
already do with logging works: levels, handlers, filters, dictConfig, pytest's caplog.
import logging
logging.basicConfig(level=logging.INFO) # your app's normal setup is all FlaUI needs
logging.getLogger("flaui").setLevel(logging.WARNING) # quieter FlaUI...
logging.getLogger("flaui.csharp").setLevel(logging.DEBUG) # ...except the C# messages
C# logging
FlaUI's C# code has its own logger (FlaUI.Core.Logging.Logger.Default), which records things like
process launches and application shutdown. It is off by default. Turning it on forwards every C#
message to the Python logger flaui.csharp, so C# and Python messages end up in the same place and
you control both from Python.
You can also turn it on without changing any code. Set these environment variables before
setup_pythonnet_bridge() runs:
$env:FLAUI_LOG_CSHARP = "1"
$env:FLAUI_LOG_CSHARP_LEVEL = "DEBUG" # optional: level for the flaui.csharp logger
disable_csharp_logging() turns it off again. C# log levels map to Python levels as follows: Trace
and Debug become DEBUG, Info becomes INFO, Warn becomes WARNING, Error becomes ERROR, and
Fatal becomes CRITICAL.
A typical C# message looks like this:
DEBUG flaui.csharp: [Launching process:C:\...\WpfApplication.exe] [Working directory:C:\...] ...
DEBUG flaui.csharp: Closing application
Sending logs to a sink
Each example below sends both the Python-side flaui.* messages and the flaui.csharp messages to
one place. Pick the logging library you already use.
Console at INFO, plus every C# message in its own file:
import logging.config
logging.config.dictConfig(
{
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"plain": {"format": "%(asctime)s %(levelname)-7s %(name)s: %(message)s"},
},
"handlers": {
"console": {"class": "logging.StreamHandler", "formatter": "plain", "level": "INFO"},
"csharp_file": {
"class": "logging.FileHandler",
"filename": "flaui-csharp.log",
"formatter": "plain",
"level": "DEBUG",
},
},
"loggers": {
# Every Python-side FlaUI logger: INFO and above to the console.
"flaui": {"handlers": ["console"], "level": "INFO"},
# C# FlaUI messages: everything, into their own file (and up to "flaui" for the console).
"flaui.csharp": {"handlers": ["csharp_file"], "level": "DEBUG"},
},
}
)
FlaUI logs through the standard library, so loguru's usual intercept handler bridges it. This sends everything to loguru, then writes the C# messages to a separate JSON-lines file:
import inspect
import logging
import sys
from loguru import logger
class InterceptHandler(logging.Handler):
"""Re-emit standard-library log records through loguru (the pattern from loguru's docs)."""
def emit(self, record: logging.LogRecord) -> None:
"""Forward one record, keeping its level and the caller's location."""
try:
level: str | int = logger.level(record.levelname).name
except ValueError:
level = record.levelno
frame, depth = inspect.currentframe(), 0
while frame and (depth == 0 or frame.f_code.co_filename == logging.__file__):
frame, depth = frame.f_back, depth + 1
logger.opt(depth=depth, exception=record.exc_info).bind(source=record.name).log(level, record.getMessage())
# Route the whole "flaui" hierarchy (Python modules + "flaui.csharp") into loguru only.
flaui_logger = logging.getLogger("flaui")
flaui_logger.handlers = [InterceptHandler()]
flaui_logger.setLevel(logging.DEBUG)
flaui_logger.propagate = False
# loguru sinks: a readable console, and a JSON file holding only the C# messages.
logger.remove()
logger.add(sys.stderr, level="INFO", format="{time:HH:mm:ss} {level:<7} {extra[source]}: {message}")
logger.add(
"flaui-csharp.jsonl", level="DEBUG", serialize=True, filter=lambda r: r["extra"].get("source") == "flaui.csharp"
)
structlog's ProcessorFormatter formats FlaUI's standard-library records, so FlaUI messages
(Python and C#) and your own structlog events come out as one JSON stream:
import logging
import sys
import structlog
shared_processors = [
structlog.stdlib.add_logger_name, # "logger": "flaui.csharp", "flaui.core.application", ...
structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
]
# Your own structlog loggers and FlaUI's stdlib records share one pipeline and one output.
structlog.configure(
processors=[*shared_processors, structlog.stdlib.ProcessorFormatter.wrap_for_formatter],
logger_factory=structlog.stdlib.LoggerFactory(),
)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(
structlog.stdlib.ProcessorFormatter(
foreign_pre_chain=shared_processors, # applied to FlaUI's (non-structlog) records
processors=[structlog.stdlib.ProcessorFormatter.remove_processors_meta, structlog.processors.JSONRenderer()],
)
)
flaui_logger = logging.getLogger("flaui")
flaui_logger.handlers = [handler]
flaui_logger.setLevel(logging.DEBUG)
flaui_logger.propagate = False
log = structlog.get_logger("flaui.example") # under "flaui", so it uses the same handler
Sample output:
In pytest
pytest captures logging records by default, and they show up in failure reports. To watch them
live, run uv run pytest -o log_cli=true --log-cli-level=DEBUG, or set log_cli = true and
log_cli_level = "INFO" under [tool.pytest.ini_options]. Use caplog to assert on them, as
test_enable_csharp_logging in the example file does.
Logs can contain sensitive data
C# messages include full process paths and working directories. Your own logging of element values can capture whatever is on screen. Treat log files like any other test artifact before uploading them from CI. See the Security Policy.
Environment variables
FlaUI reads these settings once, when it is first imported, so set them before your first
flaui import. The FLAUI_ prefix is required: a plain BIN_HOME, for example, is ignored.
| Variable | Default | What it does |
|---|---|---|
FLAUI_LOG_CSHARP |
off | Send C# FlaUI messages to the flaui.csharp logger (1, true, yes). |
FLAUI_LOG_CSHARP_LEVEL |
unset | Level for the flaui.csharp logger (DEBUG, INFO, ...). |
FLAUI_MAIN_WINDOW_TIMEOUT |
unset | How many milliseconds get_main_window waits (see Timeouts). |
FLAUI_BIN_HOME |
bundled flaui/bin |
Load the FlaUI DLLs from another folder. Only use this to test a different FlaUI build; the bridge logs a warning when it is set. |
CI |
unset | When set (most CI services set it), the default main-window wait grows from 7 s to 20 s. |
In code, the same values are available as flaui.lib.config.settings.
Timeouts
Main-window wait
automation.application.get_main_window(automation) keeps polling until the application's main
window appears. It uses the first of these that is set:
Automation(timeout=...), in milliseconds.- The
FLAUI_MAIN_WINDOW_TIMEOUTenvironment variable, in milliseconds. - 7000 ms, or 20000 ms when
CIis set.
UI Automation transaction and connection timeouts
The UIA3 automation object has two timeouts of its own. The transaction timeout limits how long
a single call to the target app may take; the default is 20 s. The connection timeout limits how
long connecting to the app may take; the default is 2 s. Both are .NET TimeSpan values. UIA2 does
not support them.
Units at a glance
Different APIs use different units, and C# uses TimeSpan everywhere:
| API | Unit |
|---|---|
Retry.*(timeout=, interval=) |
milliseconds (int) |
Automation(timeout=), FLAUI_MAIN_WINDOW_TIMEOUT, wait_while_main_handle_is_missing(time_out=) |
milliseconds (int) |
expect(el).to_*(timeout=) |
milliseconds (int) |
Wait.until_input_is_processed(...), Wait.until_responsive(...), Wait.while_cursor_is_busy(...) |
seconds (float) |
post_wait= on input methods |
True (~100 ms) or seconds (float) |
automation_base.transaction_timeout / connection_timeout |
.NET TimeSpan |
Mouse speed
Real mouse movement (Mouse.move_to, click(move_mouse=True), drag) is animated. These two class
settings are FlaUI's C# Mouse settings, so changing them takes effect immediately for every
following movement.