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

Develop to Main #64

Merged
merged 12 commits into from
Aug 31, 2024
Merged
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
49 changes: 20 additions & 29 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,30 @@ jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [3.8, 3.9, 3.10, 3.11, 3.12]
django-version: [4.2, 5.0, 5.1]

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- uses: actions/checkout@v4

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry tox
poetry install
pip install coverage codecov pytest
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Run tests with coverage
run: coverage run -m pytest
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install coverage codecov pytest poetry
pip install -r packages/requirements-dev.txt

- name: Generate coverage report
run: coverage xml
- name: Run tests with coverage
run: pytest --cov=django_logging --cov-report=xml

- name: Run Tox tests
run: tox
- name: Run Tox tests
run: tox

- name: Run pre-commit hooks
run: tox -e pre-commit
- name: Run pre-commit hooks
run: tox -e pre-commit

- name: Upload coverage to Codecov
run: codecov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Upload coverage to Codecov
run: codecov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,4 @@ uwsgi.ini
nginx.conf
test_app/
myenv/
myenv3_8/
myenv3_8/
52 changes: 38 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-toml
- id: check-yaml
files: \.yaml$
- id: trailing-whitespace
exclude: (migrations/|tests/|docs/).*
- id: end-of-file-fixer
Expand All @@ -15,6 +18,21 @@ repos:
- id: check-docstring-first
exclude: (migrations/|tests/|docs/).*

- repo: https://github.com/tox-dev/pyproject-fmt
rev: 2.2.1
hooks:
- id: pyproject-fmt

- repo: https://github.com/tox-dev/tox-ini-fmt
rev: 1.3.1
hooks:
- id: tox-ini-fmt

- repo: https://github.com/asottile/pyupgrade
rev: v3.17.0
hooks:
- id: pyupgrade

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
hooks:
Expand Down Expand Up @@ -49,6 +67,14 @@ repos:
args: ["--in-place", "--recursive", "--blank"]
exclude: (migrations/|tests/|docs/).*

- repo: https://github.com/adamchainz/blacken-docs
rev: 1.18.0
hooks:
- id: blacken-docs
additional_dependencies:
- black==24.4.2
files: '\.rst$'

- repo: https://github.com/rstcheck/rstcheck
rev: "v6.2.4"
hooks:
Expand All @@ -68,17 +94,15 @@ repos:
pass_filenames: false
always_run: true

- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
require_serial: true
args:
- "-rn"
- "-sn"
- "--rcfile=pyproject.toml"
files: ^django_logging/

ci:
skip: [pylint]
# - id: pylint
# name: pylint
# entry: pylint
# language: system
# types: [python]
# require_serial: true
# args:
# - "-rn"
# - "-sn"
# - "--rcfile=pyproject.toml"
# files: ^django_logging/
# exclude: (migrations/|tests/|docs/).*
File renamed without changes.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The [`django_logging`](https://github.com/ARYAN-NIKNEZHAD/django_logging) is a D

- Language: Python > 3.8
- Framework: Django > 4.2
-


## Documentation

Expand Down
1 change: 1 addition & 0 deletions django_logging/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .date_format_directives import VALID_DIRECTIVES
from .default_settings import DefaultConsoleSettings, DefaultLoggingSettings
from .log_format_options import FORMAT_OPTIONS
from .log_format_specifiers import LOG_FORMAT_SPECIFIERS
22 changes: 22 additions & 0 deletions django_logging/constants/date_format_directives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Set

VALID_DIRECTIVES: Set = {
"%Y",
"%m",
"%d",
"%H",
"%M",
"%S",
"%a",
"%A",
"%b",
"%B",
"%p",
"%I",
"%j",
"%U",
"%W",
"%c",
"%x",
"%X",
}
22 changes: 11 additions & 11 deletions django_logging/filters/log_level_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@


class LoggingLevelFilter(logging.Filter):
"""
Filters log records based on their logging level.
"""Filters log records based on their logging level.

This filter is used to prevent log records from being written to log
files intended for lower log levels. For example, if we have
separate log files for DEBUG, INFO, WARNING, and ERROR levels, this
filter ensures that a log record with level ERROR is only written to
the ERROR log file, and not to the DEBUG, INFO or WARNING log files.

This filter is used to prevent log records from being written to log files
intended for lower log levels. For example, if we have separate log
files for DEBUG, INFO, WARNING, and ERROR levels, this filter ensures that
a log record with level ERROR is only written to the ERROR log file, and not
to the DEBUG, INFO or WARNING log files.
"""

def __init__(self, logging_level: int):
"""
Initializes a LoggingLevelFilter instance.
"""Initializes a LoggingLevelFilter instance.

Args:
logging_level: The logging level to filter on (e.g. logging.DEBUG, logging.INFO, etc.).

Returns:
None

"""
super().__init__()
self.logging_level = logging_level

def filter(self, record: logging.LogRecord) -> bool:
"""
Filters a log record based on its level.
"""Filters a log record based on its level.

Args:
record: The log record to filter.

Returns:
True if the log record's level matches the specified logging level, False otherwise.

"""
return record.levelno == self.logging_level
26 changes: 14 additions & 12 deletions django_logging/management/commands/send_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,35 @@


class Command(BaseCommand):
"""
A Django management command that zips the log directory and sends it to
"""A Django management command that zips the log directory and sends it to
the specified email address.

This command is used to send the log files to a specified email address.
It zips the log directory, creates an email with the zipped file as an attachment,
and sends it to the specified email address.
This command is used to send the log files to a specified email
address. It zips the log directory, creates an email with the zipped
file as an attachment, and sends it to the specified email address.

"""

help = "Send log folder to the specified email address"

def add_arguments(self, parser: ArgumentParser) -> None:
"""
Add custom command arguments.
"""Add custom command arguments.

Parameters:
parser (ArgumentParser): The argument parser to add arguments to.

"""
parser.add_argument(
"email", type=str, help="The email address to send the logs to"
)

def handle(self, *args: Tuple, **kwargs: Dict) -> None:
"""
The main entry point for the command.
"""The main entry point for the command.

Parameters:
args (tuple): Positional arguments.
kwargs (dict): Keyword arguments.

"""
email = kwargs["email"]

Expand Down Expand Up @@ -98,10 +98,12 @@ def handle(self, *args: Tuple, **kwargs: Dict) -> None:
logger.info("Temporary zip file cleaned up successfully.")

def validate_email_settings(self) -> None:
"""
Check if all required email settings are present in the settings file.
"""Check if all required email settings are present in the settings
file.

Raises ImproperlyConfigured if any of the required email
settings are missing.

Raises ImproperlyConfigured if any of the required email settings are missing.
"""
errors = check_email_settings(require_admin_email=False)
if errors:
Expand Down
24 changes: 10 additions & 14 deletions django_logging/middleware/request_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@


class RequestLogMiddleware:
"""
Middleware to log information about each incoming request.
"""Middleware to log information about each incoming request.

This middleware logs the request path, the user making the request
(if authenticated), and the user's IP address.

This middleware logs the request path, the user making the request (if authenticated),
and the user's IP address.
"""

def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]) -> None:
"""
Initializes the RequestLogMiddleware instance.
"""Initializes the RequestLogMiddleware instance.

Args:
get_response: A callable that returns an HttpResponse object.

"""
self.get_response = get_response
user_model = get_user_model()
self.username_field = user_model.USERNAME_FIELD

def __call__(self, request: HttpRequest) -> HttpResponse:
"""
Processes an incoming request and logs relevant information.
"""Processes an incoming request and logs relevant information.

Args:
request: The incoming request object.

Returns:
The response object returned by the view function.

"""
# Before view (and later middleware) are called.
response = self.get_response(request)
Expand Down Expand Up @@ -67,9 +67,7 @@ def __call__(self, request: HttpRequest) -> HttpResponse:

@staticmethod
def get_ip_address(request: HttpRequest) -> str:
"""
Retrieves the client's IP address from the request object.
"""
"""Retrieves the client's IP address from the request object."""
ip_address = request.META.get("HTTP_X_FORWARDED_FOR")
if ip_address:
ip_address = ip_address.split(",")[0]
Expand All @@ -82,7 +80,5 @@ def get_ip_address(request: HttpRequest) -> str:

@staticmethod
def get_user_agent(request: HttpRequest) -> str:
"""
Retrieves the client's user agent from the request object.
"""
"""Retrieves the client's user agent from the request object."""
return request.META.get("HTTP_USER_AGENT", "Unknown User Agent")
Loading