Skip to content

Commit 63dabf8

Browse files
authored
Merge pull request #5 from zcsadmin/feat/add_logging_filter
feat: add endpoint filter
2 parents cee58f5 + c85995e commit 63dabf8

File tree

5 files changed

+66
-31
lines changed

5 files changed

+66
-31
lines changed

README.md

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,7 @@ from zcs.core.logger import ZcsLogging
6363

6464
## Local development
6565

66-
### Run library tests
67-
68-
If you want to run libraray tests against your local docker development environment, you can run:
69-
70-
```bash
71-
./run.sh -t
72-
```
73-
74-
### Run tests in a dist docker image
75-
76-
To be sure that anything is going the right way, you can run the library tests against a dist docker image. This will assure that anything is working properly in a fresh installed environment.
77-
78-
```bash
79-
./run.sh -i
80-
```
81-
82-
### Other commands and help
83-
84-
```bash
85-
./run.sh -h
86-
```
66+
See [Local Development](./docs/LOCAL_DEVELOPMENT.md).
8767

8868
## Support
8969

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .logger import ZcsLogging as ZcsLogging
1+
from .logger import ZcsLogging as ZcsLogging
2+
from .endpoint_filter import EndpointFilter as EndpointFilter
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Define a custom logging filter that can be used to ignore endpoint access logs.
3+
"""
4+
import logging
5+
from typing import List
6+
7+
8+
class EndpointFilter(logging.Filter):
9+
"""Custom filter class to ignore endpoint access logs"""
10+
11+
def __init__(self, endpoints: List[str] = ["/health", "/metrics", "/info"]):
12+
self.__endpoints = endpoints
13+
14+
def filter(self, record: logging.LogRecord) -> bool:
15+
"""
16+
Determine if a message should be ignored.
17+
18+
Returns:
19+
bool - False if the message should be ignored, True if not
20+
"""
21+
22+
for endpoint in self.__endpoints:
23+
if record.getMessage().find(f"GET {endpoint}") != -1:
24+
return False
25+
26+
return True

app/src/zcs/core/logger/logger.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import json
22
import logging
33

4-
from zcs.core.exception import ZcsException
4+
from zcs.core.exception import ZcsException
55

66
old_factory = logging.getLogRecordFactory()
77

8+
89
def record_factory(*args, **kwargs):
910
record = old_factory(*args, **kwargs)
10-
11+
1112
record.error_code = None
1213
if args[4] and isinstance(args[4], ZcsException):
1314
record.error_code = args[4].get_error_code()
@@ -20,11 +21,13 @@ def record_factory(*args, **kwargs):
2021
record.original_exception = arg
2122
if isinstance(arg, ZcsException):
2223
record.error_code = arg.get_error_code()
23-
24+
2425
return record
2526

27+
2628
logging.setLogRecordFactory(record_factory)
2729

30+
2831
class CloudJsonFormatter(logging.Formatter):
2932
def format(self, record):
3033

@@ -50,13 +53,14 @@ def format(self, record):
5053
}
5154
return json.dumps(log_record)
5255

56+
5357
class ConsoleCustomFormatter(logging.Formatter):
5458

55-
def __init__(self, verbose = False):
59+
def __init__(self, verbose=False):
5660
super().__init__()
57-
61+
5862
set_grey = "\x1b[38;20m"
59-
set_green = "\x1b[32;20m"
63+
# set_green = "\x1b[32;20m"
6064
set_yellow = "\x1b[33;20m"
6165
set_red = "\x1b[31;20m"
6266
set_bold_red = "\x1b[31;1m"
@@ -79,6 +83,7 @@ def format(self, record):
7983
formatter = self.FORMATTERS.get(record.levelno)
8084
return formatter.format(record)
8185

86+
8287
class ZcsLogging:
8388

8489
def __init__(self, enable_cloud_logging: bool = True, log_level: str = logging.INFO):
@@ -101,8 +106,8 @@ def __init__(self, enable_cloud_logging: bool = True, log_level: str = logging.I
101106
p_logger.setLevel(self.__log_level)
102107
if len(p_logger.handlers) == 0:
103108
continue
104-
p_logger.handlers = [ custom_handler ]
105-
109+
p_logger.handlers = [custom_handler]
110+
106111
zcs_handler = logging.StreamHandler()
107112
if enable_cloud_logging:
108113
zcs_handler.setFormatter(CloudJsonFormatter())
@@ -112,7 +117,7 @@ def __init__(self, enable_cloud_logging: bool = True, log_level: str = logging.I
112117
# Set log level and handler for the root logger
113118
logging.basicConfig(
114119
level=self.__log_level,
115-
handlers=[ zcs_handler ]
120+
handlers=[zcs_handler]
116121
)
117122

118123
def get_logger(self):

docs/LOCAL_DEVELOPMENT.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Local development
2+
3+
## Run library tests
4+
5+
If you want to run library tests against your local docker development environment, you can run:
6+
7+
```bash
8+
./run.sh -t
9+
```
10+
11+
## Run tests in a dist docker image
12+
13+
To be sure that anything is going the right way, you can run the library tests against a dist docker image. This will assure that anything is working properly in a fresh installed environment.
14+
15+
```bash
16+
./run.sh -i
17+
```
18+
19+
## Other commands and help
20+
21+
```bash
22+
./run.sh -h
23+
```

0 commit comments

Comments
 (0)