diff --git a/default_config.py b/default_config.py index 5d4d125..253e482 100644 --- a/default_config.py +++ b/default_config.py @@ -24,3 +24,6 @@ MAX_AGE=31 # Keep in mind that this config option can only be loaded from default_config.py, # as the custom config is stored in $INSTANCE_PATH/ INSTANCE_PATH="instance" + +# Events to log +ENABLED_LOGS=["NEW_GROUPE", "NEW_ALBUM", "NEW_PARTITION", "NEW_USER", "SERVER_RESTART", "FAILED_LOGIN"] \ No newline at end of file diff --git a/partitioncloud/__init__.py b/partitioncloud/__init__.py index 7d77f2d..dacea5f 100644 --- a/partitioncloud/__init__.py +++ b/partitioncloud/__init__.py @@ -55,7 +55,17 @@ def load_config(): DATABASE=os.path.join(app.instance_path, f"{__name__}.sqlite"), ) + +def setup_logging(): logging.log_file = os.path.join(app.instance_path, "logs.txt") + enabled = [] + for event in app.config["ENABLED_LOGS"]: + try: + enabled.append(logging.LogEntry.from_string(event)) + except KeyError: + print(f"[ERROR] There is an error in your config: Unknown event {event}") + + logging.enabled = enabled def get_version(): @@ -68,6 +78,7 @@ def get_version(): load_config() +setup_logging() app.register_blueprint(auth.bp) app.register_blueprint(admin.bp) diff --git a/partitioncloud/modules/logging.py b/partitioncloud/modules/logging.py index 4cd0e0f..472022e 100644 --- a/partitioncloud/modules/logging.py +++ b/partitioncloud/modules/logging.py @@ -15,6 +15,19 @@ class LogEntry(Enum): SERVER_RESTART = 6 FAILED_LOGIN = 7 + def from_string(entry: str): + mapping = { + "LOGIN": LogEntry.LOGIN, + "NEW_GROUPE": LogEntry.NEW_GROUPE, + "NEW_ALBUM": LogEntry.NEW_ALBUM, + "NEW_PARTITION": LogEntry.NEW_PARTITION, + "NEW_USER": LogEntry.NEW_USER, + "SERVER_RESTART": LogEntry.SERVER_RESTART, + "FAILED_LOGIN": LogEntry.FAILED_LOGIN + } + # Will return KeyError if not available + return mapping[entry] + def add_entry(entry: str) -> None: date = datetime.now().strftime("%y-%b-%Y %H:%M:%S")