- Notifications
You must be signed in to change notification settings - Fork10
Logging library for python applications deployed on SAP Cloud Platform - CloudFoundry environment
License
SAP/cf-python-logging-support
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a collection of support libraries for Python applications running on Cloud Foundry thatserve two main purposes: provide (a) means to emit structured application log messages and (b)instrument web applications of your application stack to collect request metrics.
For details on the concepts and log formats, please look at the sibling project forjava loggingsupport.
- Lightweight, no dependencies. Support of Python 2.7 & 3.5.
- Compatible with the Pythonlogging module. Minimal configuration needed.
- Emits JSON logs (formatdetails).
- Supportscorrelation-id.
- Supports request instrumentation. Built in support for:
- Flask 0.1x
- Sanic 0.5.x
- Falcon
- Extensible to support others
- Includes CF-specific information (space id, app id, etc.) to logs.
- Supports adding extra properties to JSON log object.
Install the package with pip:
pip install sap_cf_logging
Logging library needs to be initialized. Depending on you application type, different initializationis used. You should usually do this in your application entrypoint.
For CLI applications you just need to callcf_logging.init()once to configure the library.The library will try to configure future loggers to emit logs in JSON format.
If you are using one of the supported frameworks, check theConfigurationsection to see how to configure it.
Setting up the CloudFoundry environment
In order for your logs to appear in the Kibana dashboard, you have to create anapplication-logsservice instance and bind it to your application.
After installation use the following guide to configure the Python cf logging library.
First import thecf_logging library and setup Flask logging on the application.
fromsap.cf_loggingimportflask_loggingapp=flask.Flask(__name__)flask_logging.init(app,logging.INFO)
Next use Python’s logging library
@app.route('/')defroot_route():logger=logging.getLogger('my.logger')logger.info('Hi')return'ok'
Note the logs generated by the application
importsanicimportloggingfromsanic.responseimportHTTPResponsefromsap.cf_loggingimportsanic_loggingfromsap.cf_logging.core.constantsimportREQUEST_KEYapp=sanic.Sanic('test.cf_logging')sanic_logging.init(app)@app.route('/')asyncdeftwo(request):extra= {REQUEST_KEY:request}logging.getLogger('my.logger').debug('Hi',extra=extra)returnHTTPResponse(body='ok')
Note: With Sanic you need to pass the request with anextra parameter in the logging API.This is needed in order to get thecorrelation_id generated at the beginning of the request orfetched from the HTTP headers.
importfalconfromsap.cf_loggingimportfalcon_loggingclassResource:defon_get(self,req,resp):# Use the log() method of the req object to log additional messagesreq.log('Resource requested')resp.media= {'name':'Cloud Foundry'}app=falcon.API(middleware=[falcon_logging.LoggingMiddleware()])app.add_route('/resource',Resource())falcon_logging.init(app)
Note: Use thelog method ofreq since it will include thecorrelation_id from thereq object in the logs.
importloggingfromsapimportcf_loggingcf_logging.init()logger=logging.getLogger("cli.logger")logger.info('hi')
Notes: - All loggers set up and created before the initialization of the Cloud Foundry logging library willbe left untouched. - When using Flask and Sanic with the logging library a before andafter request middleware is attached, and it will capture response times for each request.
The logging library does not log sensitive fields by default. Those fields are replaced with 'redacted' instead of their original content.The following fields are considered sensitive data:remote_ip,remote_host,remote_port,x_forwarded_for,remote_user,referer.Logging of all or some of these fields can be activated by setting the following environment variables:
| Environment variable | Value | Enables sensitive field |
|---|---|---|
LOG_SENSITIVE_CONNECTION_DATA | true | remote_ip,remote_host,remote_port,x_forwarded_for |
LOG_REMOTE_USER | true | remote_user |
LOG_REFERER | true | referer |
This behavior matches the corresponding mechanism in theCF Java Logging Support library.
For more examples please see the tests within the./tests/ directory.
No external requirements are needed to run the package.
NA
NA
Please open an issue on the github page.
Please create a pull request and briefly describe the nature of the change. Please submit a testcase along with your pull request.
NA
SeeCHANGELOG file.
Copyright (c) 2017 SAP SE or an SAP affiliate company. All rights reserved. This file is licensedunder the Apache Software License, v. 2 except as noted otherwise in theLICENSE file.
About
Logging library for python applications deployed on SAP Cloud Platform - CloudFoundry environment
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors12
Uh oh!
There was an error while loading.Please reload this page.