import functools
import logging
def logDeprecated( msg=None, *args, **kwargs ):
"""Log a message at level 'WARNING'
Args:
msg=None : (str)
The message you wish to log.
If no msg is passed, default to 'This function has been deprecated.'
All other args are passed directly to logging.Logger.log().
Keyword Args:
category : (str)
The category for the message to be logged. If not
specified, then an appropriate category will be
determined based on the calling context.
All other keyword args are passed directly to logging.Logger.log().
Raises:
n/a
Returns:
n/a
"""
if not msg:
msg = "This function has been deprecated."
# Make sure category is stripped from kwargs before passing to log().
cat = kwargs.pop('category', None)
if not cat:
cat = _getLoggingCategory()
cat = "{0}.DEPRECATED".format(cat)
logging.getLogger( category=cat ).log( logging.WARNING, msg, *args, **kwargs )
def decoratedLogDeprecated(func):
def thisFunc(*args, **kwargs):
func(*args, **kwargs)
logDeprecated()
return wraps(func)(thisFunc)
@decoratedLogDeprecated
def somefunc():
print "This used to work"
def main():
somefunc()
if __name__ == 'main':
main()
ログに記録されている行番号は、メインの行番号です。実際には、実際の関数の行番号を報告する必要があります。
デコレータを使用してその関数を装飾された関数に挿入する方法はありますか? すべての助けをいただければ幸いです。