We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
A+ Security logs, CEF format
After security audit in spring 2021, some new security-related log events were added, using SecurityLog class. The log output format should be converted to CEF format that can be exported to Aalto ITS logging systems. Also, the current log events should be reviewed: do they contain sufficient information, and should some additional events be added. Note that security log should contain only relevant events, that can be justified from security point of view.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `lib/logging.py`
Content:
```
1 from django.http import UnreadablePostError
2 import logging
3 from django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed
4 from django.dispatch import receiver
5 from django.http.request import HttpRequest
6 
7 def skip_unreadable_post(record):
8     """Skips log records of unfinished post requests."""
9     return not record.exc_info or not issubclass(record.exc_info[0], UnreadablePostError)
10 
11 class SecurityLog:
12     """
13     Static singleton class used for A+ - wide security logging.
14     Django signals are used to track login/logout events.
15     """
16 
17     seclogger = logging.getLogger('aplus.security')
18 
19     @staticmethod
20     def logger() -> logging.Logger:
21         return SecurityLog.seclogger
22 
23     @staticmethod
24     def logevent(request: HttpRequest, type: str, message: str) -> None:
25         # Unit tests do not have user defined in request object.
26         if request and hasattr(request, 'user'):
27             user=request.user
28             ip=request.META.get('REMOTE_ADDR')
29         else:
30             user='?'
31             ip='?'
32         SecurityLog.logger().info("({}/{}): {}: {}".format(
33             user,
34             ip,
35             type,
36             message
37         ))
38 
39 
40 # This example was used as an inspiration for the following functions:
41 # https://stackoverflow.com/questions/37618473/how-can-i-log-both-successful-and-failed-login-and-logout-attempts-in-django
42 @receiver(user_logged_in)
43 def user_logged_in_callback(sender, request, user, **kwargs):
44     SecurityLog.logevent(request, "login", "")
45 
46 @receiver(user_logged_out)
47 def user_logged_out_callback(sender, request, user, **kwargs):
48     SecurityLog.logevent(request, "logout", "")
49 
50 # Unfortunately the request object is not passed with this signal,
51 # so we cannot get the IP.
52 @receiver(user_login_failed)
53 def user_login_failed_callback(sender, credentials, **kwargs):
54     SecurityLog.logevent(None, "login-failed","{credentials}".format(
55         credentials=credentials)
56     )
57
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/lib/logging.py b/lib/logging.py
--- a/lib/logging.py
+++ b/lib/logging.py
@@ -3,6 +3,12 @@
 from django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed
 from django.dispatch import receiver
 from django.http.request import HttpRequest
+from format_cef import format_cef
+from aplus import VERSION
+
+
+CEF_VENDOR = 'Aalto'
+CEF_PRODUCT = 'aplus'
 
 def skip_unreadable_post(record):
     """Skips log records of unfinished post requests."""
@@ -10,7 +16,8 @@
 
 class SecurityLog:
     """
-    Static singleton class used for A+ - wide security logging.
+    Static singleton class used for A+ - wide security logging,
+    to produce ArcSight Common Event Format (CEF) log.
     Django signals are used to track login/logout events.
     """
 
@@ -21,20 +28,32 @@
         return SecurityLog.seclogger
 
     @staticmethod
-    def logevent(request: HttpRequest, type: str, message: str) -> None:
-        # Unit tests do not have user defined in request object.
-        if request and hasattr(request, 'user'):
-            user=request.user
-            ip=request.META.get('REMOTE_ADDR')
-        else:
-            user='?'
-            ip='?'
-        SecurityLog.logger().info("({}/{}): {}: {}".format(
-            user,
-            ip,
-            type,
-            message
-        ))
+    def logevent(
+            request: HttpRequest,
+            type: str,
+            message: str,
+            severity: int = 5,
+            ) -> None:
+        extensions = {}
+        # Unit tests may not have user or source address defined.
+        if request:
+            if hasattr(request, 'user'):
+                extensions['sourceUserName'] = str(request.user)
+                extensions['sourceUserId'] = str(request.user.id)
+            if (addr := request.META.get('REMOTE_ADDR')):
+                extensions['sourceAddress'] = addr
+
+        SecurityLog.logger().info(
+            format_cef(
+                CEF_VENDOR,
+                CEF_PRODUCT,
+                VERSION,
+                type,
+                message,
+                severity,
+                extensions,
+            ).decode("utf-8")
+        )
 
 
 # This example was used as an inspiration for the following functions:
@@ -51,6 +70,8 @@
 # so we cannot get the IP.
 @receiver(user_login_failed)
 def user_login_failed_callback(sender, credentials, **kwargs):
-    SecurityLog.logevent(None, "login-failed","{credentials}".format(
-        credentials=credentials)
-    )
+    try:
+        SecurityLog.logevent(None, "login-failed", f"username: {credentials['username']}")
+    except KeyError:
+        # Unit tests do not have 'username' in credentials, let's not fail them for that
+        pass

{"golden_diff": "diff --git a/lib/logging.py b/lib/logging.py\n--- a/lib/logging.py\n+++ b/lib/logging.py\n@@ -3,6 +3,12 @@\n from django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed\n from django.dispatch import receiver\n from django.http.request import HttpRequest\n+from format_cef import format_cef\n+from aplus import VERSION\n+\n+\n+CEF_VENDOR = 'Aalto'\n+CEF_PRODUCT = 'aplus'\n \n def skip_unreadable_post(record):\n     \"\"\"Skips log records of unfinished post requests.\"\"\"\n@@ -10,7 +16,8 @@\n \n class SecurityLog:\n     \"\"\"\n-    Static singleton class used for A+ - wide security logging.\n+    Static singleton class used for A+ - wide security logging,\n+    to produce ArcSight Common Event Format (CEF) log.\n     Django signals are used to track login/logout events.\n     \"\"\"\n \n@@ -21,20 +28,32 @@\n         return SecurityLog.seclogger\n \n     @staticmethod\n-    def logevent(request: HttpRequest, type: str, message: str) -> None:\n-        # Unit tests do not have user defined in request object.\n-        if request and hasattr(request, 'user'):\n-            user=request.user\n-            ip=request.META.get('REMOTE_ADDR')\n-        else:\n-            user='?'\n-            ip='?'\n-        SecurityLog.logger().info(\"({}/{}): {}: {}\".format(\n-            user,\n-            ip,\n-            type,\n-            message\n-        ))\n+    def logevent(\n+            request: HttpRequest,\n+            type: str,\n+            message: str,\n+            severity: int = 5,\n+            ) -> None:\n+        extensions = {}\n+        # Unit tests may not have user or source address defined.\n+        if request:\n+            if hasattr(request, 'user'):\n+                extensions['sourceUserName'] = str(request.user)\n+                extensions['sourceUserId'] = str(request.user.id)\n+            if (addr := request.META.get('REMOTE_ADDR')):\n+                extensions['sourceAddress'] = addr\n+\n+        SecurityLog.logger().info(\n+            format_cef(\n+                CEF_VENDOR,\n+                CEF_PRODUCT,\n+                VERSION,\n+                type,\n+                message,\n+                severity,\n+                extensions,\n+            ).decode(\"utf-8\")\n+        )\n \n \n # This example was used as an inspiration for the following functions:\n@@ -51,6 +70,8 @@\n # so we cannot get the IP.\n @receiver(user_login_failed)\n def user_login_failed_callback(sender, credentials, **kwargs):\n-    SecurityLog.logevent(None, \"login-failed\",\"{credentials}\".format(\n-        credentials=credentials)\n-    )\n+    try:\n+        SecurityLog.logevent(None, \"login-failed\", f\"username: {credentials['username']}\")\n+    except KeyError:\n+        # Unit tests do not have 'username' in credentials, let's not fail them for that\n+        pass\n", "issue": "A+ Security logs, CEF format\nAfter security audit in spring 2021, some new security-related log events were added, using SecurityLog class. The log output format should be converted to CEF format that can be exported to Aalto ITS logging systems. Also, the current log events should be reviewed: do they contain sufficient information, and should some additional events be added. Note that security log should contain only relevant events, that can be justified from security point of view.\n", "before_files": [{"content": "from django.http import UnreadablePostError\nimport logging\nfrom django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed\nfrom django.dispatch import receiver\nfrom django.http.request import HttpRequest\n\ndef skip_unreadable_post(record):\n    \"\"\"Skips log records of unfinished post requests.\"\"\"\n    return not record.exc_info or not issubclass(record.exc_info[0], UnreadablePostError)\n\nclass SecurityLog:\n    \"\"\"\n    Static singleton class used for A+ - wide security logging.\n    Django signals are used to track login/logout events.\n    \"\"\"\n\n    seclogger = logging.getLogger('aplus.security')\n\n    @staticmethod\n    def logger() -> logging.Logger:\n        return SecurityLog.seclogger\n\n    @staticmethod\n    def logevent(request: HttpRequest, type: str, message: str) -> None:\n        # Unit tests do not have user defined in request object.\n        if request and hasattr(request, 'user'):\n            user=request.user\n            ip=request.META.get('REMOTE_ADDR')\n        else:\n            user='?'\n            ip='?'\n        SecurityLog.logger().info(\"({}/{}): {}: {}\".format(\n            user,\n            ip,\n            type,\n            message\n        ))\n\n\n# This example was used as an inspiration for the following functions:\n# https://stackoverflow.com/questions/37618473/how-can-i-log-both-successful-and-failed-login-and-logout-attempts-in-django\n@receiver(user_logged_in)\ndef user_logged_in_callback(sender, request, user, **kwargs):\n    SecurityLog.logevent(request, \"login\", \"\")\n\n@receiver(user_logged_out)\ndef user_logged_out_callback(sender, request, user, **kwargs):\n    SecurityLog.logevent(request, \"logout\", \"\")\n\n# Unfortunately the request object is not passed with this signal,\n# so we cannot get the IP.\n@receiver(user_login_failed)\ndef user_login_failed_callback(sender, credentials, **kwargs):\n    SecurityLog.logevent(None, \"login-failed\",\"{credentials}\".format(\n        credentials=credentials)\n    )\n", "path": "lib/logging.py"}], "after_files": [{"content": "from django.http import UnreadablePostError\nimport logging\nfrom django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed\nfrom django.dispatch import receiver\nfrom django.http.request import HttpRequest\nfrom format_cef import format_cef\nfrom aplus import VERSION\n\n\nCEF_VENDOR = 'Aalto'\nCEF_PRODUCT = 'aplus'\n\ndef skip_unreadable_post(record):\n    \"\"\"Skips log records of unfinished post requests.\"\"\"\n    return not record.exc_info or not issubclass(record.exc_info[0], UnreadablePostError)\n\nclass SecurityLog:\n    \"\"\"\n    Static singleton class used for A+ - wide security logging,\n    to produce ArcSight Common Event Format (CEF) log.\n    Django signals are used to track login/logout events.\n    \"\"\"\n\n    seclogger = logging.getLogger('aplus.security')\n\n    @staticmethod\n    def logger() -> logging.Logger:\n        return SecurityLog.seclogger\n\n    @staticmethod\n    def logevent(\n            request: HttpRequest,\n            type: str,\n            message: str,\n            severity: int = 5,\n            ) -> None:\n        extensions = {}\n        # Unit tests may not have user or source address defined.\n        if request:\n            if hasattr(request, 'user'):\n                extensions['sourceUserName'] = str(request.user)\n                extensions['sourceUserId'] = str(request.user.id)\n            if (addr := request.META.get('REMOTE_ADDR')):\n                extensions['sourceAddress'] = addr\n\n        SecurityLog.logger().info(\n            format_cef(\n                CEF_VENDOR,\n                CEF_PRODUCT,\n                VERSION,\n                type,\n                message,\n                severity,\n                extensions,\n            ).decode(\"utf-8\")\n        )\n\n\n# This example was used as an inspiration for the following functions:\n# https://stackoverflow.com/questions/37618473/how-can-i-log-both-successful-and-failed-login-and-logout-attempts-in-django\n@receiver(user_logged_in)\ndef user_logged_in_callback(sender, request, user, **kwargs):\n    SecurityLog.logevent(request, \"login\", \"\")\n\n@receiver(user_logged_out)\ndef user_logged_out_callback(sender, request, user, **kwargs):\n    SecurityLog.logevent(request, \"logout\", \"\")\n\n# Unfortunately the request object is not passed with this signal,\n# so we cannot get the IP.\n@receiver(user_login_failed)\ndef user_login_failed_callback(sender, credentials, **kwargs):\n    try:\n        SecurityLog.logevent(None, \"login-failed\", f\"username: {credentials['username']}\")\n    except KeyError:\n        # Unit tests do not have 'username' in credentials, let's not fail them for that\n        pass\n", "path": "lib/logging.py"}]}