Hi everyone, i'm currently transforming some previ...
# prefect-community
t
Hi everyone, i'm currently transforming some previous Prefect flow to the Prefect 2.0.....i encountered some issues during migration • How can I write log into the local file? In 1.0, i use python's file handler and add that handler to the Prefect logger. However, in 2.0, it seems there's no addHandler method in Prefect logger.
1
Basically, we expose all of Python’s logging settings to you https://github.com/PrefectHQ/prefect/blob/orion/src/prefect/logging/logging.yml
t
HI Michael, I still not fully understand....so you mean I can just add a file handler into the logging.yml in handlers section?
a
t
Thanks @Andrew Huang, but i still have no idea how to add a local file handler directly in the python code......is there a handy way to add the File Handler to the Prefect 2.0 logger so the log can be written during flow run?
👀 1
I saw there's way to add extra logger from yaml.....but is that going to be global instead of flow specific? as i only want to log one specific flow
a
t
but no way to attach handler directly to Prefect logger? :(
t
thanks, i will do some investigations
a
great! if you figure it out, feel free to post on the discourse so it can be shared with others too (or reply back here and I can post it for you)
👍 1
t
@Andrew Huang I actually figured out a way to add FileHandler to specific flow. The part in Italic is the key. This solution is similar to the one I used in Prefect 1.0 but I have to add separately to both Flow and Task logger in Prefect 2.0. import prefect from prefect import flow, task import logging from datetime import datetime @task def string_prep(ct): return ct.strftime("%H:%M:%S") @flow() def dynamic_parameter(ct=datetime.now()): #Add File Handler to both flow and task logger _flow_logger = logging.getLogger('prefect.flow_runs')_ _task_logger = logging.getLogger('prefect.task_runs')_ handler = logging.FileHandler(filename=<replace with your own log file path>) handler.setFormatter(logging.Formatter("%(asctime)s.%(msecs)03d | %(levelname)-7s | %(name)s - %(message)s")) _flow_logger.addHandler(handler)_ _task_logger.addHandler(handler)_ #Actual Flow Part general_logger = prefect.logging.get_run_logger() strings = string_prep(ct) general_logger.info('see see ' + strings)
👀 1
a
awesome job! I’ll add to Discourse if you dont mind
t
Of course 😉 Hope this will help
❤️ 1