Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Logging library for python applications deployed on SAP Cloud Platform - CloudFoundry environment

License

NotificationsYou must be signed in to change notification settings

SAP/cf-python-logging-support

 
 
https://api.reuse.software/badge/github.com/SAP/cf-python-logging-support

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.

Features

  1. Lightweight, no dependencies. Support of Python 2.7 & 3.5.
  2. Compatible with the Pythonlogging module. Minimal configuration needed.
  3. Emits JSON logs (formatdetails).
  4. Supportscorrelation-id.
  5. Supports request instrumentation. Built in support for:
  1. Includes CF-specific information (space id, app id, etc.) to logs.
  2. Supports adding extra properties to JSON log object.

Installation

Install the package with pip:

pip install sap_cf_logging

Usage

Setting up your application

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.

Configuration

After installation use the following guide to configure the Python cf logging library.

Flask

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

Sanic

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.

Falcon

importfalconfromsap.cf_loggingimportfalcon_loggingfromsap.cf_logging.core.constantsimportREQUEST_KEYclassResource:defon_get(self,req,resp):extra= {REQUEST_KEY:req}logging.getLogger('my.logger').log('Resource requested',extra=extra)resp.media= {'name':'Cloud Foundry'}app=falcon.API(middleware=[falcon_logging.LoggingMiddleware()])app.add_route('/resource',Resource())falcon_logging.init(app)

Django

django-admin startproject example
# example/settings.pyMIDDLEWARES= [# ...,'sap.cf_logging.django_logging.LoggingMiddleware'    ]# example/wsgi.py# ...fromsap.cf_loggingimportdjango_loggingos.environ.setdefault("DJANGO_SETTINGS_MODULE","sap_logtesting.settings")django_logging.init()# ...

Create a new app

python manage.py startapp example_app
# example_app/views.pyimportloggingfromdjango.httpimportHttpResponsefromsap.cf_logging.core.constantsimportREQUEST_KEYdefindex(request):extra= {REQUEST_KEY:request}logger=logging.getLogger('my.logger')logger.info("Resource requested",extra=extra)returnHttpResponse("ok")# example_app/urls.pyfromdjango.conf.urlsimporturlfrom .importviewsurlpatterns= [url('^$',views.index)    ]# example/urls.pyfromdjango.contribimportadminfromdjango.conf.urlsimporturl,includeurlpatterns= [url('admin/',admin.site.urls),url('example/',include('example_app.urls'))    ]

General

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 before andafter request middleware is attached, and it will capture response times for each request.

Custom Fields

To use custom fields. Pass a dictionary property custom_fields to the initialize method:

importloggingfromsapimportcf_loggingcf_logging.init(custom_fields={"foo":"default","bar":None})

Here we mark the two fields: foo and bar as custom_fields. Logging with:

logging.getLogger('my.logger').debug('Hi')

The property foo will be output as a custom field with a value "default". The property bar will not be logged, as it does not have a value.

To log bar, provide a value when logging:

logging.getLogger('my.logger').debug('Hi',extra={"bar":"new_value"})

It is also possible to log foo with a different value:

logging.getLogger('my.logger').debug('Hi',extra={"foo":"hello"})
Setting and getting correlation ID

When using cf_logging in a web application you don't need to set the correlation ID, because the logging library will fetch it from the HTTP headers and set it.For non web applications you could set the correlation ID manually, so that the log entries can be filtered later on based on thecorrelation_id log property.In this case the correlation ID is kept in a thread local variable and each thread should set its own correlation ID.

Setting and getting the correlation_id can be done via:

cf_logging.FRAMEWORK.context.get_correlation_id()cf_logging.FRAMEWORK.context.set_correlation_id(value)

If you need to get the correlation ID in a web application, take into account the framework you are using.In async frameworks like Sanic and Falcon the context is stored into the request object and you need to provide the request to the call:

cf_logging.FRAMEWORK.context.get_correlation_id(request)

Logging sensitive data

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 variableValueEnables sensitive field
LOG_SENSITIVE_CONNECTION_DATAtrueremote_ip,remote_host,remote_port,x_forwarded_for
LOG_REMOTE_USERtrueremote_user
LOG_REFERERtruereferer

This behavior matches the corresponding mechanism in theCF Java Logging Support library.

Examples

For more examples please see the tests within the./tests/ directory.

Requirements

No external requirements are needed to run the package.

Limitations

NA

Known Issues

NA

How to obtain support

Please open an issue on the github page.

Contributing

Please create a pull request and briefly describe the nature of the change. Please submit a testcase along with your pull request.

To-Do (upcoming changes)

NA

Changelog

SeeCHANGELOG file.

License

Copyright (c) 2017-2021 SAP SE or an SAP affiliate company and cf-python-logging-support contributors. Please see ourLICENSE file for copyright and license information. Detailed information including third-party components and their licensing/copyright information is availablevia the REUSE tool.

About

Logging library for python applications deployed on SAP Cloud Platform - CloudFoundry environment

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors12

Languages


[8]ページ先頭

©2009-2025 Movatter.jp