inngest / inngest-py

Python SDK for Inngest: Durable functions and workflows in Python, hosted anywhere
https://www.inngest.com/docs/reference/python
Apache License 2.0
27 stars 6 forks source link

Logging info #50

Closed masylum closed 7 months ago

masylum commented 7 months ago

Hey there! I'm implementing a FastAPI application and it seems like none of my logging.info are working. This is what I currently have setup:

uvicorn_logger = logging.getLogger("uvicorn")
uvicorn_logger.setLevel(logging.DEBUG)

inngest_client = inngest.Inngest(
    app_id="test",
    logger=uvicorn_logger,
)

Am I holding it wrong?

Thanks in advance

goodoldneon commented 7 months ago

Sorry you're having some trouble with the logger! I wasn't able to reproduce the issue on version 0.3.1 of Inngest, but I'll leave a minimal working example to give us some common code to work off.

Here's my app:

import inngest
import fastapi
import logging

import inngest.fast_api

uvicorn_logger = logging.getLogger("uvicorn")
uvicorn_logger.setLevel(logging.DEBUG)

# Create an Inngest client
inngest_client = inngest.Inngest(
    app_id="fast_api_example",
    logger=uvicorn_logger,
)

# Create an Inngest function
@inngest_client.create_function(
    fn_id="my-fn",
    trigger=inngest.TriggerEvent(event="foo"),
)
async def fn(
    ctx: inngest.Context,
    step: inngest.Step,
) -> None:
    ctx.logger.info("Hello world!")

app = fastapi.FastAPI()

# Serve Inngest endpoint
inngest.fast_api.serve(
    app,
    inngest_client,
    [fn],
)

I run my app:

(export INNGEST_DEV=1 && uvicorn main:app --reload)

I start the Dev Server with auto-discovery and polling disabled (to prevent a bunch of other logs from showing up in the app):

npx inngest-cli@latest dev --no-discovery --no-poll

I tell my app to sync itself with the Dev Server:

curl -X PUT localhost:8000/api/inngest

I send an event to trigger my Inngest function:

curl -X POST localhost:8288/e/foo -d '{"name": "foo"}'

In my app logs I see this:

INFO:     127.0.0.1:52638 - "PUT /api/inngest HTTP/1.1" 200 OK
INFO:     Hello world!
INFO:     127.0.0.1:52843 - "POST /api/inngest?fnId=fast_api_example-my-fn&stepId=step HTTP/1.1" 200 OK
masylum commented 7 months ago

oops, I was on 0.2 and following the latest documentation 🤦 Fixed!

goodoldneon commented 7 months ago

No worries! 0.3 is pretty new

masylum commented 7 months ago

btw, it only works if I use ctx.logger, using logging.info does not

goodoldneon commented 7 months ago

What do you mean by logging.info? Like this?

import logging

logging.info(2)
masylum commented 7 months ago

yes

goodoldneon commented 7 months ago

I'm not a Python logging expert, but I thought that didn't work in general. If you run a Python script like this you won't see anything in your terminal:

import logging
logging.info("hi")

But even if that did work, you'd still want to use ctx.logger.info within an Inngest function. This will prevent duplicate logs since we actually execute your function multiple times (roughly once per step.* call)