diff --git a/.env.dist b/.env.dist index 9f3d2c580..2bceb0f10 100644 --- a/.env.dist +++ b/.env.dist @@ -6,7 +6,7 @@ RALPH_APP_DIR=/app/.ralph # Graylog storage backend -# RALPH_GRAYLOG_HOST=localhost +# RALPH_GRAYLOG_HOST=graylog # RALPH_GRAYLOG_PORT=12201 # LDP storage backend diff --git a/src/ralph/backends/storage/graylog.py b/src/ralph/backends/storage/graylog.py index c88498fb0..00eea9808 100644 --- a/src/ralph/backends/storage/graylog.py +++ b/src/ralph/backends/storage/graylog.py @@ -2,47 +2,58 @@ import logging import sys -from marshmallow import fields + from logging_gelf.formatters import GELFFormatter from logging_gelf.handlers import GELFTCPSocketHandler from logging_gelf.schemas import GelfSchema -from pathlib import Path +from marshmallow import fields + +from ...defaults import RALPH_GRAYLOG_HOST, RALPH_GRAYLOG_PORT from ..mixins import HistoryMixin from .base import BaseStorage -from ...defaults import RALPH_GRAYLOG_HOST, RALPH_GRAYLOG_PORT logger = logging.getLogger(__name__) class EventSchema(GelfSchema): + """Custom-made schema for GELF formatter""" + archive = fields.String() class GraylogStorage(HistoryMixin, BaseStorage): """Graylog storage backend""" + # pylint: disable=too-many-arguments + name = "graylog" def __init__( self, host=RALPH_GRAYLOG_HOST, port=RALPH_GRAYLOG_PORT, + client_options: dict = None, ): + + self.client_options = client_options + self.host = host self.port = port - logger = logging.getLogger("gelf") - logger.setLevel(logging.INFO) + self.gelf_logger = logging.getLogger("gelf") + self.gelf_logger.setLevel(logging.INFO) def write(self, name, chunk_size=None, overwrite=False): """Write inputs in graylog backend (one JSON event per line)""" + logger.debug("Creating archive: %s", name) + handler = GELFTCPSocketHandler(host=self.host, port=self.port) - input = sys.stdin.readlines() - for event in input: - handler.setFormatter(GELFFormatter(schema=EventSchema)) - logger.addHandler(handler) - logger.info(event, extra=dict(archive=name)) + handler.setFormatter(GELFFormatter(schema=EventSchema)) + self.gelf_logger.addHandler(handler) + + for event in sys.stdin.readlines(): + self.gelf_logger.info(event, extra=self.client_options) def list(self, details=False, new=False): """List files in the storage backend""" diff --git a/tests/backends/storage/test_graylog.py b/tests/backends/storage/test_graylog.py index 1f025ca20..e1d09dd59 100644 --- a/tests/backends/storage/test_graylog.py +++ b/tests/backends/storage/test_graylog.py @@ -8,12 +8,11 @@ def test_backends_storage_graylog_storage_instantiation(): """Tests the GraylogStorage backend instantiation.""" # pylint: disable=protected-access - assert GraylogStorage.name == "graylog" - storage = GraylogStorage( host=RALPH_GRAYLOG_HOST, port=RALPH_GRAYLOG_PORT, ) + assert storage.name == "graylog" assert storage.host == "graylog" assert storage.port == 12201