You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@airflow.apache.org by "Maycock, Luke" <lu...@affiliate.oliverwyman.com> on 2016/10/17 14:04:00 UTC
String formatting
Hi Dev List,
We're currently working on removing all of the new Landscape.io warnings from some of our code and we're noticing the following quite a lot:
"Use % formatting in logging functions and pass the % parameters as arguments"
This is being flagged up on lines such as:
"logging.info("Running command:\n {}".format(hql))"
We're just wondering whether it is indeed preferred to use the % parameters as suggested or whether this is an issue with the Landscape.io setup.
Can anybody give a judgement on this? If the .format is preferred, then we'll look into changing the landscape.io settings.
Thanks,
Luke Maycock
OLIVER WYMAN
luke.maycock@affiliate.oliverwyman.com<ma...@affiliate.oliverwyman.com>
www.oliverwyman.com<http://www.oliverwyman.com/>
________________________________
This e-mail and any attachments may be confidential or legally privileged. If you received this message in error or are not the intended recipient, you should destroy the e-mail message and any attachments or copies, and you are prohibited from retaining, distributing, disclosing or using any information contained herein. Please inform us of the erroneous delivery by return e-mail. Thank you for your cooperation.
Re: String formatting
Posted by Li Xuan Ji <xu...@gmail.com>.
I vote for sticking with logging's format
`logging.info("Some message with a param: %s", param)`
because it has less parentheses :)
On 26 October 2016 at 20:40, Maxime Beauchemin
<ma...@gmail.com> wrote:
> The history here is we started the project with Flake8 / PEP8 and
> eventually turned on PyLint through Landscape (which has been a nice
> service, but is often down, not picking up our PRs)
>
> Unless I'm missing something I think the cost of formatting strings that
> may not get logged is most likely pretty insignificant. But I could get
> convinced either way (removing the linting rule, or refactoring the code).
>
> Code wins argument. If someone can sign up for doing the task of cleaning
> this up we'll go in that direction, otherwise I vote for blacklisting the
> lint rule.
>
> btw I can't wait for f-strings!
> https://www.python.org/dev/peps/pep-0498/
>
> Max
>
> On Wed, Oct 26, 2016 at 1:44 PM, George Leslie-Waksman <
> george@cloverhealth.com.invalid> wrote:
>
>> There are a few layers of things going on here.
>>
>> The first, and the reason for the landscape warning, is that logger
>> performs string formatting lazily and doing the formatting explicitly
>> results in extra processing for log levels that are being ignored.
>>
>> The second is that the logger can be configured for % or format style
>> string formatting and the airflow logger is configured of % style.
>>
>> It would be best if we chose a style for logging and then always used that
>> style, with lazy processing for logging.
>>
>> I have seen projects that use % for logging (for log handler compatibility)
>> and {} for general string formatting.
>>
>> --George
>>
>> On Mon, Oct 17, 2016 at 8:22 AM Andrew Phillips <an...@apache.org>
>> wrote:
>>
>> > > My understanding from the latest python documentation is that
>> > > '.format' supersedes the use of '%'. Therefore, it seemed a little
>> > > strange that the Landscape configuration was advising the use of '%',
>> >
>> >
>> > I think the thing here is that the "%" that the warning is talking about
>> > is *not* the same operator that has been superseded by format, although
>> > it was very likely chosen to look similar, which is confusing.
>> >
>> > The warning is not telling the user to go back from:
>> >
>> > logging.info("some message with a param {}".format(param))
>> >
>> > to
>> >
>> > logging.info("some message with a param %s" % (param))
>> >
>> > but to a different argument passing style:
>> >
>> > logging.info("some message with a param %s", param)
>> >
>> > Having said that, I'd also be up for disabling this warning if it's
>> > perceived as more of a nuisance that a useful hint.
>> >
>> > Regards
>> >
>> > ap
>> >
>>
--
Im Xuan Ji!
Re: String formatting
Posted by Maxime Beauchemin <ma...@gmail.com>.
The history here is we started the project with Flake8 / PEP8 and
eventually turned on PyLint through Landscape (which has been a nice
service, but is often down, not picking up our PRs)
Unless I'm missing something I think the cost of formatting strings that
may not get logged is most likely pretty insignificant. But I could get
convinced either way (removing the linting rule, or refactoring the code).
Code wins argument. If someone can sign up for doing the task of cleaning
this up we'll go in that direction, otherwise I vote for blacklisting the
lint rule.
btw I can't wait for f-strings!
https://www.python.org/dev/peps/pep-0498/
Max
On Wed, Oct 26, 2016 at 1:44 PM, George Leslie-Waksman <
george@cloverhealth.com.invalid> wrote:
> There are a few layers of things going on here.
>
> The first, and the reason for the landscape warning, is that logger
> performs string formatting lazily and doing the formatting explicitly
> results in extra processing for log levels that are being ignored.
>
> The second is that the logger can be configured for % or format style
> string formatting and the airflow logger is configured of % style.
>
> It would be best if we chose a style for logging and then always used that
> style, with lazy processing for logging.
>
> I have seen projects that use % for logging (for log handler compatibility)
> and {} for general string formatting.
>
> --George
>
> On Mon, Oct 17, 2016 at 8:22 AM Andrew Phillips <an...@apache.org>
> wrote:
>
> > > My understanding from the latest python documentation is that
> > > '.format' supersedes the use of '%'. Therefore, it seemed a little
> > > strange that the Landscape configuration was advising the use of '%',
> >
> >
> > I think the thing here is that the "%" that the warning is talking about
> > is *not* the same operator that has been superseded by format, although
> > it was very likely chosen to look similar, which is confusing.
> >
> > The warning is not telling the user to go back from:
> >
> > logging.info("some message with a param {}".format(param))
> >
> > to
> >
> > logging.info("some message with a param %s" % (param))
> >
> > but to a different argument passing style:
> >
> > logging.info("some message with a param %s", param)
> >
> > Having said that, I'd also be up for disabling this warning if it's
> > perceived as more of a nuisance that a useful hint.
> >
> > Regards
> >
> > ap
> >
>
Re: String formatting
Posted by George Leslie-Waksman <ge...@cloverhealth.com.INVALID>.
There are a few layers of things going on here.
The first, and the reason for the landscape warning, is that logger
performs string formatting lazily and doing the formatting explicitly
results in extra processing for log levels that are being ignored.
The second is that the logger can be configured for % or format style
string formatting and the airflow logger is configured of % style.
It would be best if we chose a style for logging and then always used that
style, with lazy processing for logging.
I have seen projects that use % for logging (for log handler compatibility)
and {} for general string formatting.
--George
On Mon, Oct 17, 2016 at 8:22 AM Andrew Phillips <an...@apache.org> wrote:
> > My understanding from the latest python documentation is that
> > '.format' supersedes the use of '%'. Therefore, it seemed a little
> > strange that the Landscape configuration was advising the use of '%',
>
>
> I think the thing here is that the "%" that the warning is talking about
> is *not* the same operator that has been superseded by format, although
> it was very likely chosen to look similar, which is confusing.
>
> The warning is not telling the user to go back from:
>
> logging.info("some message with a param {}".format(param))
>
> to
>
> logging.info("some message with a param %s" % (param))
>
> but to a different argument passing style:
>
> logging.info("some message with a param %s", param)
>
> Having said that, I'd also be up for disabling this warning if it's
> perceived as more of a nuisance that a useful hint.
>
> Regards
>
> ap
>
Re: String formatting
Posted by Andrew Phillips <an...@apache.org>.
> My understanding from the latest python documentation is that
> '.format' supersedes the use of '%'. Therefore, it seemed a little
> strange that the Landscape configuration was advising the use of '%',
I think the thing here is that the "%" that the warning is talking about
is *not* the same operator that has been superseded by format, although
it was very likely chosen to look similar, which is confusing.
The warning is not telling the user to go back from:
logging.info("some message with a param {}".format(param))
to
logging.info("some message with a param %s" % (param))
but to a different argument passing style:
logging.info("some message with a param %s", param)
Having said that, I'd also be up for disabling this warning if it's
perceived as more of a nuisance that a useful hint.
Regards
ap
Re: String formatting
Posted by "Maycock, Luke" <lu...@affiliate.oliverwyman.com>.
Thank you for the quick responses!
My understanding from the latest python documentation is that '.format' supersedes the use of '%'. Therefore, it seemed a little strange that the Landscape configuration was advising the use of '%', which is why I asked the question. I understand having a check in to ensure consistency but I would have expected it to be checking that '.format' is being used, not '%'.
I would not be against Jeremiah's suggestion of disabling this warning. If no-one has any strong objections, we will go ahead and make the change then create a PR.
Cheers,
Luke Maycock
OLIVER WYMAN
luke.maycock@affiliate.oliverwyman.com<ma...@affiliate.oliverwyman.com>
www.oliverwyman.com<http://www.oliverwyman.com/>
________________________________
From: Jeremiah Lowin <jl...@apache.org>
Sent: 17 October 2016 15:23
To: dev@airflow.incubator.apache.org
Cc: Maycock, Luke
Subject: Re: String formatting
Indeed -- though I think the larger question from Luke is whether or not we
want to enforce a certain style of logging message (variable arguments vs
formatting the string itself). Since there's nothing to stop users from
formatting the string on one line and passing it to logging on the next,
not to mention that I don't really see the performance benefit, I think
this warning is silly and should be removed and people can log however they
feel most comfortable. Just my $0.02.
On Mon, Oct 17, 2016 at 10:20 AM Andrew Phillips <ap...@qrmedia.com>
wrote:
> Perhaps I stand corrected! -- though I don't see where it actually says
> this approach is preferred. In any case, the Python 3 docs explicitly
> state
> that the behavior is only maintained for backwards compatibility:
> https://docs.python.org/3/howto/logging.html#logging-variable-data
Ah, interesting! From [1], it appears that it's now possible to change
the placeholder syntax so that the messages look somewhat more "modern".
As far as I can tell, variable components of the log messages are still
intended to be passed as arguments, though?
So with the style change, the log message could look like this:
logging.info("A message with a param: {}", param)
Regards
ap
[1]
https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles
________________________________
This e-mail and any attachments may be confidential or legally privileged. If you received this message in error or are not the intended recipient, you should destroy the e-mail message and any attachments or copies, and you are prohibited from retaining, distributing, disclosing or using any information contained herein. Please inform us of the erroneous delivery by return e-mail. Thank you for your cooperation.
Re: String formatting
Posted by Jeremiah Lowin <jl...@apache.org>.
Indeed -- though I think the larger question from Luke is whether or not we
want to enforce a certain style of logging message (variable arguments vs
formatting the string itself). Since there's nothing to stop users from
formatting the string on one line and passing it to logging on the next,
not to mention that I don't really see the performance benefit, I think
this warning is silly and should be removed and people can log however they
feel most comfortable. Just my $0.02.
On Mon, Oct 17, 2016 at 10:20 AM Andrew Phillips <ap...@qrmedia.com>
wrote:
> Perhaps I stand corrected! -- though I don't see where it actually says
> this approach is preferred. In any case, the Python 3 docs explicitly
> state
> that the behavior is only maintained for backwards compatibility:
> https://docs.python.org/3/howto/logging.html#logging-variable-data
Ah, interesting! From [1], it appears that it's now possible to change
the placeholder syntax so that the messages look somewhat more "modern".
As far as I can tell, variable components of the log messages are still
intended to be passed as arguments, though?
So with the style change, the log message could look like this:
logging.info("A message with a param: {}", param)
Regards
ap
[1]
https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles
Re: String formatting
Posted by Andrew Phillips <ap...@qrmedia.com>.
> Perhaps I stand corrected! -- though I don't see where it actually says
> this approach is preferred. In any case, the Python 3 docs explicitly
> state
> that the behavior is only maintained for backwards compatibility:
> https://docs.python.org/3/howto/logging.html#logging-variable-data
Ah, interesting! From [1], it appears that it's now possible to change
the placeholder syntax so that the messages look somewhat more "modern".
As far as I can tell, variable components of the log messages are still
intended to be passed as arguments, though?
So with the style change, the log message could look like this:
logging.info("A message with a param: {}", param)
Regards
ap
[1]
https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles
Re: String formatting
Posted by Jeremiah Lowin <jl...@apache.org>.
Perhaps I stand corrected! -- though I don't see where it actually says
this approach is preferred. In any case, the Python 3 docs explicitly state
that the behavior is only maintained for backwards compatibility:
https://docs.python.org/3/howto/logging.html#logging-variable-data
On Mon, Oct 17, 2016 at 10:10 AM Andrew Phillips <ap...@qrmedia.com>
wrote:
> > "Use % formatting in logging functions and pass the % parameters as
> > arguments"
> >
> > [...]
> >
> > Can anybody give a judgement on this? If the .format is preferred,
> > then we'll look into changing the landscape.io settings.
>
> As far as I can tell from the Python 2 logging docs [1] at least, the
> following is indeed the preferred approach:
>
> logging.info("Some message with a param: %s", param)
>
> One of the reasons is that the string representation of param does not
> need to be evaluated if the framework can determine that info logging is
> not enabled - this differs from using format, where the string has to be
> calculated irrespective of whether it is ever emitted.
>
> Regards
>
> ap
>
> [1] https://docs.python.org/2/library/logging.html
>
Re: String formatting
Posted by Andrew Phillips <ap...@qrmedia.com>.
> "Use % formatting in logging functions and pass the % parameters as
> arguments"
>
> [...]
>
> Can anybody give a judgement on this? If the .format is preferred,
> then we'll look into changing the landscape.io settings.
As far as I can tell from the Python 2 logging docs [1] at least, the
following is indeed the preferred approach:
logging.info("Some message with a param: %s", param)
One of the reasons is that the string representation of param does not
need to be evaluated if the framework can determine that info logging is
not enabled - this differs from using format, where the string has to be
calculated irrespective of whether it is ever emitted.
Regards
ap
[1] https://docs.python.org/2/library/logging.html
Re: String formatting
Posted by Jeremiah Lowin <jl...@apache.org>.
Good question. Personally I hate this warning, I like new-style formatting
far more than "%"-style formatting (and in fact, what pylint wants is
neither -- it prefers something like `logging.info('my msg is %',
my_msg)`). The stated reason (
https://bitbucket.org/logilab/pylint/pull-requests/169/add-new-warning-logging-format/diff)
is that this defers the string interpolation until it is actually required
(if/when the logger is called) and therefore provides a performance
enhancement -- I find it incredibly hard to believe that this yields any
measurable improvement. In addition, I haven't found any PEP or other
official documentation that says this behavior is preferred (as opposed to
merely possible). I would support disabling the warning entirely.
J
On Mon, Oct 17, 2016 at 10:04 AM Maycock, Luke <
luke.maycock@affiliate.oliverwyman.com> wrote:
> Hi Dev List,
>
> We're currently working on removing all of the new Landscape.io warnings
> from some of our code and we're noticing the following quite a lot:
>
> "Use % formatting in logging functions and pass the % parameters as
> arguments"
>
> This is being flagged up on lines such as:
>
> "logging.info("Running command:\n {}".format(hql))"
>
> We're just wondering whether it is indeed preferred to use the %
> parameters as suggested or whether this is an issue with the Landscape.io
> setup.
>
> Can anybody give a judgement on this? If the .format is preferred, then
> we'll look into changing the landscape.io settings.
>
> Thanks,
>
> Luke Maycock
> OLIVER WYMAN
> luke.maycock@affiliate.oliverwyman.com<mailto:
> luke.maycock@affiliate.oliverwyman.com>
> www.oliverwyman.com<http://www.oliverwyman.com/>
>
>
> ________________________________
> This e-mail and any attachments may be confidential or legally privileged.
> If you received this message in error or are not the intended recipient,
> you should destroy the e-mail message and any attachments or copies, and
> you are prohibited from retaining, distributing, disclosing or using any
> information contained herein. Please inform us of the erroneous delivery by
> return e-mail. Thank you for your cooperation.
>