How to add function name into `logging.basicConfig`'s argument `format` in Kur?

时间:2017-04-10 01:43:39

标签: python logging

The Problem

When kur -v data mnist.yml, the logger.info will generate a logging header like [INFO 2017-04-10 10:33:41,108 kur.kurfile:97].

However, it only provides folder name kur and filename kurfile, how can I add a function name? Is there an easy way to achieve it by changing the code below?

The format of this header is defined below inside kur.__main__.main():

config = logging.basicConfig if args.no_color else logcolor.basicConfig
config(
    level=loglevel.get(args.verbose, logging.DEBUG),
    format='{color}[%(levelname)s %(asctime)s %(name)s:%(lineno)s]{reset} '
    '%(message)s'.format(
        color='' if args.no_color else '$COLOR',
        reset='' if args.no_color else '$RESET'
    )
)

solution based on the answer suggested by @Harvey below:

config = logging.basicConfig if args.no_color else logcolor.basicConfig
    config(
        level=loglevel.get(args.verbose, logging.DEBUG),
        format='{color}[%(levelname)s %(asctime)s %(name)s %(funcName)s:%(lineno)s]{reset} '
        '%(message)s'.format(
            color='' if args.no_color else '$COLOR',
            reset='' if args.no_color else '$RESET'
        )
    )

1 个答案:

答案 0 :(得分:1)

Try %(funcName) from the documentation for logging.LogRecord