Skip to content

Rework on #109 #111

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

Open
wants to merge 5 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
12 changes: 6 additions & 6 deletions .github/workflows/code_quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ 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
Expand All @@ -40,11 +40,11 @@ jobs:

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
Expand All @@ -63,11 +63,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
4 changes: 2 additions & 2 deletions example/connexion-example/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
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)
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():
Expand Down
6 changes: 3 additions & 3 deletions example/custom_log_format_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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 = super()._format_log_object(record, request_util)
json_log_object.update({
"customized_prop": "customized value",
})
Expand All @@ -32,10 +32,10 @@ class CustomDefaultRequestResponseDTO(json_logging.dto.DefaultRequestResponseDTO
"""

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


Expand Down
1 change: 0 additions & 1 deletion json_logging/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding=utf-8
import json
import logging
import sys
Expand Down
6 changes: 3 additions & 3 deletions json_logging/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, request, **kwargs):
invoked when request start, where to extract any necessary information from the request object
:param request: request object
"""
super(RequestResponseDTOBase, self).__init__(**kwargs)
super().__init__(**kwargs)
self._request = request

def on_request_complete(self, response):
Expand All @@ -31,14 +31,14 @@ class DefaultRequestResponseDTO(RequestResponseDTOBase):
"""

def __init__(self, request, **kwargs):
super(DefaultRequestResponseDTO, self).__init__(request, **kwargs)
super().__init__(request, **kwargs)
utcnow = datetime.now(timezone.utc)
self._request_start = utcnow
self["request_received_at"] = util.iso_time_format(utcnow)

# noinspection PyAttributeOutsideInit
def on_request_complete(self, response):
super(DefaultRequestResponseDTO, self).on_request_complete(response)
super().on_request_complete(response)
utcnow = datetime.now(timezone.utc)
time_delta = utcnow - self._request_start
self["response_time_ms"] = int(time_delta.total_seconds()) * 1000 + int(time_delta.microseconds / 1000)
Expand Down
15 changes: 6 additions & 9 deletions json_logging/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
except NameError:
basestring = str

if sys.version_info < (3, 0):
EASY_SERIALIZABLE_TYPES = (basestring, bool, dict, float, int, list, type(None))
else:
LOG_RECORD_BUILT_IN_ATTRS.append('stack_info')
EASY_SERIALIZABLE_TYPES = (str, bool, dict, float, int, list, type(None))
LOG_RECORD_BUILT_IN_ATTRS.append('stack_info')
EASY_SERIALIZABLE_TYPES = (str, bool, dict, float, int, list, type(None))


def _sanitize_log_msg(record):
Expand All @@ -45,7 +42,7 @@ class BaseJSONFormatter(logging.Formatter):
base_object_common = {}

def __init__(self, *args, **kw):
super(BaseJSONFormatter, self).__init__(*args, **kw)
super().__init__(*args, **kw)
if json_logging.COMPONENT_ID and json_logging.COMPONENT_ID != json_logging.EMPTY_VALUE:
self.base_object_common["component_id"] = json_logging.COMPONENT_ID
if json_logging.COMPONENT_NAME and json_logging.COMPONENT_NAME != json_logging.EMPTY_VALUE:
Expand Down Expand Up @@ -120,7 +117,7 @@ def format_exception(cls, exc_info):
return ''.join(traceback.format_exception(*exc_info)) if exc_info else ''

def _format_log_object(self, record, request_util):
json_log_object = super(JSONLogFormatter, self)._format_log_object(record, request_util)
json_log_object = super()._format_log_object(record, request_util)

json_log_object.update({
"msg": _sanitize_log_msg(record),
Expand All @@ -144,7 +141,7 @@ class JSONLogWebFormatter(JSONLogFormatter):
"""

def _format_log_object(self, record, request_util):
json_log_object = super(JSONLogWebFormatter, self)._format_log_object(record, request_util)
json_log_object = super()._format_log_object(record, request_util)

if json_logging.CORRELATION_ID_FIELD not in json_log_object:
json_log_object.update({
Expand All @@ -160,7 +157,7 @@ class JSONRequestLogFormatter(BaseJSONFormatter):
"""

def _format_log_object(self, record, request_util):
json_log_object = super(JSONRequestLogFormatter, self)._format_log_object(record, request_util)
json_log_object = super()._format_log_object(record, request_util)

request_adapter = request_util.request_adapter
response_adapter = request_util.response_adapter
Expand Down
1 change: 0 additions & 1 deletion json_logging/framework/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
# coding=utf-8
1 change: 0 additions & 1 deletion json_logging/framework/connexion/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding=utf-8
import logging
import sys

Expand Down
1 change: 0 additions & 1 deletion json_logging/framework/fastapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

def is_fastapi_present():
# noinspection PyPep8,PyBroadException
try:
Expand Down
1 change: 0 additions & 1 deletion json_logging/framework/flask/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding=utf-8
import logging

import json_logging
Expand Down
1 change: 0 additions & 1 deletion json_logging/framework/quart/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding=utf-8
import logging
import sys

Expand Down
1 change: 0 additions & 1 deletion json_logging/framework/sanic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding=utf-8
import logging
import logging.config

Expand Down
1 change: 0 additions & 1 deletion json_logging/framework_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding=utf-8
class BaseRequestInfoExtractor:
"""
Helper class help to extract logging-relevant information from HTTP request object
Expand Down
7 changes: 3 additions & 4 deletions json_logging/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# coding=utf-8
import logging
import os
import re
import sys
from datetime import datetime
from datetime import datetime, timezone
from logging import Logger, StreamHandler

import json_logging
Expand Down Expand Up @@ -71,7 +70,7 @@ def validate_subclass(subclass, superclass):
return True


_epoch = datetime(1970, 1, 1)
_epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Making _epoch a timezone-aware datetime object is a good step towards modernizing the codebase. However, this change will cause a TypeError at runtime. The function epoch_nano_second will now fail when called with a timezone-naive datetime object, as it's not possible to subtract a naive from an aware datetime.

For example, json_logging.formatters.BaseJSONFormatter._format_log_object calls it with a naive datetime:

# json_logging/formatters.py:61
utcnow = datetime.now(timezone.utc).replace(tzinfo=None)
...
# json_logging/formatters.py:65
"written_ts": json_logging.util.epoch_nano_second(utcnow),

To fix this, you need to ensure that all callers of epoch_nano_second pass a timezone-aware datetime object. The most direct fix would be to remove .replace(tzinfo=None) in formatters.py, but that change is outside the scope of this diff. Please ensure all call sites are updated to prevent runtime failures.



def epoch_nano_second(datetime_):
Expand All @@ -96,7 +95,7 @@ def currentframe(_no_of_go_up_level):
return sys.exc_info()[_no_of_go_up_level - 1].tb_frame.f_back


class RequestUtil(object):
class RequestUtil:
"""
util for extract request's information
"""
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
if version_info_major == 3:
long_description = open('README.rst', encoding="utf8").read()
else:
io_open = io.open('README.rst', encoding="utf8")
io_open = open('README.rst', encoding="utf8")
long_description = io_open.read()
Comment on lines 7 to 11

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since this pull request drops support for Python 2 and targets Python 3.9+, this entire if/else block for version compatibility is no longer necessary. The else branch is now dead code.

You can simplify this logic to directly read the file. Using a with statement is also recommended for handling files as it ensures they are closed properly even if errors occur.

with open('README.rst', encoding="utf8") as f:
    long_description = f.read()


setup(
Expand All @@ -18,7 +18,7 @@
description="JSON Python Logging",
long_description=long_description,
author="Bob T.",
keywords=["json", "elastic", "python", "python3", "python2", "logging", "logging-library", "json", "elasticsearch",
keywords=["json", "elastic", "python", "python3", "logging", "logging-library", "json", "elasticsearch",
"elk", "elk-stack", "logstash", "kibana"],
platforms='any',
url="https://github.com/thangbn/json-logging",
Expand All @@ -28,7 +28,6 @@
'License :: OSI Approved :: Apache Software License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
'Topic :: System :: Logging',
'Framework :: Flask',
Expand Down
5 changes: 2 additions & 3 deletions tests-performance/benmark_micro.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# coding=utf-8
import time
import timeit
from datetime import datetime
from datetime import datetime, timezone

utcnow = datetime.utcnow()
utcnow = datetime.now(timezone.utc)

numbers = 1000000
# timeit_timeit = timeit.timeit(lambda: '%04d-%02d-%02dT%02d:%02d:%02dZ' % (
Expand Down
11 changes: 9 additions & 2 deletions tests/helpers/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Constants shared by multiple tests"""

STANDARD_MSG_ATTRIBUTES = {
import sys

_msg_attrs = [
"written_at",
"written_ts",
"msg",
Expand All @@ -11,4 +13,9 @@
"module",
"line_no",
"correlation_id",
}
]

if sys.version_info.major == 3 and sys.version_info.minor >= 12:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The version check can be made more concise and idiomatic by comparing the sys.version_info tuple directly. This improves readability and is the recommended way to perform version checks.

Suggested change
if sys.version_info.major == 3 and sys.version_info.minor >= 12:
if sys.version_info >= (3, 12):

_msg_attrs.append("taskName")

STANDARD_MSG_ATTRIBUTES = set(_msg_attrs)
2 changes: 1 addition & 1 deletion tests/helpers/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self, level=logging.NOTSET) -> None:
"""Create a new log handler."""
super().__init__(level=level)
self.level = level
self.messages: List[str] = []
self.messages: list[str] = []

def emit(self, record: logging.LogRecord) -> None:
"""Keep the log records in a list in addition to the log text."""
Expand Down