你的goto python日志记录没有上下文切换的恐慌
alog的Python项目详细描述
alog
你的goto python日志记录没有上下文切换的恐慌。
警告:每个文件中不再有logger = logging.getLogger(__name__)。
>>>importalog>>>alog.info("Hi.")2016-12-1820:44:30INFO<stdin>Hi.>>>deftest():...alog.info("Test 1")...alog.error("Test 2")...>>>test()2016-12-1820:45:19INFO<stdin:2>Test12016-12-1820:45:19ERROR<stdin:3>Test2>>>alog.set_level("ERROR")>>>test()2016-12-1820:45:41ERROR<stdin:3>Test2
如果您是新手,请参见Why should you use logging instead of print。
安装
pip install alog
功能
具有预期默认值的即时日志记录。
你可以通过阅读一小段自述文件立即登录。 alog带有有用的默认值:
默认记录器。
日志级别:logging.INFO
日志格式:
"%(asctime)s %(levelname)-5.5s [parent_module.current_module:%(lineno)s]%(message)s", "%Y-%m-%d %H:%M:%S"
只要您开始登录模块,就不再需要\u name\uu。
alog动态构建默认模块名。
与默认的pythonlogging模块兼容。
alog是基于默认的python日志模块构建的。您可以通过配置 在需要时使用默认python日志模块的相同方法。
比较alog与python默认值logging模块
比较alog:
In[1]:importalogIn[2]:alog.info("Hello alog!")2016-11-2312:20:34INFO<IPython>Helloalog!
使用logging模块:
In[1]:importloggingIn[2]:logging.basicConfig(...:level=logging.INFO,...:format="%(asctime)s%(levelname)-5.5s "...:"[%(name)s:%(lineno)s] %(message)s")In[3]:# In every file you want to do log, otherwise %(names)s won't work.In[4]:logger=logging.getLogger(__name__)In[5]:logger.info("Hello log!")2016-11-2312:16:30INFO[__main__:1]Hellolog!
提示
importaloga_complex_json_dict={...}# or a_complex_dictalog.info(alog.pformat(a_complex_dict))restaurant=Restaurant(...)alog.info(alog.pdir(restaurant))# or just skip attributes starts with "__":alog.info(alog.pdir(restaurant,str_not_startswith="__"))# instead ofalog.info([attrforattrindir(restaurant)ifattr.startswith("_")])# Play threads?alog.turn_logging_thread_name(on=True)# Processes?alog.turn_logging_process_id(on=True)# No datetime wanted?alog.turn_logging_datetime(on=False)
为什么要使用日志而不是打印
日志记录的主要目标是找出发生了什么,并获取 洞察。print,默认情况下,只做纯字符串输出。没有时间戳, 与漂亮的日志记录相比,没有模块提示,也没有级别控制。
让我们从aproject/models/user.py:
classUser:def__init__(self,user_id,username):...print(username)...
你得到了print:
的输出>>>admin=User(1,"admin")"admin"
现在使用alog:
importalogclassUser:def__init__(self,user_id,username):...alog.info(username)...
你得到了alog.info:
的输出>>>admin=User(1,"admin")2016-11-2311:32:58INFO[models.user:6]admin
在数百行的输出中,它有很大帮助。
如果您使用了printa日志怎么办?很简单:
importalogprint=alog.info...# A lot of print code no needed to change
(未释放)
1.0.0(2019-04-03)
- Renamed:
- ^{tt14}$
- ^{tt15}$
- ^{tt16}$
- Support most same APIs between alog and Alogger.
- Add ^{tt17}$ for handy replacing ^{tt18}$.
0.9.13(2017-06-18)
- Fix not able to ^{tt19}$.
0.9.12(2017-06-16)
- Support not showing_log_datetime by ^{tt19}$.
0.9.11(2017-04-07)
- Add ^{tt21}$ for handy replacing ^{tt22}$.
0.9.10(2017-03-27)
- Default logging format asctime to “%Y-%m-%d %H:%M:%S” instead of “%Y-%m-%d,%H:%M:%S.%f”.
- Update package info and usage (setup.py, README, …).
0.9.9(2016-08-28)
- Update to turn_thread_name and turn_process_id.
0.9.8(2016-08-27)
- Support showing_thread_name and showing_process_id.
- Support global reset.
0.9.7(2016-08-17)
- Better paths log for None default root name.
0.9.6(2016-08-16)
- First public release.