Constantino Schillebeeckx
11/23/2021, 12:30 AMPREFECT__LOGGING__LEVEL="DEBUG"
within the run_config (ECSRunner) and I've confirmed that this env is set as such in the running container. However, within my running flow, when I check os.environ
its set to INFO
2. I setup a logger in my shared code that I later import like a library: logger = logging.getLogger("dwh.utils.main")
. I've set PREFECT__CLOUD__SEND_FLOW_RUN_LOGS='true'
and PREFECT__LOGGING__EXTRA_LOGGERS="['dwh']"
. when I execute a flow that uses that shared code, I can't see it emit logs in CloudWatch or in Prefect cloud, however when I run the flow locally I do see the logging statement
what am I missing?Kevin Kho
from prefect import task, Flow
from prefect.run_configs import ECSRun
from prefect.storage import S3
import prefect
@task
def abc():
<http://prefect.context.logger.info|prefect.context.logger.info>(dict(prefect.context))
return "hello"
with Flow("ecs_test") as flow:
abc()
RUN_CONFIG = ECSRun(labels=['ecs_test'],
run_task_kwargs={'cluster': 'test-cluster'},
task_role_arn= 'arn:aws:iam::123456789:role/prefect-ecs',
execution_role_arn='arn:aws:iam::123456789:role/prefect-ecs',
image='prefecthq/prefect:latest-python3.8',
env={"PREFECT__LOGGING__LEVEL":"DEBUG"}
)
flow.storage=S3(bucket="coiled-prefect", add_default_labels=False)
flow.run_config = RUN_CONFIG
flow.register("bristech")
Kevin Kho
dwh
happening on the same process or a different process? Think like spinning up a Jupyter notebook to run. Did you set those in the RunConfig?Anna Geller
export PREFECT__LOGGING__LEVEL="DEBUG"
export PREFECT__LOGGING__FORMAT="%(levelname)s - %(name)s | %(message)s" # format the message as you wish
and then use the logger from context rather than importing from a custom module:
logger = prefect.context.get("logger")
The extra loggers are meant to include logs from external libraries e.g. boto3. More on that here: https://docs.prefect.io/core/concepts/logging.html#extra-loggersConstantino Schillebeeckx
11/23/2021, 8:41 PMtask_defefintion
section - that shouldn't matter right?Constantino Schillebeeckx
11/23/2021, 8:42 PMMake sure to only access context while your task is running
Constantino Schillebeeckx
11/23/2021, 8:44 PMIsI"m not sure I follow.happening on the same process or a different process? Think like spinning up a Jupyter notebook to run. Did you set those in the RunConfig?dwh
dwh
is a named logger which I can import directly from the container.Anna Geller
logger = prefect.context.get("logger")
Constantino Schillebeeckx
11/23/2021, 8:44 PMextra_loggers
is for? I mean, how would I emit logs from other libraries I'm using?Anna Geller
Constantino Schillebeeckx
11/23/2021, 8:50 PMZanie
Zanie
from fake_lib import function_that_logs
from prefect import Flow, task
@task
def foo():
function_that_logs()
with Flow("extra-loggers-example") as flow:
foo()
if __name__ == "__main__":
flow.run()
Zanie
import logging
def function_that_logs():
logger = logging.getLogger("foo.bar")
<http://logger.info|logger.info>("Hello from fake_lib!")
Zanie
Zanie
❯ python extra-logger.py
[2021-11-23 14:53:32-0600] INFO - prefect.FlowRunner | Beginning Flow run for 'extra-loggers-example'
[2021-11-23 14:53:32-0600] INFO - prefect.TaskRunner | Task 'foo': Starting task run...
[2021-11-23 14:53:32-0600] INFO - prefect.TaskRunner | Task 'foo': Finished task run for task with final state: 'Success'
[2021-11-23 14:53:32-0600] INFO - prefect.FlowRunner | Flow run SUCCESS: all reference tasks succeeded
~/dev/support prefect-dev-38
❯ PREFECT__LOGGING__EXTRA_LOGGERS='["foo"]' python extra-logger.py
[2021-11-23 14:54:00-0600] INFO - prefect.FlowRunner | Beginning Flow run for 'extra-loggers-example'
[2021-11-23 14:54:00-0600] INFO - prefect.TaskRunner | Task 'foo': Starting task run...
[2021-11-23 14:54:00-0600] INFO - foo.bar | Hello from fake_lib!
[2021-11-23 14:54:00-0600] INFO - prefect.TaskRunner | Task 'foo': Finished task run for task with final state: 'Success'
[2021-11-23 14:54:00-0600] INFO - prefect.FlowRunner | Flow run SUCCESS: all reference tasks succeeded
Zanie
Zanie
❯ prefect agent local start --env PREFECT__LOGGING__EXTRA_LOGGERS='["foo"]'
Zanie
❯ prefect run --name "extra-loggers-example"
Zanie
(in the UI)
14:55:40
INFO
foo.bar
Hello from fake_lib!
Constantino Schillebeeckx
11/23/2021, 8:58 PMdwh/tils/main.py
import logging
from decouple import config
logger = logging.getLogger("dwh.utils.main")
def get_aws_region() -> str:
"""Get the AWS region.
Gets the AWS region from the ``AWS_REGION`` environment variable, defaults to 'us-west-2' if not set.
Returns
-------
str
AWS region
"""
region = config("AWS_REGION", "us-west-2")
<http://logger.info|logger.info>(f"Found AWS {region=}")
return region
This code gets packaged in a docker container and is installed as a python library so that I can later import with itZanie
Zanie
from fake_lib import function_that_logs
from prefect import Flow, task
from prefect.run_configs import UniversalRun
@task
def foo():
function_that_logs()
with Flow(
"extra-loggers-example",
run_config=UniversalRun(env={"PREFECT__LOGGING__EXTRA_LOGGERS": '["foo"]'}),
) as flow:
foo()
if __name__ == "__main__":
flow.run()
Zanie
❯ prefect register -p extra-logger.py --project example
Collecting flows...
Processing 'extra-logger.py':
Building `Local` storage...
Registering 'extra-loggers-example'... Done
└── ID: 4921bd3f-646f-42b9-ac0d-0272b09d3140
└── Version: 2
======================== 1 registered ========================
~/dev/support
❯ prefect run --name "extra-loggers-example" --watch
Looking up flow metadata... Done
Creating run for flow 'extra-loggers-example'... Done
└── Name: graceful-kangaroo
└── UUID: 5bd86a3f-7441-4127-ab5c-9b60565977d5
└── Labels: ['Michaels-MacBook-Pro.local']
└── Parameters: {}
└── Context: {}
└── URL: <https://staging.prefect.io/michael-staging/flow-run/5bd86a3f-7441-4127-ab5c-9b60565977d5>
Watching flow run execution...
└── 14:58:32 | INFO | Entered state <Scheduled>: Flow run scheduled.
└── 14:58:41 | INFO | Entered state <Submitted>: Submitted for execution
└── 14:58:41 | INFO | Submitted for execution: PID: 25150
└── 14:58:42 | INFO | Entered state <Running>: Running flow.
└── 14:58:41 | INFO | Beginning Flow run for 'extra-loggers-example'
└── 14:58:43 | INFO | Task 'foo': Starting task run...
└── 14:58:43 | INFO | Hello from fake_lib!
└── 14:58:44 | INFO | Task 'foo': Finished task run for task with final state: 'Success'
└── 14:58:44 | INFO | Flow run SUCCESS: all reference tasks succeeded
└── 14:58:45 | INFO | Entered state <Success>: All reference tasks succeeded.
Flow run succeeded!
Constantino Schillebeeckx
11/23/2021, 9:01 PMlogger = logging.getLogger("foo.bar")
to the top of your library file?Zanie
logger = logging.getLogger("foo.bar")
call out of the function and into the top-level (as you do) also works.Constantino Schillebeeckx
11/23/2021, 9:01 PMZanie
Zanie
Zanie
from prefect import Flow, task
from prefect.run_configs import UniversalRun
from fake_lib import function_that_logs
Constantino Schillebeeckx
11/23/2021, 9:03 PMfrom dwh.utils.main import get_aws_region
Zanie
Zanie
@task(log_stdout=True)
def show_extra_loggers():
print(config.logging.extra_loggers)
Constantino Schillebeeckx
11/23/2021, 9:09 PMZanie
Zanie
Zanie
Constantino Schillebeeckx
11/23/2021, 9:16 PMConstantino Schillebeeckx
11/23/2021, 9:16 PMConstantino Schillebeeckx
11/23/2021, 9:16 PMConstantino Schillebeeckx
11/23/2021, 9:17 PM'logging': {'level': 'INFO', 'format': '%(levelname)s - %(name)s | %(message)s', 'log_attributes': [], 'datefmt': '%Y-%m-%d %H:%M:%S%z', 'extra_loggers': ['dwh'], 'log_to_cloud': True}
Zanie
Zanie
Zanie
log_to_cloud
there and not send_flow_run_logs
(the first was deprecated)Zanie
Constantino Schillebeeckx
11/23/2021, 9:22 PMECSRun(**run_config)
, you can see that i'm also setting send_flow_run_logs
thereConstantino Schillebeeckx
11/23/2021, 9:23 PMcontainerDefinitions
?Constantino Schillebeeckx
11/23/2021, 9:23 PMZanie
Zanie
Zanie
Constantino Schillebeeckx
11/23/2021, 9:30 PMCan you try setting the environment variables at the top-level instead of the container definition?trying that now
Constantino Schillebeeckx
11/23/2021, 9:38 PMZanie
Constantino Schillebeeckx
11/23/2021, 9:39 PMPREFECT__CLOUD__SEND_FLOW_RUN_LOGS
- if I set this to False, are the custom logs NOT sent to the cloud?Zanie
SEND_FLOW_RUN_LOGS
disable all emission of logs to the cloud APIZanie
Constantino Schillebeeckx
11/23/2021, 9:40 PMZanie
Zanie
Constantino Schillebeeckx
11/23/2021, 9:41 PMConstantino Schillebeeckx
11/24/2021, 4:41 PM