Is it possible to log the function name and line n...
# ask-community
b
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
I believe so, you can change the format of logs
b
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 :
Copy code
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
Ah which Prefect function?
b
Copy code
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
We may need to adjust the stack-level on the print logger to support this
Interested in making a contribution? 🙂
b
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
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
z
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
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
Nope, it’s just for this logging call.
Yeah that’s true.
b
i see
z
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
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
Well, this fix would be great for anyone using
log_print
b
z
but yeah generally I’d recommend using an actual logger anyway
Ah great thanks!
b
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 🙂
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
Hi @Zanie @Bryan Whiting even changing the substack level as 2 we get same output.
b
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
sure
Same result
b
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
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