Skip to content

Commit

Permalink
feat: add logging configuration to settings.py
Browse files Browse the repository at this point in the history
  • Loading branch information
MissterHao committed Feb 10, 2023
1 parent 63ad7e2 commit ec31cae
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,22 @@ DYSESSION = {
"TTL_ATTRIBUTE_NAME": "ttl",
"CACHE_PERIOD": 3600,
"DYNAMODB_REGION": "ap-northeast-1",
"LOGGING": {
"TYPE": "CONSOLE",
},
}
```


| Argument | Default | Description |
| ------------------ | -------------- | ------------------------------------------------------------ |
| DYNAMODB_TABLENAME | sessions | DynamoDB table name |
| PARTITION_KEY_NAME | PK | Partition key name |
| TTL_ATTRIBUTE_NAME | ttl | Time to live attribute name |
| CACHE_PERIOD | 3600 | Define how long should be the cache live in DynamoDB's table |
| DYNAMODB_REGION | ap-northeast-1 | The region of the DynamoDB table |
| Argument | Default | Description |
|----------------------|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| DYNAMODB_TABLENAME | sessions | DynamoDB table name |
| PARTITION_KEY_NAME | PK | Partition key name |
| TTL_ATTRIBUTE_NAME | ttl | Time to live attribute name |
| CACHE_PERIOD | 3600 | Define how long should be the cache live in DynamoDB's table |
| DYNAMODB_REGION | ap-northeast-1 | The region of the DynamoDB table |
| LOGGING | Dict | Configuration of Logging |
| LOGGING["TYPE"] | CONSOLE | Only accept two kinds of parameters: `CONSOLE`, `FILE`. If this set to `CONSOLE`, django-dysession will use `StreamHandler` to stream to the console. If this set to `FILE`, django-dysession will use `FileHandler` to stream to `LOGGING["FILE_PATH"]`. |
| LOGGING["FILE_PATH"] | session.log | The file path to save logs of session managements. |


## Logging
Expand Down
18 changes: 16 additions & 2 deletions dysession/logger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from functools import lru_cache
from typing import Literal

from dysession.settings import get_config

from .handler.colorful_console import ColorfulConsoleLoggerHandler


Expand All @@ -23,7 +25,7 @@ def is_tty() -> bool:

def get_logger(
logger_name: str = "dysession",
logger_type: Literal[LoggingType.CONSOLE, LoggingType.FILE] = LoggingType.CONSOLE,
logger_type: Literal[None, LoggingType.CONSOLE, LoggingType.FILE] = None,
level: int = logging.DEBUG,
) -> logging.Logger:
"""
Expand All @@ -40,6 +42,17 @@ def get_logger(
```
"""

if logger_type is None:
try:
logger_type = LoggingType[get_config()["LOGGING"]["TYPE"]]
if (
logger_type == LoggingType.PLAINTEXT_CONSOLE
or logger_type == LoggingType.COLOR_CONSOLE
):
raise KeyError
except KeyError:
raise KeyError("logger_type only accept 'CONSOLE' and 'FILE'")

logger = logging.getLogger(logger_name)
format = logging.Formatter(
"[%(asctime)-s] [%(levelname)-8s] %(name)s %(message)s ... ( %(filename)s:%(levelno)s )"
Expand All @@ -53,7 +66,8 @@ def get_logger(
else:
handler = logging.StreamHandler()
elif logger_type == LoggingType.FILE:
handler = logging.FileHandler("session.log", "a", encoding="utf-8")
filepath = get_config()["LOGGING"].get("FILE_PATH", "session.log")
handler = logging.FileHandler(filepath, "a", encoding="utf-8")

handler.setFormatter(format)
logger.addHandler(handler)
Expand Down
3 changes: 3 additions & 0 deletions dysession/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"TTL_ATTRIBUTE_NAME": "ttl",
"CACHE_PERIOD": 3600,
"DYNAMODB_REGION": "ap-northeast-1",
"LOGGING": {
"TYPE": "CONSOLE",
},
}


Expand Down
15 changes: 5 additions & 10 deletions tests/test_aws_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@
from moto import mock_dynamodb
from parameterized import parameterized

from dysession.aws.dynamodb import (
check_dynamodb_table_exists,
create_dynamodb_table,
delete_session_item,
destory_dynamodb_table,
get_item,
insert_session_item,
key_exists,
)
from dysession.aws.dynamodb import (check_dynamodb_table_exists,
create_dynamodb_table, delete_session_item,
destory_dynamodb_table, get_item,
insert_session_item, key_exists)
from dysession.aws.error import DynamodbItemNotFound, DynamodbTableNotFound
from dysession.backends.model import SessionDataModel
from dysession.settings import get_config


class AWSDynamoDBTestCase(TestCase):
def setUp(self):
logging.disable(logging.NOTSET)
logging.disable(logging.CRITICAL)

def tearDown(self):
logging.disable(logging.NOTSET)
Expand Down

0 comments on commit ec31cae

Please sign in to comment.