Quickstart¶
Get azure-functions-logging running in a few minutes, then expand into structured logs, context injection, and request-scoped metadata.
What You Will Build¶
In this guide you will:
- Install the package.
- Configure logging with
setup_logging(). - Create a logger with
get_logger(). - Run the code locally and inspect output.
- Learn where to go next for production patterns.
Tip
If you already use the Azure Functions Python v2 model, you only need one additional import and one setup call.
Prerequisites¶
Before starting, make sure your environment includes:
- Python 3.10 or newer.
- A virtual environment (recommended).
- An Azure Functions Python project, or any local Python script for first validation.
- Optional: Azure Functions Core Tools if you want to run a local function host.
Install the Package¶
Install from PyPI:
If you pin dependencies, add to requirements.txt:
Note
The library has no external runtime dependencies. It builds on Python's standard logging module.
Five-Second Setup¶
Copy this into your module:
from azure_functions_logging import get_logger, setup_logging
setup_logging()
logger = get_logger(__name__)
logger.info("logging initialized")
What this does:
- Calls
setup_logging()once. - Uses color output by default in local development.
- Creates a
FunctionLoggerwrapper throughget_logger(__name__).
Minimal Azure Functions Example¶
Use this pattern for HTTP triggers:
import azure.functions as func
from azure_functions_logging import get_logger, logging_context, setup_logging
setup_logging()
logger = get_logger(__name__)
app = func.FunctionApp()
@app.route(route="health")
def health(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
with logging_context(context):
logger.info("Health check called")
return func.HttpResponse("ok", status_code=200)
Why logging_context(context) matters:
- Adds invocation metadata to each log record.
- Enables automatic
cold_startflagging. - Keeps your handler code clean and explicit.
- Always restores prior context on exit, even on exceptions.
Which context API do I use?¶
Three ways to attach invocation context — pick one:
with logging_context(context):— recommended. Scoped, always restores prior context on exit (even on exceptions).@with_context— decorator form of the same behavior; good when you want the whole handler wrapped implicitly.tokens = inject_context(context)/restore_context(tokens)— manual token form for middleware or framework integrations where you cannot use awithblock. You are responsible for callingrestore_context(tokens)in afinally.
Bare inject_context(context) without a paired restore_context() can leak context between invocations in a reused worker — prefer logging_context.
Expected Local Output¶
With default format="color", you should see lines shaped like:
14:32:10 INFO my_module logging initialized
14:32:12 INFO function_app Health check called [invocation_id=..., function_name=health, trace_id=..., cold_start=true]
You will notice:
- Time, level, logger name, and message are always present.
- Context fields appear when available.
cold_start=trueappears on the first invocation after process start.
Warning
In Azure-hosted environments, the host controls handlers and level behavior. Your app-level setup does not replace host logging configuration.
Test Locally in a Script¶
You can validate behavior without a Function host:
import logging
from azure_functions_logging import get_logger, setup_logging
setup_logging(level=logging.DEBUG)
logger = get_logger("demo")
logger.debug("debug visible")
logger.info("info visible")
logger.warning("warning visible")
logger.error("error visible")
This confirms:
setup_logging(level=...)applies locally.get_logger(name)wraps a standard logger withFunctionLogger.- All standard methods (
debug,info,warning,error,critical,exception) are available.
Test Locally in Azure Functions Core Tools¶
When running under Core Tools:
- Start local host.
- Send a request to your function.
- Check host output.
Important behavior in Core Tools and Azure environments:
- The library installs
ContextFilteronly. - It does not add new handlers.
- It respects host-managed logging pipeline.
Common First Mistakes¶
Avoid these early pitfalls:
- Calling
setup_logging()in multiple modules expecting different formats. - Forgetting to wrap the handler body in
with logging_context(context):. - Expecting app-level
levelto override restrictivehost.jsonlog level.
Next Steps¶
Continue with focused guides:
- Configuration for parameters and environment behavior.
- Usage Guide for complete patterns.
- Basic Setup Example for practical local flow.
- JSON Output Example for production structured logs.
- Context Injection Example for Azure context fields.
- API Reference for signatures and types.
Quick Checklist¶
Use this checklist before shipping:
setup_logging()called once during startup.get_logger(__name__)used in modules.- Handler body wrapped in
with logging_context(context):. - Chosen format is aligned with environment (
colorfor local,jsonfor production pipelines). host.jsonlogging level reviewed for conflicts.
Tip
Treat logger instances from bind() as request-scoped objects. Create them inside invocation logic, not as global mutable state.
When this checklist is green, you are ready for advanced patterns.