Simplify logging configuration

This commit is contained in:
Patrick Jentsch 2024-08-01 16:29:06 +02:00
parent 94548ac30c
commit 81c6f32a35
4 changed files with 43 additions and 50 deletions

View File

@ -21,7 +21,6 @@ def deploy():
print('Make default directories') print('Make default directories')
base_dir = current_app.config['NOPAQUE_DATA_DIR'] base_dir = current_app.config['NOPAQUE_DATA_DIR']
default_dirs: List[Path] = [ default_dirs: List[Path] = [
Path('/var/log/nopaque'),
base_dir / 'tmp', base_dir / 'tmp',
base_dir / 'users' base_dir / 'users'
] ]

View File

@ -20,6 +20,7 @@ class Config:
APIFAIRY_UI_PATH = '/api' APIFAIRY_UI_PATH = '/api'
''' # Flask # ''' ''' # Flask # '''
APPLICATION_ROOT = os.environ.get('APPLICATION_ROOT', '/')
PREFERRED_URL_SCHEME = os.environ.get('PREFERRED_URL_SCHEME', 'http') PREFERRED_URL_SCHEME = os.environ.get('PREFERRED_URL_SCHEME', 'http')
SECRET_KEY = os.environ.get('SECRET_KEY', 'hard to guess string') SECRET_KEY = os.environ.get('SECRET_KEY', 'hard to guess string')
SERVER_NAME = os.environ.get('SERVER_NAME', 'localhost:5000') SERVER_NAME = os.environ.get('SERVER_NAME', 'localhost:5000')
@ -85,17 +86,15 @@ class Config:
'[%(asctime)s] %(levelname)s in ' '[%(asctime)s] %(levelname)s in '
'%(pathname)s (function: %(funcName)s, line: %(lineno)d): %(message)s' '%(pathname)s (function: %(funcName)s, line: %(lineno)d): %(message)s'
) )
NOPAQUE_LOG_LEVEL = os.environ.get('NOPAQUE_LOG_LEVEL', 'INFO')
NOPAQUE_LOG_FILE_ENABLED = \ NOPAQUE_LOG_FILE_ENABLED = \
os.environ.get('NOPAQUE_LOG_FILE_ENABLED', 'true').lower() == 'true' os.environ.get('NOPAQUE_LOG_FILE_ENABLED', 'false').lower() == 'true'
NOPAQUE_LOG_FILE_DIR = \ NOPAQUE_LOG_FILE_DIR = Path(os.environ.get('NOPAQUE_LOG_FILE_DIR', '/var/log/nopaque'))
os.environ.get('NOPAQUE_LOGS_PATH', os.path.join(basedir, 'logs'))
NOPAQUE_LOG_FILE_LEVEL = \ NOPAQUE_LOG_FILE_LEVEL = \
os.environ.get('NOPAQUE_LOG_FILE_LEVEL', NOPAQUE_LOG_LEVEL) os.environ.get('NOPAQUE_LOG_FILE_LEVEL', None)
NOPAQUE_LOG_STDERR_ENABLED = \ NOPAQUE_LOG_STDERR_ENABLED = \
os.environ.get('NOPAQUE_LOG_STDERR_ENABLED', 'false').lower() == 'true' os.environ.get('NOPAQUE_LOG_STDERR_ENABLED', 'true').lower() == 'true'
NOPAQUE_LOG_STDERR_LEVEL = \ NOPAQUE_LOG_STDERR_LEVEL = \
os.environ.get('NOPAQUE_LOG_STDERR_LEVEL', NOPAQUE_LOG_LEVEL) os.environ.get('NOPAQUE_LOG_STDERR_LEVEL', None)
NOPAQUE_PROXY_FIX_ENABLED = \ NOPAQUE_PROXY_FIX_ENABLED = \
os.environ.get('NOPAQUE_PROXY_FIX_ENABLED', 'false').lower() == 'true' os.environ.get('NOPAQUE_PROXY_FIX_ENABLED', 'false').lower() == 'true'
@ -119,42 +118,35 @@ class Config:
@staticmethod @staticmethod
def init_app(app: Flask): def init_app(app: Flask):
# Set up logging according to the corresponding (NOPAQUE_LOG_*)
# configurations
app.logger.setLevel(app.config['NOPAQUE_LOG_LEVEL'])
# Remove existing handlers
for handler in app.logger.handlers: for handler in app.logger.handlers:
app.logger.removeHandler(handler) app.logger.removeHandler(handler)
# Setup handlers
formatter = logging.Formatter( log_formatter = logging.Formatter(
fmt=app.config['NOPAQUE_LOG_FORMAT'], fmt=app.config['NOPAQUE_LOG_FORMAT'],
datefmt=app.config['NOPAQUE_LOG_DATE_FORMAT'] datefmt=app.config['NOPAQUE_LOG_DATE_FORMAT']
) )
if app.config['NOPAQUE_IS_PRIMARY_INSTANCE']:
app.config['JOBS'].append(
{
"id": "daemon",
"func": "app.daemon:daemon",
"args": (app,),
"trigger": "interval",
"seconds": 3,
}
)
if app.config['NOPAQUE_LOG_STDERR_ENABLED']: if app.config['NOPAQUE_LOG_STDERR_ENABLED']:
log_stderr_level: str | None = app.config['NOPAQUE_LOG_STDERR_LEVEL']
stream_handler = logging.StreamHandler() stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter) stream_handler.setFormatter(log_formatter)
stream_handler.setLevel(app.config['NOPAQUE_LOG_STDERR_LEVEL']) if log_stderr_level is not None:
stream_handler.setLevel(log_stderr_level)
app.logger.addHandler(stream_handler) app.logger.addHandler(stream_handler)
if app.config['NOPAQUE_LOG_FILE_ENABLED']: if app.config['NOPAQUE_LOG_FILE_ENABLED']:
if not os.path.exists(app.config['NOPAQUE_LOG_FILE_DIR']): log_file_dir: Path = app.config['NOPAQUE_LOG_FILE_DIR']
os.mkdir(app.config['NOPAQUE_LOG_FILE_DIR']) log_file_level: str | None = app.config['NOPAQUE_LOG_FILE_LEVEL']
if not log_file_dir.exists():
log_file_dir.mkdir()
rotating_file_handler = RotatingFileHandler( rotating_file_handler = RotatingFileHandler(
os.path.join(app.config['NOPAQUE_LOG_FILE_DIR'], 'nopaque.log'), # noqa log_file_dir / 'nopaque.log',
maxBytes=10_240, maxBytes=10_240,
backupCount=10 backupCount=10
) )
rotating_file_handler.setFormatter(formatter) rotating_file_handler.setFormatter(log_formatter)
rotating_file_handler.setLevel(app.config['NOPAQUE_LOG_FILE_LEVEL']) # noqa if log_file_level is not None:
rotating_file_handler.setLevel(log_file_level)
app.logger.addHandler(rotating_file_handler) app.logger.addHandler(rotating_file_handler)
if app.config['NOPAQUE_PROXY_FIX_ENABLED']: if app.config['NOPAQUE_PROXY_FIX_ENABLED']:
@ -168,3 +160,14 @@ class Config:
x_prefix=app.config['NOPAQUE_PROXY_FIX_X_PREFIX'], x_prefix=app.config['NOPAQUE_PROXY_FIX_X_PREFIX'],
x_proto=app.config['NOPAQUE_PROXY_FIX_X_PROTO'] x_proto=app.config['NOPAQUE_PROXY_FIX_X_PROTO']
) )
if app.config['NOPAQUE_IS_PRIMARY_INSTANCE']:
app.config['JOBS'].append(
{
"id": "daemon",
"func": "app.daemon:daemon",
"args": (app,),
"trigger": "interval",
"seconds": 3,
}
)

View File

@ -50,7 +50,6 @@ services:
restart: "unless-stopped" restart: "unless-stopped"
volumes: volumes:
- "/var/run/docker.sock:/var/run/docker.sock" - "/var/run/docker.sock:/var/run/docker.sock"
- "./volumes/nopaque/logs:/var/log/nopaque"
# DANGER: Don't change the following mount within a Docker Compose # DANGER: Don't change the following mount within a Docker Compose
# config file, use the `.env` file instead. # config file, use the `.env` file instead.
- "${HOST_NOPAQUE_DATA_PATH}:${HOST_NOPAQUE_DATA_PATH}" - "${HOST_NOPAQUE_DATA_PATH}:${HOST_NOPAQUE_DATA_PATH}"

View File

@ -113,10 +113,10 @@ NOPAQUE_ADMIN=
# DEFAULT: /mnt/nopaque # DEFAULT: /mnt/nopaque
# NOTES: # NOTES:
# - This must be a network share and it must be available on all # - This must be a network share and it must be available on all
# Docker Swarm nodes, mounted to the same path with the same # Docker Swarm nodes, mounted to the same path.
# user and group ownership
# - When running with Docker Compose, this gets overwritten in the # - When running with Docker Compose, this gets overwritten in the
# `docker-compose.yml` file # `docker-compose.yml` file with the value of the `HOST_NOPAQUE_DATA_PATH`
# environment variable from the `.env` file.
# NOPAQUE_DATA_PATH= # NOPAQUE_DATA_PATH=
# CHOOSE ONE: False, True # CHOOSE ONE: False, True
@ -141,30 +141,22 @@ NOPAQUE_DOCKER_REGISTRY_PASSWORD=
# DEFAULT: [%(asctime)s] %(levelname)s in %(pathname)s (function: %(funcName)s, line: %(lineno)d): %(message)s # DEFAULT: [%(asctime)s] %(levelname)s in %(pathname)s (function: %(funcName)s, line: %(lineno)d): %(message)s
# NOPAQUE_LOG_FORMAT= # NOPAQUE_LOG_FORMAT=
# DEFAULT: INFO
# CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG
# NOPAQUE_LOG_LEVEL=
# CHOOSE ONE: False, True # CHOOSE ONE: False, True
# DEFAULT: True # DEFAULT: False
# NOPAQUE_LOG_FILE_ENABLED= # NOPAQUE_LOG_FILE_ENABLED=
# DEFAULT: <nopaque-basedir>/logs # DEFAULT: /var/log/nopaque
# NOTES: # NOPAQUE_LOG_FILE_DIR=
# - Use `.` as <nopaque-basedir>
# - When running with Docker Compose, this gets overwritten in the
# `docker-compose.yml` file
# NOPAQUE_LOGS_PATH=
# DEFAULT: NOPAQUE_LOG_LEVEL # DEFAULT: DEBUG if FLASK_DEBUG == True else WARNING
# CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG # CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG
# NOPAQUE_LOG_FILE_LEVEL= # NOPAQUE_LOG_FILE_LEVEL=
# CHOOSE ONE: False, True # CHOOSE ONE: False, True
# DEFAULT: False # DEFAULT: True
# NOPAQUE_LOG_STDERR_ENABLED= # NOPAQUE_LOG_STDERR_ENABLED=
# DEFAULT: NOPAQUE_LOG_LEVEL # DEFAULT: DEBUG if FLASK_DEBUG == True else WARNING
# CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG # CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG
# NOPAQUE_LOG_STDERR_LEVEL= # NOPAQUE_LOG_STDERR_LEVEL=