实用:python中的线程处理日志

import threading
import time
import logging
formatstr = "%(asctime)s %(thread)d %(message)s %(schoolname)s"
logging.basicConfig(level=logging.INFO,format=formatstr,datefmt="%Y-%m-%d %H:%M:%S",filename='/tmp/text.txt')

d = {'schoolname':'python.org'}

def add(x, y):
    # logging.info('{} {}'.format(threading.enumerate(),x+y))
    logging.warning("{} {}".format(threading.enumerate(),x+y),extra=d)
    logging.info("%s %s",x,y,extra=d)



t = threading.Timer(1, add, args=(4, 5))
t.start()

log = logging.getLogger('')
print(log.name)
print(log,type(log))

运行结果:(日志被重定向到了文件)

root
 

文件内容:

yzx@yzx-PC:~/PycharmProjects/python$ cat /tmp/text.txt 
2019-07-05 21:11:37 140294948624128 [<_MainThread(MainThread, stopped 140294973105920)>, ] 9 python.org
2019-07-05 21:11:37 140294948624128 4 5 python.org

日志的输出

import logging

formatstr = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.WARNING, format=formatstr, datefmt="%Y-%m-%d %H:%M:%S")

root = logging.getLogger()
print(root.name, id(root))
# root.info('my root')  打印不出来
root.warning('my root')

loga = logging.getLogger(__name__)
print(loga.name, id(loga), id((loga.parent)))
# loga.info('my loga')
loga.warning('my loga')

logab = logging.getLogger("{}.{}".format(__name__,'echo'))
print(logab.name, id(logab), id(logab.parent))
logab.warning('my logab')

运行结果:

2019-07-05 22:01:53 139736248575744 my root
2019-07-05 22:01:53 139736248575744 my loga
2019-07-05 22:01:53 139736248575744 my logab
root 139736224121128
__main__ 139736247135760 139736224121128
__main__.echo 139736224121352 139736247135760

日志的级别

import logging

formatstr = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.WARNING, format=formatstr, datefmt="%Y-%m-%d %H:%M:%S")

root = logging.getLogger()
print(root.name, id(root))
# root.info('my root')  打印不出来
root.warning('my root')

loga = logging.getLogger(__name__)
print(loga.name, id(loga), id((loga.parent)))
print(loga.getEffectiveLevel())
loga.warning('before')

loga.setLevel(28)
print(loga.getEffectiveLevel())
loga.info('after-1')
loga.warning('after-2')

运行结果:

root 140289207099688
__main__ 140289230114264 140289207099688
30
28
2019-07-05 22:08:41 140289231554304 my root
2019-07-05 22:08:41 140289231554304 before
2019-07-05 22:08:41 140289231554304 after-2

日志等级的继承以及个性化打印格式

    import logging
    
    formatstr = "%(asctime)s %(thread)d %(message)s"
    logging.basicConfig(level=logging.ERROR, format=formatstr, datefmt="%Y-%m-%d %H:%M:%S")
    
    root = logging.getLogger()
    print(root.getEffectiveLevel())
    
    log1 = logging.getLogger('s')
    # log1.setLevel(logging.WARNING)
    log1.setLevel(logging.INFO)
    print(log1.getEffectiveLevel())
    
    hdlr1 = logging.StreamHandler()
    # hdlr1 = logging.FileHandler('/tmp/txt1')
    fmtr = logging.Formatter('%(message)s --- (%(asctime)s %(thread)d %(threadName)s)')
    hdlr1.setFormatter(fmtr)
    hdlr1.setLevel(logging.INFO)
    log1.addHandler(hdlr1)
    
    log2 = logging.getLogger('s.s1')
    print(log2.getEffectiveLevel())
    
    log2.info('log2 info')
运行结果:

    40
    20
    20
    log2 info --- (2019-07-06 09:18:28,465 140472704694016 MainThread)
    2019-07-06 09:18:28 140472704694016 log2 info
日志流向:
import logging
formatstr = "%(asctime)s %(thread)d %(message)s........"
formatstr1 = logging.Formatter('%(asctime)s %(thread)d %(message)s-----------')
formatstr2 = logging.Formatter("%(asctime)s %(thread)d %(message)s===========")
#父
logging.basicConfig(level=logging.INFO, format=formatstr, datefmt="%Y-%m-%d %H:%M:%S")

#子
log1 = logging.getLogger('s')
log1.setLevel(logging.WARNING)

#子-handler
hdlr1 = logging.StreamHandler()
hdlr1.setLevel(logging.INFO)
hdlr1.setFormatter(formatstr1)
log1.addHandler(hdlr1)

#孙
log2 = logging.getLogger('s.s1')
log2.setLevel(logging.INFO)

#孙-hanlder
hdlr2 = logging.StreamHandler()
hdlr2.setLevel(logging.INFO)
# hdlr2.setFormatter(formatstr2) #如果不设置格式则默认打印%(message)s
log2.addHandler(hdlr2)

# log2.propagate=False  #如果设置为False则消息不往上级传递日志流
log2.info('log2 info')

运行结果:

log2 info
2019-07-12 14:01:13,810 140499023283968 log2 info-----------
2019-07-12 14:01:13 140499023283968 log2 info........

日志的过滤

import logging

formatstr = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.ERROR, format=formatstr, datefmt="%Y-%m-%d %H:%M:%S")

root = logging.getLogger()
print(root.getEffectiveLevel())

log1 = logging.getLogger('s')
# log1.setLevel(logging.WARNING)
log1.setLevel(logging.INFO)
print(log1.getEffectiveLevel())

hdlr1 = logging.StreamHandler()
hdlr1 = logging.FileHandler('/tmp/txt1')
fmtr = logging.Formatter('%(message)s --- (%(asctime)s %(thread)d %(threadName)s)')
hdlr1.setFormatter(fmtr)
hdlr1.setLevel(logging.INFO)

fltr = logging.Filter('s')
hdlr1.addFilter(fltr)
log1.addHandler(hdlr1)

log2 = logging.getLogger('s.s1')
print(log2.getEffectiveLevel())

log2.info('log2 info')

运行结果:

40
20
20
2019-07-06 09:47:25 139922302666496 log2 info

文件内容:

yzx@yzx-PC:~/PycharmProjects/python$ cat /tmp/txt1
log2 info --- (2019-07-06 09:34:32,908 139650908985088 MainThread)

你可能感兴趣的:(python)