https://prefect.io logo
Title
t

Tuoyi Zhao

08/24/2022, 8:00 PM
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

Tuoyi Zhao

08/24/2022, 8:12 PM
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

Andrew Huang

08/24/2022, 8:40 PM
t

Tuoyi Zhao

08/24/2022, 8:49 PM
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

Andrew Huang

08/24/2022, 8:54 PM
t

Tuoyi Zhao

08/24/2022, 8:56 PM
but no way to attach handler directly to Prefect logger? :(
t

Tuoyi Zhao

08/24/2022, 9:05 PM
thanks, i will do some investigations
a

Andrew Huang

08/24/2022, 9:06 PM
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

Tuoyi Zhao

08/25/2022, 10:37 PM
@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

Andrew Huang

08/25/2022, 10:41 PM
awesome job! Iโ€™ll add to Discourse if you dont mind
t

Tuoyi Zhao

08/25/2022, 10:41 PM
Of course ๐Ÿ˜‰ Hope this will help
and also linked here for visibility https://discourse.prefect.io/t/prefect-logging-faq/1476
โค๏ธ 1