项目过程当中写了一个小模块,设计到了日志存储的问题,结果发现了个小问题。python
代码结构以下:数据库
db.py run.py
其中db.py是操做数据库抽象出来的一个类,run.py是业务逻辑代码。两个文件中都有使用Python自带的logging模块,来记录日志。其中前者将日志存入到db_xxx.log下,后者存入run_xxx.log下。函数
二者logging相关代码为:设计
# db.py import logging import time dt = time.time() logging.basicConfig(filename='db_' + str(dt) + '.log', level=logging.INFO) # run.py import logging import time dt = time.time() logging.basicConfig(filename='run_' + str(dt) + '.log', level=logging.INFO)
同时,在run.py中会调用db.py的函数,例如:日志
# db.py class DB(): def __init__(self): xxxx def select(self): logging.info('log from db.py') # run.py from db import DB if __name__ == '__main__': db = DB() db.select() logging.info('log from run.py')
实际运行起来,发现全部的日志都只会存在 db_xxx.log 中,同时并不会产生 run_xxx.log 文件。code
依照上面的结果,猜想run.py文件中,引入的db.py中也有logging的设置,但只会有一个生效。get
写两个py文件first.py和second.py,在second中引用first的函数,看最终日志的输出文件名及顺序。it
内容分别为:class
# first.py import logging class TEST(): def __init__(self, log_type, dt): dt = str(dt) logging.basicConfig(filename='./log/' + log_type + '_' + dt + '.txt', level=logging.INFO) def test_log(self): logging.info('log from first.py') # second.py from first import TEST import time dt = time.time() import logging logging.basicConfig(filename='./log/' + 'second.txt', level=logging.INFO) logging.info('log from second') test = TEST('db', dt) test.test_log() # 结果 ➜ log_dir_test python second.py ➜ log_dir_test ls log second.txt ➜ log_dir_test cat log/second.txt INFO:root:log from second INFO:root:log from first.py
能够看到,文件名为 second.txt,即采用了 second.py 的logging设置。同时,日志输出的顺序也是先输出 second 再是 first。test
此时尝试将second.py的语句顺序作一个调整,调整为下面:
# second.py from first import TEST import time dt = time.time() test = TEST('db', dt) test.test_log() import logging logging.basicConfig(filename='./log/' + 'second.txt', level=logging.INFO) logging.info('log from second') # result ➜ log_dir_test cat log/db_1561088221.83.txt INFO:root:log from first.py INFO:root:log from second
能够看到结果存储到了 db_xxx.txt 中,即采用了 first.py 的logging设置。
再对second.py作调整:
# second.py from first import TEST import time dt = time.time() test = TEST('db', dt) import logging logging.basicConfig(filename='./log/' + 'second.txt', level=logging.INFO) logging.info('log from second') test.test_log() # result ➜ log_dir_test cat log/db_1561088393.36.txt INFO:root:log from second INFO:root:log from first.py
采用了first.py的设置
继续调整second.py:
# second.py from first import TEST import time import logging logging.basicConfig(filename='./log/' + 'second.txt', level=logging.INFO) dt = time.time() test = TEST('db', dt) logging.info('log from second') test.test_log() # result ➜ log_dir_test cat log/second.txt INFO:root:log from second INFO:root:log from first.py
采用了 second.py 的设置
能够发现,两个文件的logging设置,在second.py的顺序,影响了second.py的logging设置。即:
起初以为奇怪,如今想一想仍是比较容易理解的。
假如 second.py 中已经设置了logging,后面引用了first.py的函数,first.py中又设置了logging。若此时又采用 first.py的设置,那后续若是second.py中又使用了logging.xxx怎么办?也就是说,Python会以为混乱,不知所措……
若是仍是想达到 db.py 操做都存储到 db 相关目录下,run日志存储到run目录下怎么办?彷佛没太好的解决办法;不优雅的处理方式,能够采用文件操做……比方说使用with open(xx) as f
去操做,这样的话,比较繁琐。
更好的办法是什么?就是如今Python的这种机制。即 run.py 相关日志都存储到 run 目录下,即便调用了 db.py 的函数,日志也存储到 run 目录下。这样能够保证 run.py 的日志是全的,且时间顺序是正确的,减小了排错的成本。