Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bringing it to working state #109

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions .github/workflows/code_quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
name: Code Quality
on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]
workflow_dispatch:

jobs:
flake8:
Expand All @@ -13,15 +14,15 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python 3.9
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r test-requirements.txt
pip install -r requirements-test.txt
- name: Flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand All @@ -36,19 +37,19 @@ jobs:

strategy:
matrix:
python-version: ["3.7", "3.8", "3.9"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: "Git checkout"
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r test-requirements.txt
pip install -r requirements-test.txt
- name: Run tests
run: |
pytest --ignore tests/smoketests
Expand All @@ -63,11 +64,11 @@ jobs:

steps:
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: 3.9
- name: "Git checkout"
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](http://semver.org/).
This project adheres to [Semantic Versioning](http://semver.org/).
The format is based on [Keep a Changelog](http://keepachangelog.com/).

## [Unreleased]

### Changed

- refactor for Python 3.12

## 1.4.1rc0 - 2021-04-23

- fix #77 Implement extra properties from json-logging-py
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ author.
# Content

1. [Features](#1-features)
2. [Usage](#2-usage)
2.1 [Non-web application log](#21-non-web-application-log)
2.2 [Web application log](#22-web-application-log)
2.3 [Get current correlation-id](#23-get-current-correlation-id)
2.4 [Log extra properties](#24-log-extra-properties)
2.5 [Root logger](#25-root-logger)
2.6 [Custom log formatter](#26-custom-log-formatter)
2. [Usage](#2-usage)
2.1 [Non-web application log](#21-non-web-application-log)
2.2 [Web application log](#22-web-application-log)
2.3 [Get current correlation-id](#23-get-current-correlation-id)
2.4 [Log extra properties](#24-log-extra-properties)
2.5 [Root logger](#25-root-logger)
2.6 [Custom log formatter](#26-custom-log-formatter)
2.7 [Exclude certain URL from request instrumentation](#27-exclude-certain-url-from-request-instrumentation)
3. [Configuration](#3-configuration)
4. [Python References](#4-python-references)
Expand Down Expand Up @@ -259,8 +259,8 @@ json_logging.config_root_logger()
## 2.6 Custom log formatter

Customer JSON log formatter can be passed to init method. see examples for more
detail: [non web](https://github.com/thangbn/json-logging-python/blob/master/example/custom_log_format.py),
[web](https://github.com/thangbn/json-logging-python/blob/master/example/custom_log_format_request.py)
detail: [non web](https://github.com/virajkanwade/json-logging-python/blob/master/example/custom_log_format.py),
[web](https://github.com/virajkanwade/json-logging-python/blob/master/example/custom_log_format_request.py)

## 2.7 Exclude certain URl from request instrumentation

Expand Down Expand Up @@ -298,7 +298,7 @@ TODO: update Python API docs on Github page

To add support for a new web framework, you need to extend following classes in [**
framework_base**](/blob/master/json_logging/framework_base.py) and register support using [**
json_logging.register_framework_support**](https://github.com/thangbn/json-logging-python/blob/master/json_logging/__init__.py#L38)
json_logging.register_framework_support**](https://github.com/virajkanwade/json-logging-python/blob/master/json_logging/__init__.py#L38)
method:

Class | Description | Mandatory
Expand Down
15 changes: 8 additions & 7 deletions example/connexion-example/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys

import connexion

import json_logging

# init the logger as usual
Expand All @@ -13,23 +14,23 @@

def post_greeting(name):
logger.info("test log statement")
logger.info("test log statement with extra props", extra={'props': {"extra_property": 'extra_value'}})
return 'Hello {name}'.format(name=name)
logger.info("test log statement with extra props", extra={"props": {"extra_property": "extra_value"}})
return f"Hello {name}"


def exclude_from_request_instrumentation(name):
return 'Hello {name}. this request wont log request instrumentation information'.format(name=name)
return f"Hello {name}. this request wont log request instrumentation information"


def create():
app = connexion.FlaskApp(__name__, port=9090, specification_dir='openapi/')
app = connexion.FlaskApp(__name__, port=9090, specification_dir="openapi/")
json_logging.init_connexion(enable_json=True)
json_logging.init_request_instrument(app, exclude_url_patterns=[r'/exclude_from_request_instrumentation'])
json_logging.init_request_instrument(app, exclude_url_patterns=[r"/exclude_from_request_instrumentation"])

app.add_api('helloworld-api.yaml', arguments={'title': 'Hello World Example'})
app.add_api("helloworld-api.yaml", arguments={"title": "Hello World Example"})
return app


if __name__ == '__main__':
if __name__ == "__main__":
app = create()
app.run()
11 changes: 4 additions & 7 deletions example/custom_log_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@


def extra(**kw):
'''Add the required nested props layer'''
return {'extra': {'props': kw}}
"""Add the required nested props layer"""
return {"extra": {"props": kw}}


class CustomJSONLog(json_logging.formatters.JSONLogFormatter):
Expand All @@ -18,10 +18,7 @@ class CustomJSONLog(json_logging.formatters.JSONLogFormatter):
"""

def format(self, record):
json_customized_log_object = ({
"customized_prop": "customized value",
"message": record.getMessage()
})
json_customized_log_object = {"customized_prop": "customized value", "message": record.getMessage()}

return json.dumps(json_customized_log_object)

Expand All @@ -35,4 +32,4 @@ def format(self, record):
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stderr))

logger.info('sample log message')
logger.info("sample log message")
38 changes: 21 additions & 17 deletions example/custom_log_format_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,52 @@ def _format_log_object(self, record, request_util):
request = record.request_response_data._request
response = record.request_response_data._response

json_log_object = super(CustomRequestJSONLog, self)._format_log_object(record, request_util)
json_log_object.update({
"customized_prop": "customized value",
})
json_log_object = super()._format_log_object(record, request_util)
json_log_object.update(
{
"customized_prop": "customized value",
}
)
return json_log_object


class CustomDefaultRequestResponseDTO(json_logging.dto.DefaultRequestResponseDTO):
"""
custom implementation
custom implementation
"""

def __init__(self, request, **kwargs):
super(CustomDefaultRequestResponseDTO, self).__init__(request, **kwargs)
super().__init__(request, **kwargs)

def on_request_complete(self, response):
super(CustomDefaultRequestResponseDTO, self).on_request_complete(response)
super().on_request_complete(response)
self.status = response.status


app = flask.Flask(__name__)
json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(app, exclude_url_patterns=[r'/exclude_from_request_instrumentation'],
custom_formatter=CustomRequestJSONLog,
request_response_dto_class=CustomDefaultRequestResponseDTO)
json_logging.init_request_instrument(
app,
exclude_url_patterns=[r"/exclude_from_request_instrumentation"],
custom_formatter=CustomRequestJSONLog,
request_response_dto_class=CustomDefaultRequestResponseDTO,
)

# init the logger as usual
logger = logging.getLogger("test logger")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))


@app.route('/')
@app.route("/")
def home():
logger.info("test log statement")
logger.info("test log statement with extra props", extra={'props': {"extra_property": 'extra_value'}})
logger.info("test log statement with extra props", extra={"props": {"extra_property": "extra_value"}})
correlation_id = json_logging.get_correlation_id()
return "hello world" \
"\ncorrelation_id : " + correlation_id
return f"hello world\ncorrelation_id : {correlation_id}"


@app.route('/exception')
@app.route("/exception")
def exception():
try:
raise RuntimeError
Expand All @@ -70,10 +74,10 @@ def exception():
return "Error occurred, check log for detail"


@app.route('/exclude_from_request_instrumentation')
@app.route("/exclude_from_request_instrumentation")
def exclude_from_request_instrumentation():
return "this request wont log request instrumentation information"


if __name__ == "__main__":
app.run(host='0.0.0.0', port=int(5000), use_reloader=False)
app.run(host="0.0.0.0", port=int(5000), use_reloader=False)
37 changes: 19 additions & 18 deletions example/fastapi_sample_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

@app.get('/')

@app.get("/")
async def home():
logger.info("test log statement")
logger.info("test log statement with extra props", extra={'props': {"extra_property": 'extra_value'}})
logger.info("test log statement with extra props", extra={"props": {"extra_property": "extra_value"}})
correlation_id = json_logging.get_correlation_id()
return "hello world" \
"\ncorrelation_id : " + correlation_id
return "hello world" "\ncorrelation_id : " + correlation_id


@app.get('/exception')
@app.get("/exception")
def exception():
try:
raise RuntimeError
Expand All @@ -29,28 +29,29 @@ def exception():
return "Error occurred, check log for detail"


@app.get('/exclude_from_request_instrumentation')
@app.get("/exclude_from_request_instrumentation")
def exclude_from_request_instrumentation():
return "this request wont log request instrumentation information"


if __name__ == "__main__":
import uvicorn

logging_config = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'default_handler': {
'class': 'logging.StreamHandler',
'level': 'DEBUG',
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"default_handler": {
"class": "logging.StreamHandler",
"level": "DEBUG",
},
},
'loggers': {
'': {
'handlers': ['default_handler'],
"loggers": {
"": {
"handlers": ["default_handler"],
}
}
},
}
json_logging.init_fastapi(enable_json=True)
json_logging.init_request_instrument(app, exclude_url_patterns=[r'^/exclude_from_request_instrumentation'])
uvicorn.run(app, host='0.0.0.0', port=5000, log_level="debug", log_config=logging_config)
json_logging.init_request_instrument(app, exclude_url_patterns=[r"^/exclude_from_request_instrumentation"])
uvicorn.run(app, host="0.0.0.0", port=5000, log_level="debug", log_config=logging_config)
22 changes: 12 additions & 10 deletions example/flask_sample_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import sys

import flask
Expand All @@ -7,27 +8,27 @@

app = flask.Flask(__name__)
json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(app, exclude_url_patterns=[r'/exclude_from_request_instrumentation'])
json_logging.init_request_instrument(app, exclude_url_patterns=[r"/exclude_from_request_instrumentation"])

# init the logger as usual
logger = logging.getLogger("test logger")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))


@app.route('/')
@app.route("/")
def home():
logger.info("test log statement")
logger.info("test log statement with extra props", extra={'props': {"extra_property": 'extra_value'}})
logger.info("test log statement with custom correlation id",
extra={'props': {'correlation_id': 'custom_correlation_id'}})
logger.info("test log statement with extra props", extra={"props": {"extra_property": "extra_value"}})
logger.info(
"test log statement with custom correlation id", extra={"props": {"correlation_id": "custom_correlation_id"}}
)

correlation_id = json_logging.get_correlation_id()
return "hello world" \
"\ncorrelation_id : " + correlation_id
return "hello world" "\ncorrelation_id : " + correlation_id


@app.route('/exception')
@app.route("/exception")
def exception():
try:
raise RuntimeError
Expand All @@ -37,10 +38,11 @@ def exception():
return "Error occurred, check log for detail"


@app.route('/exclude_from_request_instrumentation')
@app.route("/exclude_from_request_instrumentation")
def exclude_from_request_instrumentation():
return "this request wont log request instrumentation information"


if __name__ == "__main__":
app.run(host='0.0.0.0', port=int(5000), use_reloader=False)
port = os.getenv("PORT", "5000")
app.run(host="0.0.0.0", port=int(port), use_reloader=False)
Loading