python - logging.basicConfig format parameter is invalid

Has this to say python code

import threading
import time
import requests
from decimal import Decimal, ROUND_DOWN
import logging
import os
import sys
import randomfrom utils import common, filter, cache
from configs import settings

logging.basicConfig(level=logging.INFO, format='%(levelname)s %(asctime)s [line:%(lineno)d]  %(message)s')

 

No matter how basicConfig set in the value, have been unable to take effect, then see a saying: before calling basicConfig function, because the import of the other package, while the other bag and imported logging package, has led to setting basicConfig unsuccessful . A investigation, and indeed common cache bag and imported logging.

Trimming code sequence as follows:

import os
import sys
import random
import threading
import time
import requests
from decimal import Decimal, ROUND_DOWN
import logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s %(asctime)s [line:%(lineno)d]  %(message)s')

this_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(this_dir, '..'))
from utils import common, filter, cache
from configs import settings

Indeed, it goes into effect.

 

By the investigation, " before calling basicConfig function, because the import of the other package, while the other bag and imported logging package, has led to an unsuccessful set basicConfig " This statement is not enough, should be " before calling basicConfig function, because the import of other packages, while the other bag and imported logging package, and also called basicConfig function , leads to setting basicConfig unsuccessful . "

why? On basicConfig Source:

def basicConfig(**kwargs):
    _acquireLock()
    try:
        if len(root.handlers) == 0:
            filename = kwargs.get("filename")
            if filename:
                mode = kwargs.get("filemode", 'a')
                hdlr = FileHandler(filename, mode)
            else:
                stream = kwargs.get("stream")
                hdlr = StreamHandler(stream)
            fs = kwargs.get("format", BASIC_FORMAT)
            dfs = kwargs.get("datefmt", None)
            fmt = Formatter(fs, dfs)
            hdlr.setFormatter(fmt)
            root.addHandler(hdlr)
            level = kwargs.get("level")
            if level is not None:
                root.setLevel(level)
    finally:
        _releaseLock()

Because, in other places it's called a basicConfig functions in the current file and then call basicConfig time, you will find that the length of the len (root.handlers) is no longer 0, so do not take the lead if len (root.handlers) == 0, so the invalid log format settings.

 

Guess you like

Origin www.cnblogs.com/hf8051/p/11727520.html