Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Adds logging to /var/log/syslog using rsyslog
Browse files Browse the repository at this point in the history
Also adds a new test to test configure_logging function call.
  • Loading branch information
kushaldas committed Jan 20, 2020
1 parent 96b93dc commit baae8c0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
15 changes: 14 additions & 1 deletion securedrop_proxy/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
import logging
import os
import sys
import platform

from logging.handlers import TimedRotatingFileHandler
from logging.handlers import TimedRotatingFileHandler, SysLogHandler

from securedrop_proxy import main
from securedrop_proxy import proxy
Expand Down Expand Up @@ -83,7 +84,19 @@ def configure_logging() -> None:
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)

# For rsyslog handler
if platform.system() != "Linux": # pragma: no cover
syslog_file = "/var/run/syslog"
else:
syslog_file = "/dev/log"

sysloghandler = SysLogHandler(address=syslog_file)
sysloghandler.setFormatter(formatter)

# set up primary log
log = logging.getLogger()
log.setLevel(LOGLEVEL)
log.addHandler(handler)

# add the secondard logger
log.addHandler(sysloghandler)
18 changes: 18 additions & 0 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import io
import json
import os
import platform
import tempfile
import unittest.mock
from unittest.mock import patch

import vcr
from securedrop_proxy import entrypoint
Expand Down Expand Up @@ -46,6 +48,22 @@ def test_missing_config(self):
body["error"], "Configuration file does not exist at {}".format(config_path)
)

@patch("securedrop_proxy.entrypoint.logging")
@patch("securedrop_proxy.entrypoint.SysLogHandler")
@patch("securedrop_proxy.entrypoint.TimedRotatingFileHandler")
def test_configure_logging(self, mock_log_conf, mock_log_conf_sys, mock_logging):
with sdhome() as homedir:
mock_log_file = os.path.join(homedir, 'logs', 'proxy.log')
entrypoint.configure_logging()
mock_log_conf.assert_called_once_with(mock_log_file)
# For rsyslog handler
if platform.system() != "Linux": # pragma: no cover
syslog_file = "/var/run/syslog"
else:
syslog_file = "/dev/log"
mock_log_conf_sys.assert_called_once_with(address=syslog_file)
mock_logging.getLogger.assert_called_once_with()

def test_unwritable_log_folder(self):
"""
Tests a permission problem in `configure_logging`.
Expand Down

0 comments on commit baae8c0

Please sign in to comment.