Coverage for jutil/logs.py: 33%

# Coverage for jutil/logs.py : 33%

## 15 statements 7 run 8 missing 0 excluded 0 partial

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 import json

2 import logging

3 from typing import Optional, Dict, Any

4 from ipware.ip import get_real_ip # type: ignore

5 from rest_framework.request import Request

6

7

8 logger = logging.getLogger(__name__)

9

10

11 def log_event(name: str, request: Optional[Request] = None, data: Optional[Dict[str, Any]] = None, ip: str = ''):

12 """

13 Logs consistent event for easy parsing/analysis.

14 Format: EVENT_<UPPERCASE_NAME>: <JSON object>

15 :param name: Name of the event. Will be logged as EVENT_XXX with XXX in capitals.

16 :param request: Django REST framework Request (optional)

17 :param data: Even data (optional)

18 :param ip: Even IP (optional)

19 """

20 log_data: Dict[str, Any] = {}

21 if not ip and request:

22 ip = get_real_ip(request)

23 if ip:

24 log_data['ip'] = ip

25 if data:

26 log_data['data'] = data

27 logger.info('EVENT_%s: %s', name.upper(), json.dumps(log_data))