Package: alog
AlogYour goto Python logging without panic on context swtich.Warning:No morelogger = logging.getLogger(__name__)in your every file.>>>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>Test2If you’re new to logging, seeWhy should you use logging instead of print.Installationpip install alogFeaturesInstant logging with expected defaults.You can do logging instantly by reading a small piece of README.
Alog comes with useful defaults:A default logger.Logging level:logging.INFOLogging format:"%(asctime)s %(levelname)-5.5s [parent_module.current_module:%(lineno)s]%(message)s",
"%Y-%m-%d %H:%M:%S"No more__name__whenever you start to do logging in a module.Alog builds the default module names on the fly.Compatible with default Pythonloggingmodule.Alog is built upon default Python logging module. You can configure it by
the same way of default Python logging module when it’s needed.Comparingalogwith Python defaultloggingmoduleComparingalog:In[1]:importalogIn[2]:alog.info("Hello alog!")2016-11-2312:20:34INFO<IPython>Helloalog!withloggingmodule: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!Tipsimportaloga_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)Why should you use logging instead of printThe main goal of logging is to figure out what was going on and to get the
insight.print, by default, does simply pure string output. No timestamp,
no module hint, and no level control, comparing to a pretty logging record.Lets start withaproject/models/user.py:classUser:def__init__(self,user_id,username):...print(username)...What you got output ofprint:>>>admin=User(1,"admin")"admin"Now usealog:importalogclassUser:def__init__(self,user_id,username):...alog.info(username)...What you got output ofalog.info:>>>admin=User(1,"admin")2016-11-2311:32:58INFO[models.user:6]adminIn the output of hundreds of lines, it helps (a lot).What if you have usedprinta log? That’s as easy:importalogprint=alog.info...# A lot of print code no needed to change1.2.0 (2021-08-09)Support Python 3.9Remove Python 3.4 and 3.5 support.1.1.0 (2020-02-10)Support Python 3.8Fix brokenset_formatfunction when formatter argument is given.1.0.0 (2019-04-03)Renamed:turn_logging_datetime(on=True)turn_logging_thread_name(on=False)turn_logging_process_id(on=False)Support most same APIs between alog and Alogger.Addalog.pdir()for handy replacing[attr for attr in dir(obj) if notattr.startswith("_")].0.9.13 (2017-06-18)Fix not able toturn_log_datetime(on=False).0.9.12 (2017-06-16)Support not showing_log_datetime byturn_log_datetime(on=False).0.9.11 (2017-04-07)Addalog.getLogger()for handy replacinglogging.getLogger.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.