Package: temlogger-test
TemLoggerTemloggeris a library to send logs to ELK, StackDriver(Google Cloud
Logging).FeaturesTemlogger gives you:Flexibility to send logs to StackDriver(Google Cloud Logging) or ELK
(Elastic, Logstash and Kibana).Register events handlers(globally and per logger) to update log entry
before send to providers.98% test coverage.Logging Providerslogstash(ELK)stackdriver(Google StackDriver)default(don’t send logs)Requirementspython 3.6+python3-logstash == 0.4.80google-cloud-logging>=1.14.0,<2Instalationpip install -e git+https://github.com/tembici/temlogger.git#egg=temloggerUse this to specify tag version:pip install -e git+https://github.com/tembici/temlogger.git@<tag-version>#egg=temloggerUsageUsing environment variables:exportTEMLOGGER_PROVIDER='logstash'exportTEMLOGGER_URL='localhost'exportTEMLOGGER_PORT='5000'exportTEMLOGGER_ENVIRONMENT='staging'importsysimporttemloggertest_logger=temlogger.getLogger('python-logstash-logger')test_logger.error('python-logstash: test logstash error message.')test_logger.info('python-logstash: test logstash info message.')test_logger.warning('python-logstash: test logstash warning message.')# add extra field to logstash messageextra={'test_string':'python version: '+repr(sys.version_info),'test_boolean':True,'test_dict':{'a':1,'b':'c'},'test_float':1.23,'test_integer':123,'test_list':[1,2,'3'],}test_logger.info('temlogger: test with extra fields',extra=extra)Example passing parameters directly to temlogger:importsysimporttemloggertemlogger.config.set_provider('logstash')temlogger.config.set_url('localhost')temlogger.config.set_port(5000)temlogger.config.set_environment('staging')test_logger=temlogger.getLogger('python-logstash-logger')test_logger.info('python-logstash: test logstash info message.')# add extra field to logstash messageextra={'test_string':'python version: '+repr(sys.version_info),'test_boolean':True,'test_dict':{'a':1,'b':'c'},'test_float':1.23,'test_integer':123,'test_list':[1,2,'3'],}test_logger.info('temlogger: test with extra fields',extra=extra)Example with StackDriverDocumentation of how set ``GOOGLE_APPLICATION_CREDENTIALS`environment
variable <https://cloud.google.com/docs/authentication/getting-started>`__exportTEMLOGGER_PROVIDER='stackdriver'exportGOOGLE_APPLICATION_CREDENTIALS='<path to json>'importsysimporttemloggertest_logger=temlogger.getLogger('python-stackdriver-logger')test_logger.info('python-stackdriver: test stackdriver info message.')# add extra field to stackdriver messageextra={'test_string':'python version: '+repr(sys.version_info),'test_boolean':True,'test_dict':{'a':1,'b':'c'},'test_float':1.23,'test_integer':123,'test_list':[1,2,'3'],}test_logger.info('temlogger: test with extra fields',extra=extra)Using with DjangoModify yoursettings.pyto integrate temlogger with Django’s
logging:importtemloggerhost='localhost'temlogger.config.set_provider('logstash')temlogger.config.set_url('localhost')temlogger.config.set_port(5000)temlogger.config.set_environment('staging')Then in others files such asviews.py,models.pyyou can use in
this way:importtemloggertest_logger=temlogger.getLogger('python-logger')Event HandlersThis functionality allow register handlers before send log to Logging
Providers.Register event handlers globallyIs recommended initialize event handlers early as possible, for example
insettings.pyfor django. The below example shows how register a
handleradd_tracker_id_to_messageglobally.importtemloggertemlogger.config.set_provider('logstash')temlogger.config.setup_event_handlers(['temlogger.tests.base.add_tracker_id_to_message',])logger=temlogger.getLogger('python-logger')extra={'app_name':'tembici'}logger.info('test with extra fields',extra=extra)Register event handlers per loggerThe below example shows how register a handleradd_user_id_keyfor
one logger.importtemloggerdefadd_user_id_key(message):message['user_id']='User Id'returnmessagetemlogger.config.set_provider('logstash')logger=temlogger.getLogger('python-logger',event_handlers=['temlogger.tests.base.add_tracker_id_to_message',add_user_id_key])extra={'app_name':'tembici'}logger.info('test with extra fields',extra=extra)