diff --git a/README.md b/README.md index 8e2e5a4..f5e8abf 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/dysession/logger/__init__.py b/dysession/logger/__init__.py index ccac192..334b77c 100644 --- a/dysession/logger/__init__.py +++ b/dysession/logger/__init__.py @@ -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 @@ -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: """ @@ -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 )" @@ -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) diff --git a/dysession/settings.py b/dysession/settings.py index 40a76a3..d255252 100644 --- a/dysession/settings.py +++ b/dysession/settings.py @@ -12,6 +12,9 @@ "TTL_ATTRIBUTE_NAME": "ttl", "CACHE_PERIOD": 3600, "DYNAMODB_REGION": "ap-northeast-1", + "LOGGING": { + "TYPE": "CONSOLE", + }, } diff --git a/tests/test_aws_dynamodb.py b/tests/test_aws_dynamodb.py index c2d4787..ade3c3d 100644 --- a/tests/test_aws_dynamodb.py +++ b/tests/test_aws_dynamodb.py @@ -5,15 +5,10 @@ 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 @@ -21,7 +16,7 @@ class AWSDynamoDBTestCase(TestCase): def setUp(self): - logging.disable(logging.NOTSET) + logging.disable(logging.CRITICAL) def tearDown(self): logging.disable(logging.NOTSET)