https://prefect.io logo
Title
b

Bryan Whiting

01/24/2023, 5:39 PM
Is it possible to log the function name and line number? (I know I can do this with the default logger, but is this possible with prefect logger?) also, can this be done with @flow(log_prints=True)?
1
z

Zanie

01/24/2023, 5:46 PM
I believe so, you can change the format of logs
b

Bryan Whiting

01/24/2023, 9:58 PM
That seems to work on updating the format, but it doesn’t log the name of the function that called the print statement. Just logs the name of the prefect function. Hmm…
I updated the logger to be :
formatters:
    simple:
        format: "%(asctime)s.%(msecs)03d | %(message)s"
        datefmt: "%H:%M:%S"

    standard:
        (): prefect.logging.formatters.PrefectFormatter
        format: "%(asctime)s.%(msecs)03d | %(levelname)-7s | %(name)s | %(filename)s:%(lineno)s - %(funcName)20s() - %(message)s"
        flow_run_fmt: "%(asctime)s.%(msecs)03d | %(levelname)-7s | Flow run %(flow_run_name)r | %(filename)s:%(lineno)s - %(funcName)20s() | %(message)s"
        task_run_fmt: "%(asctime)s.%(msecs)03d | %(levelname)-7s | Task run %(task_run_name)r | %(filename)s:%(lineno)s - %(funcName)20s() | %(message)s"
        datefmt: "%H:%M:%S"
z

Zanie

01/24/2023, 10:05 PM
Ah which Prefect function?
b

Bryan Whiting

01/24/2023, 10:06 PM
13:57:01.089 | INFO    | Flow run 'imperial-hoatzin' | loggers.py:221 -         print_as_log() | Available eval_metrics: ['mae', 'rmse'] (Note: xgboost uses the last to determine n_tree_limit)
z

Zanie

01/24/2023, 10:06 PM
We may need to adjust the stack-level on the print logger to support this
Interested in making a contribution? 🙂
b

Bryan Whiting

01/24/2023, 10:06 PM
i’m using
log_prints=True
, so it’s just doing
print_as_log()
lol sure, if i knew how!
is this possible with python logging?
z

Zanie

01/24/2023, 10:07 PM
Oh yeah totally
Where we call
<http://logger.info|logger.info>
in
print_as_log
I think we need to set a
stacklevel
Maybe to 2? 🙂 that’s where it’s helpful if you give it a try since you’ve got it setup
b

Bryan Whiting

01/24/2023, 10:08 PM
z

Zanie

01/24/2023, 10:08 PM
Yep!
The third optional keyword argument is stacklevel, which defaults to
1
. If greater than 1, the corresponding number of stack frames are skipped when computing the line number and function name set in the
LogRecord
created for the logging event. This can be used in logging helpers so that the function name, filename and line number recorded are not the information for the helper function/method, but rather its caller.
b

Bryan Whiting

01/24/2023, 10:09 PM
would that mess up the other parts of the logger?
seems like it has to be on the individual messages. so
<http://log.info|log.info>()
level rather than a global setting. is that true?
z

Zanie

01/24/2023, 10:11 PM
Nope, it’s just for this logging call.
Yeah that’s true.
b

Bryan Whiting

01/24/2023, 10:11 PM
i see
z

Zanie

01/24/2023, 10:19 PM
We have some docs on getting started contributing (I just pushed some minor changes so here’s a preview!) https://deploy-preview-8247--prefect-orion.netlify.app/contributing/overview/#setting-up-a-development-environment
Let me know if you’re interested! We do our best to help people get started contributing
b

Bryan Whiting

01/24/2023, 10:19 PM
debating, would this take longer or would just implementing the
get_run_logger()
? lol. I only used print as a hack but now i’m realizing might just be better to use the base logger
z

Zanie

01/24/2023, 10:26 PM
Well, this fix would be great for anyone using
log_print
b

Bryan Whiting

01/24/2023, 10:26 PM
z

Zanie

01/24/2023, 10:26 PM
but yeah generally I’d recommend using an actual logger anyway
Ah great thanks!
b

Bryan Whiting

01/24/2023, 10:26 PM
i would probably want to use the actual logger since I can then use warning/debug/etc, which I didn’t realize I couldn’t do with
print_as_log()
yea, happy to bump from 666 issues to 667 😉
666 was bad news, needed one more 🙂
image.png
at least it creates a searchable place for future discussion! I’m guessing I’ll want the root logger anyway
When I use
<http://logger.info|logger.info>()
this is WAI:
s

Sidharth Ramalingam

02/18/2023, 4:43 AM
Hi @Zanie @Bryan Whiting even changing the substack level as 2 we get same output.
image.png,image.png
b

Bryan Whiting

02/18/2023, 4:45 AM
Interesting. What if you set to 3?
Can you force it to break with something like “logger.info(“hello world”, stacklevel=2)”, just to make sure the module is loading your changes?
s

Sidharth Ramalingam

02/18/2023, 4:46 AM
sure
Same result
b

Bryan Whiting

02/18/2023, 4:49 AM
Interesting. I have no idea, I’m not a prefect expert. But that suggests the logger’s stack grace is probably overwritten somewhere else
s

Sidharth Ramalingam

02/18/2023, 4:50 AM
Yeah
Stack level 4 is working
😃
Hi apologies for the confusion had some issue on my dev environment stacklevel 2 working as expected
@Zanie @Bryan Whiting Have written test for checking and the function is working as expected