Commit 69a9ba4d authored by shadMod's avatar shadMod 💬

added GroupWritableTimedRotatingFileHandler()

replaced FileHandler with GroupWritableTimedRotatingFileHandler (as requested)
parent 13a78762
Pipeline #364 passed with stage
in 0 seconds
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import os import os
import flask import flask
import logging.handlers
from . import actions from . import actions
from . import auth from . import auth
...@@ -45,7 +45,7 @@ def create_app(data_path, debug=False): ...@@ -45,7 +45,7 @@ def create_app(data_path, debug=False):
# Prepare the data directory # Prepare the data directory
init_data_directory(data_path) init_data_directory(data_path)
set_log_config(data_path) set_log_config(data_path, debug)
# Load the secret key # Load the secret key
with open(os.path.join(data_path, "secret_key")) as fn: with open(os.path.join(data_path, "secret_key")) as fn:
...@@ -118,32 +118,52 @@ def init_data_downloads(data_path, debug=False): ...@@ -118,32 +118,52 @@ def init_data_downloads(data_path, debug=False):
download_inst.store_cache_file() download_inst.store_cache_file()
def set_log_config(data_path): class GroupWritableTimedRotatingFileHandler(
dir_log = os.path.join(data_path, "logs") logging.handlers.TimedRotatingFileHandler
os.makedirs(dir_log, exist_ok=True) ):
"""
TimedRotatingFileHandler that creates group writable files
"""
def _open(self):
orig_umask = os.umask(0o002)
try:
return super()._open()
finally:
os.umask(orig_umask)
logging.handlers.GroupWritableTimedRotatingFileHandler = (
GroupWritableTimedRotatingFileHandler
)
def set_log_config(data_path: str, debug: bool = False):
DIR_LOG = os.path.join(data_path, "logs")
os.makedirs(DIR_LOG, exist_ok=True)
LOG_LEVEL = "DEBUG" if debug else "INFO"
dictConfig( dictConfig(
{ {
"version": 1, "version": 1,
"formatters": { "formatters": {
"default": { "default": {
"format": "[%(asctime)s %(levelname)s] %(module)s: %(message)s", "format": "[%(asctime)s %(module)s] %(levelname)s: %(message)s",
"datefmt": "%d/%m/%Y %H:%M:%S", "datefmt": "%d/%m/%Y %H:%M:%S"
} },
}, },
"handlers": { "handlers": {
"console": { "console": {
"class": "logging.StreamHandler", "level": "WARNING",
"stream": "ext://sys.stdout", "class": "logging.StreamHandler"
}, },
"file": { "file": {
"class": "logging.FileHandler", "level": "INFO",
"filename": os.path.join(dir_log, "access"), "class": "logging.handlers.GroupWritableTimedRotatingFileHandler",
"formatter": "default" "filename": os.path.join(DIR_LOG, "log"),
}, "when": "midnight",
"error_file": { "backupCount": 7,
"class": "logging.FileHandler",
"filename": os.path.join(dir_log, "error.log"),
"formatter": "default" "formatter": "default"
}, },
}, },
...@@ -152,10 +172,9 @@ def set_log_config(data_path): ...@@ -152,10 +172,9 @@ def set_log_config(data_path):
"handlers": ["console", "file"] "handlers": ["console", "file"]
}, },
"loggers": { "loggers": {
"errors": { "uitwww": {
"level": "ERROR", "level": LOG_LEVEL,
"handlers": ["error_file"], "handlers": ["file"]
"propagate": False,
} }
}, },
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment