Package: log4mongo
log4mongo-python is mongo database handler for python logging, part of log4mongo.org project.
log4mongo-python is using pymongo driver -http://github.com/mongodb/mongo-python-driverRequirementspython 3.6+pymongo 3.9+mongo databaseFor more information seedebian_requirements.txtandrequirements.txtfiles.ConfigurationExample handler python configuration:importloggingfromlog4mongo.handlersimportMongoHandlerlogger=logging.getLogger('test')logger.addHandler(MongoHandler(host='localhost'))logger.warning('test')Contextual informationIt is possible to decorate you document with contextual information. There are tow approaches.1.) approachimportloggingfromlog4mongo.handlersimportMongoHandlerhandler=MongoHandler(host='localhost')logger=logging.getLogger('test')logger.addHandler(handler)logging.LoggerAdapter(logger,{'ip':'127.0.0.1'}).info('test')2.) approachimportloggingfromlog4mongo.handlersimportMongoHandlerhandler=MongoHandler(host='localhost')logger=logging.getLogger('test')logger.addHandler(handler)logger.info('test',extra={'ip':'127.0.0.1'})As you can see, second approach is more straightforward and there is no need to use LoggerAdapter.Capped collectionsCapped collections are fixed-size collections that support high-throughput operations that insert, retrieve,
and delete documents based on insertion order. Capped collections work in a way similar
to circular buffers: once a collection fills its allocated space, it makes room for new documents
by overwriting the oldest documents in the collection.Before switching to capped collections, read this document please:http://docs.mongodb.org/manual/core/capped-collections/This behaviour is disabled by default. You can enable this behaviour in constructor withcapped=True:importloggingfromlog4mongo.handlersimportMongoHandlerhandler=MongoHandler(host='localhost',capped=True)Buffered handlerBufferedMongoHandleris a subclass ofMongoHandlerallowing to buffer log messages
and write them all at once to the database. The goal is to avoid too many writes to the database, thus avoiding
too frequent write-locks.
Log message buffer flush happens when the buffer is full, when a critical log message is emitted, and also periodically.
An early buffer flush can happen when a critical message is emitted.
And in order to avoid messages to stay indefinitively in the buffer queue before appearing in database, a periodical
flush happens every X seconds.This periodical flush can also be deactivated withbuffer_periodical_flush_timing=False, thus avoiding
the timer thread to be created.Buffer size is configurable, as well as the log level for early flush (default islogging.CRITICAL):importloggingfromlog4mongo.handlersimportBufferedMongoHandlerhandler=BufferedMongoHandler(host='localhost',# All MongoHandler parameters are validcapped=True,buffer_size=100,# buffer size.buffer_periodical_flush_timing=10.0,# periodical flush every 10 secondsbuffer_early_flush_level=logging.CRITICAL)# early flush levellogger=logging.getLogger().addHandler(handler)Test** Tested on evnironment **Ubuntu 18.04python >=3.6.4pymongo >=3.9.0mongod - db version 4.0.13pytestRunning testsBefore you run the test you must start mongo database. You will do so by this command:$ mongod --dbpath /tmp/To run the test run command:$ python3 setup.py testSee vagrant file to quickly setup the test environment.Original Authorchar0n (Vladimir Gorej)email:vladimir.gorej@gmail.comweb:https://www.linkedin.com/in/vladimirgorej/Current MaitainerOz Nahum Tiramemail:nahumoz@gmail.comweb: oz123.github.ioReferenceshttp://www.mongodb.org/http://docs.python.org/library/logging.htmlhttp://github.com/mongodb/mongo-python-driverhttp://log4mongo.org