Package: temlogger
TemLoggerTemloggeris a library to sends logs to providers such as ELK and
StackDriver(Google Cloud Logging). Temlogger can be used in any python
3.6+ application.FeaturesTemlogger gives you:Flexibility to send logs:StackDriver(Google Cloud Logging)ELK (Elastic, Logstash and Kibana)Console (Default logging output)Register events handlers(globally and per logger) to update log entry
before send to providers.99% test coverage.Logging Providerslogstash(ELK)stackdriver(Google StackDriver)console(Display logs on Console)default(don’t send logs)Requirementspython 3.6+python3-logstash == 0.4.80google-cloud-logging>=1.14.0,<2Instalationpip install temloggerConfigurationTemlogger can be used with environment variables or programmatically.Example of configuration with environment variables to Console provider:exportTEMLOGGER_APP_NAME='your-app-name'exportTEMLOGGER_PROVIDER='console'exportTEMLOGGER_ENVIRONMENT='staging'exportTEMLOGGER_LOG_LEVEL='INFO'importsysimporttemloggertest_logger.info('python-console: test console info message.')test_logger.debug('python-console: debug message will not be displayed. Change level to "DEBUG"')test_logger.warning('python-console: test console warning message.')Example of configuration programmatically to Console provider:importsysimporttemloggertemlogger.config.set_app_name('your-app-name')temlogger.config.set_provider('console')temlogger.config.set_environment('staging')temlogger.config.set_log_level('INFO')test_logger.info('python-console: test console info message.')test_logger.debug('python-console: debug message will not be displayed. Change level to "DEBUG"')test_logger.warning('python-console: test console warning message.')Parameters to setup Logstash Providerexport TEMLOGGER_APP_NAME='your-app-name'
export TEMLOGGER_PROVIDER='logstash'
export TEMLOGGER_URL='<logstash url>'
export TEMLOGGER_PORT='<logstash port>'
export TEMLOGGER_ENVIRONMENT='<your environment>'
export TEMLOGGER_LOG_LEVEL='INFO'Parameters to setup StackDriver ProviderThe variableGOOGLE_APPLICATION_CREDENTIALSis now deprecated and
your use isn’t recommended. UseTEMLOGGER_GOOGLE_CREDENTIALS_BASE64instead.export TEMLOGGER_APP_NAME='your-app-name'
export TEMLOGGER_PROVIDER='stackdriver'
export TEMLOGGER_ENVIRONMENT='<your environment>'
export TEMLOGGER_GOOGLE_CREDENTIALS_BASE64='<your google json creds as base64>'
export TEMLOGGER_LOG_LEVEL='INFO'To encode your google credentials use:base64<googleapplicationcredentialspath>Parameters to setup Console Providerexport TEMLOGGER_PROVIDER='console'
export TEMLOGGER_ENVIRONMENT='<your environment>'
export TEMLOGGER_LOG_LEVEL='INFO'Usage ExamplesExample with StackDriverIf you have a Google Credentials, step ahead. If not, create one herehttps://console.cloud.google.com/apis/credentials/serviceaccountkey.
It’s recomended to assign just the needed permissions
(logging > write logs).exportTEMLOGGER_APP_NAME='your-app-name'exportTEMLOGGER_PROVIDER='stackdriver'exportTEMLOGGER_GOOGLE_CREDENTIALS_BASE64='<your google json creds as base64>'exportTEMLOGGER_ENVIRONMENT='staging'exportTEMLOGGER_LOG_LEVEL='INFO'importsysimporttemloggerlogger=temlogger.getLogger('python-stackdriver-logger')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'],}logger.info('temlogger: test with extra fields',extra=extra)Example with LogStashexportTEMLOGGER_APP_NAME='your-app-name'exportTEMLOGGER_PROVIDER='logstash'exportTEMLOGGER_URL='localhost'exportTEMLOGGER_PORT='5000'exportTEMLOGGER_ENVIRONMENT='staging'exportTEMLOGGER_LOG_LEVEL='INFO'importsysimporttemloggerlogger=temlogger.getLogger('python-logstash-logger')logger.info('python-logstash: test logstash 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'],}logger.info('temlogger: test with extra fields',extra=extra)Example with ConsoleexportTEMLOGGER_APP_NAME='your-app-name'exportTEMLOGGER_PROVIDER='console'exportTEMLOGGER_ENVIRONMENT='staging'exportTEMLOGGER_LOG_LEVEL='INFO'importsysimporttemloggerlogger=temlogger.getLogger('python-console-logger')logger.info('python-logstash: test logstash info message.')# add extra field to log messageextra={'test_string':'python version: '+repr(sys.version_info),'test_boolean':True,'test_dict':{'a':1,'b':'c'},}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_app_name('your-app-name')temlogger.config.set_provider('logstash')temlogger.config.set_url('localhost')temlogger.config.set_port(5000)temlogger.config.set_environment('staging')temlogger.config.set_log_level('INFO')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_app_name('your-app-name')temlogger.config.set_provider('console')temlogger.config.set_log_level('INFO')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_app_name('your-app-name')temlogger.config.set_provider('console')temlogger.config.set_log_level('INFO')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)