<@ULVA73B9P> I am trying to test my deployment cod...
# ask-marvin
j
@Marvin I am trying to test my deployment code at the command line but am having problems getting my custom logger to show up. What looks wrong?
Copy code
if __name__ == "__main__":
    from pipeline.projectname_main.projectname_otherflow import projectname_otherflow

    job_variables = {"env":  {
        "PREFECT_LOGGING_EXTRA_LOGGERS": "mylog",
        "PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW": "ignore",
    }}

    d2 = projectname_otherflow.to_deployment("my-test", tags=["my-test"], job_variables=job_variables)
    d1 = projectname_driver.to_deployment("my-test", parameters = {
                                         "staging_dir_var": "projectname_sandbox_staging_dir",
                                         "output_dir_var": "projectname_sandbox_output_dir",
                                         "dsn_block": "projectname-sandbox-dsn",
                                         "otherflow_deployment": d2.name },
                                      job_variables=job_variables,
                                      tags=["my-test"])

    serve(d1, d2)
m
thought for 225.0 seconds
Short answer: the env vars you set in
job_variables
won’t be applied when you run
serve(...)
locally.
job_variables["env"]
are only used by workers (Docker/K8s/etc.) when a deployment runs from a work pool. With
serve
, flows run in your local process, so your extra logger never gets attached. What to change: - Set the logging settings in the process environment (export in your shell or set via Prefect profile) before you call
serve(...)
. - Make sure you’re logging from inside a flow/task run if you expect the logs to appear in the Prefect UI. Your
PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW=ignore
will silently drop logs to the API when there’s no flow context. Quick example to test locally with serve:
Copy code
# At shell
export PREFECT_LOGGING_EXTRA_LOGGERS=mylog
export PREFECT_LOGGING_LEVEL=INFO
# Optional: this suppresses warnings for logs outside a flow; they also won't go to the UI
export PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW=ignore

python your_script.py
Or set in code before importing/using your flows:
Copy code
import os, logging
os.environ["PREFECT_LOGGING_EXTRA_LOGGERS"] = "mylog"
os.environ["PREFECT_LOGGING_LEVEL"] = "INFO"
# os.environ["PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW"] = "ignore"

logger = logging.getLogger("mylog")

from prefect import flow, task
from prefect import serve

@task
def t():
    <http://logger.info|logger.info>("hello from mylog task")

@flow
def f():
    <http://logger.info|logger.info>("hello from mylog flow")
    t()

if __name__ == "__main__":
    serve(f)
If you do want these env vars to apply when the deployment runs on a worker, keep them in
job_variables={"env": {...}}
— that’s correct. But for local
serve
, set them in the environment or Prefect profile:
Copy code
prefect config set logging.extra_loggers='["mylog"]'
prefect config set logging.level="INFO"
A few gotchas to double-check: - The logger name must match exactly:
logging.getLogger("mylog")
. - If you’re logging at DEBUG, also set
PREFECT_LOGGING_LEVEL=DEBUG
. - Logs must be emitted inside a flow/task to reach the Prefect UI. Outside a flow, they’ll go to console, and with
...WHEN_MISSING_FLOW=ignore
they won’t attempt the API handler. - Minor: your snippet doesn’t show an import for `projectname_driver`; make sure that’s imported. If you can share how you’re creating and using the
mylog
logger (and whether you expect to see it in the console vs. in the UI), I can tailor the fix further.