Package: logzero
logzeroRobust and effective logging for Python 2 and 3.Documentation:https://logzero.readthedocs.ioGitHub:https://github.com/metachris/logzeroFeaturesEasy logging to console and/or (rotating) file.Provides a fully configured standardPython logger object.JSON logging (with integratedpython-json-logger)Pretty formatting, including level-specific colors in the console.No dependenciesWindows color output supported bycoloramaRobust against str/bytes encoding problems, works with all kinds of character encodings and special characters.Multiple loggers can write to the same logfile (also across multiple Python files and processes).Global default logger withlogzero.loggerand custom loggers withlogzero.setup_logger(..).Compatible with Python 2 and 3.All contained in asingle file.Licensed under the MIT license.Heavily inspired by theTornado web framework.Installation:python-mpipinstalllogzeroExample Usagefromlogzeroimportloggerlogger.debug("hello")logger.info("info")logger.warning("warn")logger.error("error")# This is how you'd log an exceptiontry:raiseException("this is a demo exception")exceptExceptionase:logger.exception(e)# JSON loggingimportlogzerologzero.json()logger.info("JSON test")# Start writing into a logfilelogzero.logfile("/tmp/logzero-demo.log")# Set a minimum loglevellogzero.loglevel(logzero.WARNING)This is the output:Note: You can find more examples in the documentation:https://logzero.readthedocs.ioJSON loggingJSON logging can be enabled for the default logger withlogzero.json(), or withsetup_logger(json=True)for custom loggers:>>>logzero.json()>>>logger.info("test"){"asctime":"2020-10-21 10:42:45,808","filename":"<stdin>","funcName":"<module>","levelname":"INFO","levelno":20,"lineno":1,"module":"<stdin>","message":"test","name":"logzero_default","pathname":"<stdin>","process":76179,"processName":"MainProcess","threadName":"MainThread"}>>>my_logger=setup_logger(json=True)>>>my_logger.info("test"){"asctime":"2020-10-21 10:42:45,808","filename":"<stdin>","funcName":"<module>","levelname":"INFO","levelno":20,"lineno":1,"module":"<stdin>","message":"test","name":"logzero_default","pathname":"<stdin>","process":76179,"processName":"MainProcess","threadName":"MainThread"}The logged JSON object has these fields:{"asctime":"2020-10-21 10:43:40,765","filename":"test.py","funcName":"test_this","levelname":"INFO","levelno":20,"lineno":9,"module":"test","message":"info","name":"logzero","pathname":"_tests/test.py","process":76204,"processName":"MainProcess","threadName":"MainThread"}Exceptions logged withlogger.exception(e)have these additional JSON fields:{"levelname":"ERROR","levelno":40,"message":"this is a demo exception","exc_info":"Traceback (most recent call last):\n  File \"_tests/test.py\", line 15, in test_this\n    raise Exception(\"this is a demo exception\")\nException: this is a demo exception"}Take a look at the documentation for more information and examples:Documentation:https://logzero.readthedocs.io.InstallationInstalllogzerowithpip:python-mpipinstalllogzeroHere's how you setup a virtualenv and download and run the demo:# Create and activate a virtualenv in ./venv/python3-mvenvvenv
.venv/bin/activate# Install logzeropython-mpipinstalllogzero# Download and run demo.pywgethttps://raw.githubusercontent.com/metachris/logzero/master/examples/demo.py
pythondemo.pyIf you don't havepipinstalled, thisPython installation guidecan guide
you through the process.Alternatively, if you use theAnaconda distribution:$condaconfig--addchannelsconda-forge
$condainstalllogzeroYou can also installlogzerofrom the publicGithub repo:$gitclonehttps://github.com/metachris/logzero.git
$cdlogzero
$pythonsetup.pyinstallContributorsChris HagercarlodrBrian LenzDavid MartinZakaria Zajac(creator ofpython-json-logger)DevelopmentGetting started$gitclonehttps://github.com/metachris/logzero.git
$cdlogzero# Activate virtualenv$python3-mvenvvenv
$.venv/bin/activate# Install main and dev dependencies$pipinstall-e.
$pipinstall-rrequirements_dev.txt# Run the tests$maketest# Run the linter$makelint# Generate the docs (will auto-open in Chrome)$makedocs# You can enable watching mode to automatically rebuild on changes:$makeservedocsTo test with Python 2.7, you can use Docker:dockerrun--rm-it-v/Users/chris/stream/logzero:/mntpython:2.7/bin/bashNow you have a shell with the current directory mounted into/mnt/inside the container.Notespytestis the test runnerCI is run withGithub actions.Download stats:https://pepy.tech/project/logzeroChangelogSee the changelog here:https://github.com/metachris/logzero/blob/master/HISTORY.mdFeedbackAll kinds of feedback and contributions are welcome.Create an issueCreate a pull request@metachrisHistory1.6.5 (2021-03-17)Export loglevels directly (you can use eg.logzero.DEBUGinstead oflogging.DEBUG)setup_default_loggerusebackupCountUpdate dependenciesPRs: (386)[https://github.com/metachris/logzero/pull/386]1.6.3 (2020-11-15)JSON logging with UTF-8 enabled by default (PR 357)1.6.0 (1.6.2) (2020-10-29)JSON logging support (PR 344)Ability to easily change colors (#82)Allow creating a root logger (#342)Bugfix: file logging with lower loglevel than stream (PR 338)Running tests with Python up to 3.9Dependency updates1.5.0 (2018-03-07)logzero.syslog(..)(PR 83)1.4.0 (2018-03-02)Allow Disabling stderr Output (PR 83)1.3.0 (2017-07-19)Color output now works in Windows (supported by colorama)1.2.1 (2017-07-09)Logfiles with custom loglevels (eg. stream handler with DEBUG and
file handler with ERROR).1.2.0 (2017-07-05)Way better API for configuring the default logger withlogzero.loglevel(..),logzero.logfile(..), etc.Built-in rotating logfile support.importloggingimportlogzerofromlogzeroimportlogger# This log message goes to the consolelogger.debug("hello")# Set a minimum log levellogzero.loglevel(logging.INFO)# Set a logfile (all future log messages are also saved there)logzero.logfile("/tmp/logfile.log")# Set a rotating logfile (replaces the previous logfile handler)logzero.logfile("/tmp/rotating-logfile.log",maxBytes=1000000,backupCount=3)# Disable logging to a filelogzero.logfile(None)# Set a custom formatterformatter=logging.Formatter('%(name)s-%(asctime)-15s-%(levelname)s:%(message)s');logzero.formatter(formatter)# Log some variableslogger.info("var1:%s, var2:%s",var1,var2)1.1.2 (2017-07-04)Better reconfiguration of handlers, doesn't remove custom handlers
anymore1.1.0 (2017-07-03)Bugfix: Disabled color logging to logfile1.1.0 (2017-07-02)Global default logger instance (logzero.logger)Ability to reconfigure the default logger with (logzero.setup_default_logger(..))More testsMore documentation1.0.0 (2017-06-27)Cleanup and documentation0.2.0 (2017-06-12)Working logzero package with code and tests0.1.0 (2017-06-12)First release on PyPI.