Movatterモバイル変換


[0]ホーム

URL:


Sorry, we no longer support your browser
Please upgrade toMicrosoft Edge,Google Chrome, orFirefox. Learn more about ourbrowser support.
Skip to main content
Stack Overflow
  1. About
  2. For Teams
Loading…
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Return to Answer

added 1 character in body
David Sanders
  • 4.2k
  • 1
  • 26
  • 40

As a side note, you don't have to take a performance hit to use new style formatting with logging. You can pass any object tologging.debug,logging.info, etc. that implements the__str__ magic method. When the logging module has decided that it must emit your message object (whatever it is), it callsstr(message_object) before doing so. So you could do something like this:

import loggingclass NewStyleLogMessage(object):    def __init__(self, message, *args, **kwargs):        self.message = message        self.args = args        self.kwargs = kwargs    def __str__(self):        args = (i() if callable(i) else i for i in self.args)        kwargs = dict((k, v() if callable(v) else v) for k, v in self.kwargs.items())        return self.message.format(*args, **kwargs)N = NewStyleLogMessage# Neither one of these messages are formatted (or calculated) until they're# needed# Emits "Lazily formatted log entry: 123 foo" in loglogging.debug(N('Lazily formatted log entry: {0} {keyword}', 123, keyword='foo'))def expensive_func():    # Do something that takes a long time...    return 'foo'# Emits "Expensive log entry: foo" in loglogging.debug(N('Expensive log entry: {keyword}', keyword=expensive_func))

This is all described in the Python 3 documentation (https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles). However, it will work with Python 2.6 as well (https://docs.python.org/2.6/library/logging.html#using-arbitrary-objects-as-messages).

One of the advantages of using this technique, other than the fact thatitsit's formatting-style-agnostic agnostic, is that it allows for lazy values e.g. the functionexpensive_func above. This provides a more elegant alternative to the advice being given in the Python docs here:https://docs.python.org/2.6/library/logging.html#optimization.

As a side note, you don't have to take a performance hit to use new style formatting with logging. You can pass any object tologging.debug,logging.info, etc. that implements the__str__ magic method. When the logging module has decided that it must emit your message object (whatever it is), it callsstr(message_object) before doing so. So you could do something like this:

import loggingclass NewStyleLogMessage(object):    def __init__(self, message, *args, **kwargs):        self.message = message        self.args = args        self.kwargs = kwargs    def __str__(self):        args = (i() if callable(i) else i for i in self.args)        kwargs = dict((k, v() if callable(v) else v) for k, v in self.kwargs.items())        return self.message.format(*args, **kwargs)N = NewStyleLogMessage# Neither one of these messages are formatted (or calculated) until they're# needed# Emits "Lazily formatted log entry: 123 foo" in loglogging.debug(N('Lazily formatted log entry: {0} {keyword}', 123, keyword='foo'))def expensive_func():    # Do something that takes a long time...    return 'foo'# Emits "Expensive log entry: foo" in loglogging.debug(N('Expensive log entry: {keyword}', keyword=expensive_func))

This is all described in the Python 3 documentation (https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles). However, it will work with Python 2.6 as well (https://docs.python.org/2.6/library/logging.html#using-arbitrary-objects-as-messages).

One of the advantages of using this technique, other than the fact thatits formatting-style-agnostic, is that it allows for lazy values e.g. the functionexpensive_func above. This provides a more elegant alternative to the advice being given in the Python docs here:https://docs.python.org/2.6/library/logging.html#optimization.

As a side note, you don't have to take a performance hit to use new style formatting with logging. You can pass any object tologging.debug,logging.info, etc. that implements the__str__ magic method. When the logging module has decided that it must emit your message object (whatever it is), it callsstr(message_object) before doing so. So you could do something like this:

import loggingclass NewStyleLogMessage(object):    def __init__(self, message, *args, **kwargs):        self.message = message        self.args = args        self.kwargs = kwargs    def __str__(self):        args = (i() if callable(i) else i for i in self.args)        kwargs = dict((k, v() if callable(v) else v) for k, v in self.kwargs.items())        return self.message.format(*args, **kwargs)N = NewStyleLogMessage# Neither one of these messages are formatted (or calculated) until they're# needed# Emits "Lazily formatted log entry: 123 foo" in loglogging.debug(N('Lazily formatted log entry: {0} {keyword}', 123, keyword='foo'))def expensive_func():    # Do something that takes a long time...    return 'foo'# Emits "Expensive log entry: foo" in loglogging.debug(N('Expensive log entry: {keyword}', keyword=expensive_func))

This is all described in the Python 3 documentation (https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles). However, it will work with Python 2.6 as well (https://docs.python.org/2.6/library/logging.html#using-arbitrary-objects-as-messages).

One of the advantages of using this technique, other than the fact thatit's formatting-style agnostic, is that it allows for lazy values e.g. the functionexpensive_func above. This provides a more elegant alternative to the advice being given in the Python docs here:https://docs.python.org/2.6/library/logging.html#optimization.

added 96 characters in body
David Sanders
  • 4.2k
  • 1
  • 26
  • 40

As a side note, you don't have to take a performance hit to use new style formatting with logging. You can pass any object tologging.debug,logging.info, etc. that implements the__str__ magic method. When the logging module has decided that it must emit your message object (whatever it is), it callsstr(message_object) before doing so. So you could do something like this:

import loggingclass NewStyleLogMessage(object):    def __init__(self, message, *args, **kwargs):        self.message = message        self.args = args        self.kwargs = kwargs    def __str__(self):        args = (i() if callable(i) else i for i in self.args)        kwargs = dict((k, v() if callable(v) else v) for k, v in self.kwargs.items())        return self.message.format(*args, **kwargs)N = NewStyleLogMessage# Neither one of these messages are formatted (or calculated) until they're# needed# Emits "Lazily formatted log entry: 123 foo" in loglogging.debug(N('Lazily formatted log entry: {0} {keyword}', 123, keyword='foo')) def expensive_func():    # Do something that takes a long time...    return 'foo'# Emits "Expensive log entry:bar"foo" in loglogging.debug(N('Expensive log entry: {expensivekeyword}', expensive=lambda:'bar'keyword=expensive_func))

This is all described in the Python 3 documentation (https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles). However, it will work with Python 2.6 as well (https://docs.python.org/2.6/library/logging.html#using-arbitrary-objects-as-messages).

One of the advantages of using this technique, other than the fact that its formatting-style-agnostic, is that it allows for lazy values e.g. thelambda value above:functionlambda: 'bar'expensive_func above. This provides a more elegant alternative to the advice being given in the Python docs here:https://docs.python.org/2.6/library/logging.html#optimization.

As a side note, you don't have to take a performance hit to use new style formatting with logging. You can pass any object tologging.debug,logging.info, etc. that implements the__str__ magic method. When the logging module has decided that it must emit your message object (whatever it is), it callsstr(message_object) before doing so. So you could do something like this:

import loggingclass NewStyleLogMessage(object):    def __init__(self, message, *args, **kwargs):        self.message = message        self.args = args        self.kwargs = kwargs    def __str__(self):        args = (i() if callable(i) else i for i in self.args)        kwargs = dict((k, v() if callable(v) else v) for k, v in self.kwargs.items())        return self.message.format(*args, **kwargs)N = NewStyleLogMessage# Neither one of these messages are formatted (or calculated) until they're# needed# Emits "Lazily formatted log entry: 123 foo" in loglogging.debug(N('Lazily formatted log entry: {0} {keyword}', 123, keyword='foo'))# Emits "Expensive log entry:bar" in loglogging.debug(N('Expensive log entry: {expensive}', expensive=lambda:'bar'))

This is all described in the Python 3 documentation (https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles). However, it will work with Python 2.6 as well (https://docs.python.org/2.6/library/logging.html#using-arbitrary-objects-as-messages).

One of the advantages of using this technique, other than the fact that its formatting-style-agnostic, is that it allows for lazy values e.g. thelambda value above:lambda: 'bar'. This provides a more elegant alternative to the advice being given in the Python docs here:https://docs.python.org/2.6/library/logging.html#optimization.

As a side note, you don't have to take a performance hit to use new style formatting with logging. You can pass any object tologging.debug,logging.info, etc. that implements the__str__ magic method. When the logging module has decided that it must emit your message object (whatever it is), it callsstr(message_object) before doing so. So you could do something like this:

import loggingclass NewStyleLogMessage(object):    def __init__(self, message, *args, **kwargs):        self.message = message        self.args = args        self.kwargs = kwargs    def __str__(self):        args = (i() if callable(i) else i for i in self.args)        kwargs = dict((k, v() if callable(v) else v) for k, v in self.kwargs.items())        return self.message.format(*args, **kwargs)N = NewStyleLogMessage# Neither one of these messages are formatted (or calculated) until they're# needed# Emits "Lazily formatted log entry: 123 foo" in loglogging.debug(N('Lazily formatted log entry: {0} {keyword}', 123, keyword='foo')) def expensive_func():    # Do something that takes a long time...    return 'foo'# Emits "Expensive log entry:foo" in loglogging.debug(N('Expensive log entry: {keyword}',keyword=expensive_func))

This is all described in the Python 3 documentation (https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles). However, it will work with Python 2.6 as well (https://docs.python.org/2.6/library/logging.html#using-arbitrary-objects-as-messages).

One of the advantages of using this technique, other than the fact that its formatting-style-agnostic, is that it allows for lazy values e.g. thefunctionexpensive_func above. This provides a more elegant alternative to the advice being given in the Python docs here:https://docs.python.org/2.6/library/logging.html#optimization.

deleted 16 characters in body
David Sanders
  • 4.2k
  • 1
  • 26
  • 40

As a side note, you don't have to take a performance hit to use new style formatting with logging. You can pass any object tologging.debug,logging.info, etc. that implements the__str__ magic method. When the logging module has decided that it must emit your message object (whatever it is), it callsstr(message_object) before doing so. So you could do something like this:

import loggingclass NewStyleLogMessage(object):    def __init__(self, message, *args, **kwargs):        self.message = message        self.args = args        self.kwargs = kwargs    def __str__(self):        args = (i() if callable(i) else i for i in self.args)        kwargs = dict((k, v() if callable(v) else v) for k, v in self.kwargs.items())        return self.message.format(*args, **kwargs)N = NewStyleLogMessage# Neither one of these messages are formatted (or calculated) until they're# needed# Emits "Lazily formatted log entry: 123 foo" in loglogging.debug(N('Lazily formatted log entry: {0} {keyword}', 123, keyword='foo'))# Emits "Expensive log entry: bar" in loglogging.debug(N('Expensive log entry: {expensive}', expensive=lambda: 'bar'))

This is all described in the Python 3 documentation (https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles). However, it will work with Python 2.6 as well (https://docs.python.org/2.6/library/logging.html#using-arbitrary-objects-as-messages).

One of the advantages of using this technique, other than the fact that its formatting-style-agnostic, is that it allows for lazy values e.g. the lambda value above:lambda: 'bar'. This provides a more elegant alternative to the advice being given in the Python docs here:https://docs.python.org/2.6/library/logging.html#optimization.

As a side note, you don't have to take a performance hit to use new style formatting with logging. You can pass any object tologging.debug,logging.info, etc. that implements the__str__ magic method. When the logging module has decided that it must emit your message object (whatever it is), it callsstr(message_object) before doing so. So you could do something like this:

import loggingclass NewStyleLogMessage(object):    def __init__(self, message, *args, **kwargs):        self.message = message        self.args = args        self.kwargs = kwargs    def __str__(self):        args = (i() if callable(i) else i for i in self.args)        kwargs = dict((k, v() if callable(v) else v) for k, v in self.kwargs.items())        return self.message.format(*args, **kwargs)N = NewStyleLogMessage# Neither one of these messages are formatted (or calculated) until they're# needed# Emits "Lazily formatted log entry: 123 foo" in loglogging.debug(N('Lazily formatted log entry: {0} {keyword}', 123, keyword='foo'))# Emits "Expensive log entry: bar" in loglogging.debug(N('Expensive log entry: {expensive}', expensive=lambda: 'bar'))

This is all described in the Python 3 documentation (https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles). However, it will work with Python 2.6 as well (https://docs.python.org/2.6/library/logging.html#using-arbitrary-objects-as-messages).

As a side note, you don't have to take a performance hit to use new style formatting with logging. You can pass any object tologging.debug,logging.info, etc. that implements the__str__ magic method. When the logging module has decided that it must emit your message object (whatever it is), it callsstr(message_object) before doing so. So you could do something like this:

import loggingclass NewStyleLogMessage(object):    def __init__(self, message, *args, **kwargs):        self.message = message        self.args = args        self.kwargs = kwargs    def __str__(self):        args = (i() if callable(i) else i for i in self.args)        kwargs = dict((k, v() if callable(v) else v) for k, v in self.kwargs.items())        return self.message.format(*args, **kwargs)N = NewStyleLogMessage# Neither one of these messages are formatted (or calculated) until they're# needed# Emits "Lazily formatted log entry: 123 foo" in loglogging.debug(N('Lazily formatted log entry: {0} {keyword}', 123, keyword='foo'))# Emits "Expensive log entry: bar" in loglogging.debug(N('Expensive log entry: {expensive}', expensive=lambda: 'bar'))

This is all described in the Python 3 documentation (https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles). However, it will work with Python 2.6 as well (https://docs.python.org/2.6/library/logging.html#using-arbitrary-objects-as-messages).

One of the advantages of using this technique, other than the fact that its formatting-style-agnostic, is that it allows for lazy values e.g. the lambda value above:lambda: 'bar'. This provides a more elegant alternative to the advice being given in the Python docs here:https://docs.python.org/2.6/library/logging.html#optimization.

Post Undeleted byDavid Sanders
Post Deleted byDavid Sanders
deleted 16 characters in body
David Sanders
  • 4.2k
  • 1
  • 26
  • 40
Loading
David Sanders
  • 4.2k
  • 1
  • 26
  • 40
Loading
lang-py

[8]ページ先頭

©2009-2025 Movatter.jp