Skip to content

Commit 2cbeb3b

Browse files
Merge pull request #89 from MEHRSHAD-MIRSHEKARY/fix/email-timestamp
πŸ› Fix/email timestamp
2 parents a8093ac + 23a9260 commit 2cbeb3b

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

β€ŽSECURITY.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Supported Versions
44

5-
We actively support the following versions of `dj-logging` with security updates:
5+
We actively support the following versions of `django_logging` with security updates:
66

77
| Version | Supported |
88
| --------- | ------------------ |
@@ -12,7 +12,7 @@ We actively support the following versions of `dj-logging` with security updates
1212

1313
## Reporting a Vulnerability
1414

15-
We take security issues seriously. If you find a vulnerability in `dj-logging`, please report it confidentially. Here are the steps to report security vulnerabilities:
15+
We take security issues seriously. If you find a vulnerability in `django_logging`, please report it confidentially. Here are the steps to report security vulnerabilities:
1616

1717
1. **Email**: Please send an email to [aryan513966@gmail.com](mailto:aryan513966@gmail.com) with a detailed description of the vulnerability.
1818
2. **Details**: In your email, include the following details:
@@ -29,18 +29,18 @@ We will:
2929
## Handling Vulnerabilities
3030

3131
When a vulnerability is confirmed:
32-
- We will create a fix and apply it to all actively supported versions of `dj-logging`.
32+
- We will create a fix and apply it to all actively supported versions of `django_logging`.
3333
- A new release with the security fix will be published, and the vulnerability will be disclosed in the changelog or via a security advisory.
3434
- We may delay the disclosure of details about the vulnerability until a sufficient number of users have updated to the patched version.
3535

3636
## General Security Guidelines
3737

38-
- Keep your `dj-logging` package up to date with the latest versions to ensure you benefit from the latest security fixes.
38+
- Keep your `django_logging` package up to date with the latest versions to ensure you benefit from the latest security fixes.
3939
- Follow our changelog for announcements regarding security fixes.
4040
- Ensure that your logging configuration is secure and does not expose sensitive information in log files.
4141

4242
## Responsible Disclosure
4343

44-
We strongly encourage responsible disclosure and will work to fix issues in a timely manner. We appreciate any effort to help make `dj-logging` more secure for all users.
44+
We strongly encourage responsible disclosure and will work to fix issues in a timely manner. We appreciate any effort to help make `django_logging` more secure for all users.
4545

46-
Thank you for helping us improve the security of `dj-logging`!
46+
Thank you for helping us improve the security of `django_logging`!

β€Ždjango_logging/handlers/email_handler.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,48 @@
44
from django.conf import settings
55
from django.http import HttpRequest
66
from django.template import engines
7-
from django.utils.timezone import now
7+
from django.utils.timezone import localtime
88

99
from django_logging.middleware import RequestLogMiddleware
1010
from django_logging.utils.get_conf import use_email_notifier_template
1111
from django_logging.utils.log_email_notifier.notifier import send_email_async
1212

1313

1414
class EmailHandler(Handler):
15+
"""A custom logging handler that sends log records via email.
16+
17+
This handler formats log records, optionally renders them using an
18+
email template, and sends the resulting email to the administrator's
19+
email address defined in the Django settings.
20+
21+
Methods:
22+
-------
23+
emit(record: LogRecord) -> None:
24+
Processes a log record and sends it via email to the administrator.
25+
26+
render_template(log_entry: str, request: Optional[HttpRequest] = None, template_path: str = "email_notifier_template.html") -> str:
27+
Renders the email body using the provided log entry and optional request details.
28+
The rendered email includes the log message, the current date and time,
29+
the user's IP address, and browser information.
30+
31+
"""
32+
1533
def emit(self, record: LogRecord) -> None:
34+
"""Processes a log record and sends it via email.
35+
36+
This method retrieves the request from the log record (if available), formats
37+
the log message, optionally renders the email body using a template, and sends
38+
the email asynchronously to the administrator.
39+
40+
Args:
41+
----
42+
record (LogRecord): The log record to be processed and sent via email.
43+
44+
Raises:
45+
------
46+
Exception: If any error occurs while sending the email or formatting the log record.
47+
48+
"""
1649
try:
1750
request = getattr(record, "request", None)
1851
log_entry = self.format(record)
@@ -34,6 +67,25 @@ def render_template(
3467
request: Optional[HttpRequest] = None,
3568
template_path: str = "email_notifier_template.html",
3669
) -> str:
70+
"""Renders the email body using a Django template.
71+
72+
This method uses the provided log entry and request (if available)
73+
to generate an HTML email body. The email includes details such as the
74+
log message, current date and time, the IP address, and browser type
75+
of the user making the request.
76+
77+
Args:
78+
----
79+
log_entry (str): The formatted log message to be included in the email.
80+
request (Optional[HttpRequest]): The HTTP request associated with the log entry, if available.
81+
template_path (str): The path to the Django template to be used for rendering the email.
82+
Defaults to "email_notifier_template.html".
83+
84+
Returns:
85+
-------
86+
str: The rendered email body as a string.
87+
88+
"""
3789
django_engine = engines["django"]
3890
template = django_engine.get_template(template_path)
3991

@@ -46,7 +98,7 @@ def render_template(
4698
)
4799

48100
# Get current time
49-
current_time = now()
101+
current_time = localtime()
50102

51103
# Format date and time separately
52104
formatted_date = current_time.strftime("%d %B %Y").replace(

β€Ždjango_logging/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
request_middleware,
1919
reset_settings,
2020
)
21-
from django_logging.tests.settings_configuration import configure_django_settings
21+
from django_logging.tests.setup import configure_django_settings
2222

2323
configure_django_settings()

β€Ždjango_logging/tests/settings_configuration.py renamed to β€Ždjango_logging/tests/setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,9 @@ def configure_django_settings():
4747
EMAIL_HOST_PASSWORD="the_password",
4848
DEFAULT_FROM_EMAIL="example@test.com",
4949
ADMIN_EMAIL="admin@test.com",
50+
LANGUAGE_CODE="en-us",
51+
TIME_ZONE="UTC",
52+
USE_I18N=True,
53+
USE_TZ=True,
5054
)
5155
django.setup()

0 commit comments

Comments
Β (0)