Commit dec7683f authored by Igor Ponomarev's avatar Igor Ponomarev Committed by Paweł Wieczorek
Browse files

Make LAVADaemonCommand use LOGGING configuration by default

New configuration priotity:

1. Passed arguments such as `--log-file`
2. LOGGING variable

The systemd files had to be changed to allow LOGGING configuration
be the default. The `--log-file` and `--level` arguments can be
passed with no arguments which is equal to not passing them at all.
parent 2d09b73e
......@@ -4,7 +4,7 @@ After=network.target remote-fs.target
[Service]
Type=simple
Environment=LOGLEVEL=DEBUG LOGFILE=/var/log/lava-server/lava-publisher.log HOST="*" PORT=8001
Environment=HOST="*" PORT=8001
EnvironmentFile=-/etc/default/lava-publisher
EnvironmentFile=-/etc/lava-server/lava-publisher
ExecStart=/usr/bin/lava-server manage lava-publisher --level $LOGLEVEL --log-file $LOGFILE --host $HOST --port $PORT
......
......@@ -4,7 +4,6 @@ After=network.target remote-fs.target
[Service]
Type=simple
Environment=LOGLEVEL=DEBUG LOGFILE=/var/log/lava-server/lava-scheduler.log
EnvironmentFile=-/etc/default/lava-scheduler
EnvironmentFile=-/etc/lava-server/lava-scheduler
ExecStart=/usr/bin/lava-server manage lava-scheduler --level $LOGLEVEL --log-file $LOGFILE $EVENT_URL $IPV6
......
......@@ -28,9 +28,14 @@ import os
import pwd
import signal
from typing import Optional
from django.core.management.base import BaseCommand
DEFAULT_LAVA_DAEMON_LOG_FORMAT = "%(asctime)-15s %(levelname)7s %(message)s"
class LAVADaemonCommand(BaseCommand):
def add_arguments(self, parser):
log = parser.add_argument_group("logging")
......@@ -38,12 +43,15 @@ class LAVADaemonCommand(BaseCommand):
"-l",
"--level",
choices=["ERROR", "WARN", "INFO", "DEBUG"],
default="DEBUG",
nargs="?",
help="Logging level (ERROR, WARN, INFO, DEBUG) " "Default: DEBUG",
)
log.add_argument(
"-o", "--log-file", default=self.default_logfile, help="Logging file path"
"-o",
"--log-file",
nargs="?",
help="Logging file path",
)
priv = parser.add_argument_group("privileges")
......@@ -88,27 +96,42 @@ class LAVADaemonCommand(BaseCommand):
return True
def setup_logging(self, logger_name, level, log_file, log_format):
del logging.root.handlers[:]
del logging.root.filters[:]
def setup_logging(
self,
logger_name: str,
level: Optional[str] = None,
log_file: Optional[str] = None,
log_format: Optional[str] = None,
) -> None:
# Create the logger
self.logger = logging.getLogger(logger_name)
if log_file == "-":
handler = logging.StreamHandler()
else:
handler = logging.handlers.WatchedFileHandler(log_file)
handler.setFormatter(logging.Formatter(log_format))
self.logger.addHandler(handler)
if log_file is not None:
# Remove existing handlers
self.logger.handlers.clear()
if log_file == "-":
handler = logging.StreamHandler()
else:
handler = logging.handlers.WatchedFileHandler(log_file)
self.logger.addHandler(handler)
if log_format is None:
log_format = DEFAULT_LAVA_DAEMON_LOG_FORMAT
handler.setFormatter(logging.Formatter(log_format))
# Set log level
if level == "ERROR":
self.logger.setLevel(logging.ERROR)
elif level == "WARN":
self.logger.setLevel(logging.WARN)
elif level == "INFO":
self.logger.setLevel(logging.INFO)
else:
self.logger.setLevel(logging.DEBUG)
if level is not None:
if level == "ERROR":
self.logger.setLevel(logging.ERROR)
elif level == "WARN":
self.logger.setLevel(logging.WARN)
elif level == "INFO":
self.logger.setLevel(logging.INFO)
else:
self.logger.setLevel(logging.DEBUG)
def setup_zmq_signal_handler(self):
# Mask signals and create a pipe that will receive a bit for each
......
......@@ -34,7 +34,6 @@ from lava_server.cmdutils import LAVADaemonCommand
TIMEOUT = 5
FORMAT = "%(asctime)-15s %(levelname)7s %(message)s"
async def zmq_proxy(app):
......@@ -164,9 +163,7 @@ class Command(LAVADaemonCommand):
parser.add_argument("--port", default=8001, type=int, help="port to bind to")
def handle(self, *args, **options):
self.setup_logging(
"lava-publisher", options["level"], options["log_file"], FORMAT
)
self.setup_logging("lava-publisher", options["level"], options["log_file"])
self.logger.info("[INIT] Starting lava-publisher")
self.logger.info("[INIT] Version %s", __version__)
......
......@@ -44,9 +44,6 @@ from lava_server.cmdutils import LAVADaemonCommand
INTERVAL = 20
PING_TIMEOUT = 3 * INTERVAL
# Log format
FORMAT = "%(asctime)-15s %(levelname)7s %(message)s"
class Command(LAVADaemonCommand):
logger = None
......@@ -87,7 +84,9 @@ class Command(LAVADaemonCommand):
def handle(self, *args, **options):
# Initialize logging.
self.setup_logging(
"lava-scheduler", options["level"], options["log_file"], FORMAT
"lava-scheduler",
options["level"],
options["log_file"],
)
self.logger.info("[INIT] Starting lava-scheduler")
......
......@@ -207,6 +207,8 @@ CUSTOM_DOCS = {}
# Logging
DJANGO_LOGFILE = "/var/log/lava-server/django.log"
LAVA_SCHEDULER_LOG_FILE = "/var/log/lava-server/lava-scheduler.log"
LAVA_PUBLISHER_LOG_FILE = "/var/log/lava-server/lava-publisher.log"
# Configuration directories
DISPATCHER_CONFIG_PATH = "/etc/lava-server/dispatcher.d"
......@@ -339,7 +341,11 @@ def update(values):
AUTH_GITLAB_SCOPE = values.get("AUTH_GITLAB_SCOPE")
AUTHENTICATION_BACKENDS = values.get("AUTHENTICATION_BACKENDS")
DISALLOWED_USER_AGENTS = values.get("DISALLOWED_USER_AGENTS")
DJANGO_LOGFILE = values.get("DJANGO_LOGFILE")
LAVA_SCHEDULER_LOG_FILE = values.get("LAVA_SCHEDULER_LOG_FILE")
LAVA_PUBLISHER_LOG_FILE = values.get("LAVA_PUBLISHER_LOG_FILE")
EVENT_NOTIFICATION = values.get("EVENT_NOTIFICATION")
INSTALLED_APPS = values.get("INSTALLED_APPS")
INTERNAL_IPS = values.get("INTERNAL_IPS")
......@@ -474,7 +480,13 @@ def update(values):
"require_debug_false": {"()": "django.utils.log.RequireDebugFalse"}
},
"formatters": {
"lava": {"format": "%(levelname)s %(asctime)s %(module)s %(message)s"}
"lava": {"format": "%(levelname)s %(asctime)s %(module)s %(message)s"},
"lava-scheduler": {
"format": "%(asctime)-15s %(levelname)7s %(message)s"
},
"lava-publisher": {
"format": "%(asctime)-15s %(levelname)7s %(message)s"
},
},
"handlers": {
"console": {
......@@ -487,6 +499,16 @@ def update(values):
"filename": DJANGO_LOGFILE,
"formatter": "lava",
},
"lava-scheduler-logfile": {
"class": "logging.handlers.WatchedFileHandler",
"filename": LAVA_SCHEDULER_LOG_FILE,
"formatter": "lava-scheduler",
},
"lava-publisher-logfile": {
"class": "logging.handlers.WatchedFileHandler",
"filename": LAVA_PUBLISHER_LOG_FILE,
"formatter": "lava-publisher",
},
},
"loggers": {
"django": {
......@@ -510,6 +532,16 @@ def update(values):
"level": "INFO",
"propagate": True,
},
"lava-scheduler": {
"handlers": ["lava-scheduler-logfile"],
"level": "DEBUG",
"propagate": False,
},
"lava-publisher": {
"handlers": ["lava-publisher-logfile"],
"level": "DEBUG",
"propagate": False,
},
},
}
......
......@@ -44,6 +44,8 @@ PROJECT_STATE_DIR.mkdir(parents=True, exist_ok=True)
# LAVA logs
DJANGO_LOGFILE = str(PROJECT_STATE_DIR / "django.log")
LAVA_SCHEDULER_LOG_FILE = str(PROJECT_STATE_DIR / "lava-scheduler.log")
LAVA_PUBLISHER_LOG_FILE = str(PROJECT_STATE_DIR / "lava-publisher.log")
# Test database
DATABASES = {
......
Supports Markdown
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