Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set permission for network logging files and dirs #1214

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/ocrd_network/logging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@ def configure_file_handler_with_formatter(logger: Logger, log_file: Path, mode:
file_handler = FileHandler(filename=log_file, mode=mode)
file_handler.setFormatter(Formatter(LOG_FORMAT))
logger.addHandler(file_handler)
try:
log_file.chmod(0o666)
except PermissionError:
# if the file exists the permissions are supposed to already fit
pass


def get_root_logging_dir(module_name: NetworkLoggingDirs) -> Path:
module_log_dir = Path(config.OCRD_NETWORK_LOGS_ROOT_DIR, module_name.value)
module_log_dir.mkdir(parents=True, exist_ok=True)
try:
module_log_dir.mkdir(parents=True, exist_ok=True)
module_log_dir.chmod(0o777)
except PermissionError:
# if the folder exists the permissions are supposed to already fit
pass

return module_log_dir


Expand Down
14 changes: 12 additions & 2 deletions src/ocrd_utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,23 @@ def _ocrd_download_timeout_parser(val):
description="The root directory where all mets server related socket files are created",
parser=lambda val: Path(val),
default=(True, Path(gettempdir(), "ocrd_network_sockets")))
config.OCRD_NETWORK_SOCKETS_ROOT_DIR.mkdir(parents=True, exist_ok=True)
config.OCRD_NETWORK_SOCKETS_ROOT_DIR.mkdir(mode=0o777, parents=True, exist_ok=True)
try:
config.OCRD_NETWORK_SOCKETS_ROOT_DIR.chmod(0o777)
except PermissionError:
# if the folder exists the permissions are supposed to already fit
pass

config.add(name="OCRD_NETWORK_LOGS_ROOT_DIR",
description="The root directory where all ocrd_network related file logs are stored",
parser=lambda val: Path(val),
default=(True, Path(gettempdir(), "ocrd_network_logs")))
config.OCRD_NETWORK_LOGS_ROOT_DIR.mkdir(parents=True, exist_ok=True)
config.OCRD_NETWORK_LOGS_ROOT_DIR.mkdir(mode=0o777, parents=True, exist_ok=True)
try:
config.OCRD_NETWORK_LOGS_ROOT_DIR.chmod(0o777)
except PermissionError:
# if the folder exists the permissions are supposed to already fit
pass

config.add("HOME",
description="Directory to look for `ocrd_logging.conf`, fallback for unset XDG variables.",
Expand Down
10 changes: 10 additions & 0 deletions src/ocrd_utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import logging.config
from pathlib import Path
import sys
from os import chmod

from .constants import LOG_FORMAT, LOG_TIMEFMT
from .config import config
Expand Down Expand Up @@ -179,6 +180,15 @@ def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_L
if not silent:
print(f"[LOGGING] Picked up logging config at {config_file}", file=sys.stderr)
logging.config.fileConfig(config_file)
# Set permission of processing-server logfile to 666 to prevent possible permission erros while logging
try:
network_logger = logging.getLogger("ocrd_network")
for handler in network_logger.handlers:
if isinstance(handler, logging.FileHandler):
chmod(handler.baseFilename, 0o666)
except PermissionError:
# if the file exists the permissions are supposed to already fit
pass
else:
if not silent:
print("[LOGGING] Initializing logging with built-in defaults", file=sys.stderr)
Expand Down
Loading