Add ENABLED_LOGS config option

This commit is contained in:
augustin64 2024-01-19 13:48:23 +01:00
parent 99c9781767
commit 2ff7a515d5
3 changed files with 27 additions and 0 deletions

View File

@ -24,3 +24,6 @@ MAX_AGE=31
# Keep in mind that this config option can only be loaded from default_config.py, # Keep in mind that this config option can only be loaded from default_config.py,
# as the custom config is stored in $INSTANCE_PATH/ # as the custom config is stored in $INSTANCE_PATH/
INSTANCE_PATH="instance" INSTANCE_PATH="instance"
# Events to log
ENABLED_LOGS=["NEW_GROUPE", "NEW_ALBUM", "NEW_PARTITION", "NEW_USER", "SERVER_RESTART", "FAILED_LOGIN"]

View File

@ -55,7 +55,17 @@ def load_config():
DATABASE=os.path.join(app.instance_path, f"{__name__}.sqlite"), DATABASE=os.path.join(app.instance_path, f"{__name__}.sqlite"),
) )
def setup_logging():
logging.log_file = os.path.join(app.instance_path, "logs.txt") 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(): def get_version():
@ -68,6 +78,7 @@ def get_version():
load_config() load_config()
setup_logging()
app.register_blueprint(auth.bp) app.register_blueprint(auth.bp)
app.register_blueprint(admin.bp) app.register_blueprint(admin.bp)

View File

@ -15,6 +15,19 @@ class LogEntry(Enum):
SERVER_RESTART = 6 SERVER_RESTART = 6
FAILED_LOGIN = 7 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: def add_entry(entry: str) -> None:
date = datetime.now().strftime("%y-%b-%Y %H:%M:%S") date = datetime.now().strftime("%y-%b-%Y %H:%M:%S")