Skip to content

Commit

Permalink
Allow to set daemon log level with the command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric Mohier authored and mohierf committed Apr 20, 2019
1 parent f01db44 commit 9f8c30d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 15 deletions.
31 changes: 25 additions & 6 deletions alignak/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,10 @@ def __init__(self, name, **kwargs):
self.log_cherrypy = None
self.log_filename = ''

# Log level...
if 'log_level' in kwargs and kwargs['log_level']:
self.log_level = PathProp().pythonize(kwargs['log_level'].strip())

# pid file is stored in the working directory
self.pid = os.getpid()
self.pid_filename = PathProp().pythonize("%s.pid" % self.name)
Expand Down Expand Up @@ -1751,12 +1755,21 @@ def get_header(self, configuration=False):
:return: A string list containing project name, daemon name, version, licence etc.
:rtype: list
"""
header = ["-----",
"Alignak %s - %s daemon" % (VERSION, self.name),
"Copyright (c) 2015-2018: Alignak Team",
"License: AGPL",
"-----",
"My pid: %s" % self.pid]
header = [u"-----",
u" █████╗ ██╗ ██╗ ██████╗ ███╗ ██╗ █████╗ ██╗ ██╗",
u" ██╔══██╗██║ ██║██╔════╝ ████╗ ██║██╔══██╗██║ ██╔╝",
u" ███████║██║ ██║██║ ███╗██╔██╗ ██║███████║█████╔╝ ",
u" ██╔══██║██║ ██║██║ ██║██║╚██╗██║██╔══██║██╔═██╗ ",
u" ██║ ██║███████╗██║╚██████╔╝██║ ╚████║██║ ██║██║ ██╗",
u" ╚═╝ ╚═╝╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝",
u"-----",
u"Alignak %s - %s daemon" % (VERSION, self.name),
u"Copyright (c) 2015-2019: Alignak Team",
u"License: AGPL",
u"-----",
u"Python: %s" % sys.version,
u"-----",
u"My pid: %s" % self.pid]

if configuration:
header = ["My configuration: "]
Expand Down Expand Up @@ -2185,6 +2198,12 @@ def setup_alignak_logger(self):
logger.info("-----")
logger.info("Daemon log level set to a minimum of INFO")
logger.info("-----")
elif self.log_level:
# Force the global logger at provided level
set_log_level(self.log_level)
logger.info("-----")
logger.info("Daemon log level set to %s", self.log_level)
logger.info("-----")
except Exception as exp: # pylint: disable=broad-except
print("***** %s - exception when setting-up the logger: %s" % (self.name, exp))
self.exit_on_exception(exp, message="Logger configuration error!")
Expand Down
3 changes: 2 additions & 1 deletion alignak/daemons/arbiterdaemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,8 @@ def daemons_start(self, run_daemons=True):
# Ignore myself ;)
continue

if satellite.alignak_launched and satellite.address not in ['127.0.0.1', 'localhost']:
if satellite.alignak_launched and \
satellite.address not in ['127.0.0.1', 'localhost']:
logger.error("Alignak is required to launch a daemon for %s %s "
"but the satelitte is defined on an external address: %s",
satellite.type, satellite.name, satellite.address)
Expand Down
2 changes: 1 addition & 1 deletion alignak/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,7 @@ def check_freshness(self):
h.passive_checks_enabled and not h.active_checks_enabled]
statsmgr.gauge('freshness.hosts-count', len(hosts))
items.extend(hosts)
logger.debuginfo("Freshness check is enabled for %d hosts", len(hosts))
logger.debug("Freshness check is enabled for %d hosts", len(hosts))

hosts = [h for h in self.hosts if h.check_freshness and h.freshness_expired]
logger.debug("Freshness still expired for %d hosts", len(hosts))
Expand Down
4 changes: 4 additions & 0 deletions alignak/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,10 @@ def parse_daemon_args(arbiter=False):
parser.add_argument('-l', '--log_file', dest='log_filename',
help='File used for the daemon log. Set as empty to disable log file.')

parser.add_argument('-Lm', '--log_level', dest='log_level',
help='Log level: DEBUG, INFO, WARNING, ERROR or CRITICAL. '
'Overrides the -v and -vv options.')

parser.add_argument('-i', '--pid_file', dest='pid_filename',
help='File used to store the daemon pid')

Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ docopt
# Use psutil for daemons memory monitoring (env ALIGNAK_DAEMONS_MONITORING)
# Use psutil for scheduler ALIGNAK_LOG_MONITORING
# Use psutil for launching daemons from the Arbiter
# Uncomment or `pip install psutil` to enable the concerned features
# Note that psutil requires to be compiled: sudo apt-get install gcc python3-dev
psutil>=5.4.3,<=5.4.5
24 changes: 17 additions & 7 deletions tests/test_daemon_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@


import os
import sys
import time
import socket
import tempfile
Expand Down Expand Up @@ -397,14 +398,23 @@ def test_daemon_header(self):
"""
daemon = self.get_daemon()
expected_result = [
"-----",
"Alignak %s - %s daemon" % (VERSION, daemon.name),
"Copyright (c) 2015-2018: Alignak Team",
"License: AGPL",
"-----",
"My pid: %s" % daemon.pid
u"-----",
u" █████╗ ██╗ ██╗ ██████╗ ███╗ ██╗ █████╗ ██╗ ██╗",
u" ██╔══██╗██║ ██║██╔════╝ ████╗ ██║██╔══██╗██║ ██╔╝",
u" ███████║██║ ██║██║ ███╗██╔██╗ ██║███████║█████╔╝ ",
u" ██╔══██║██║ ██║██║ ██║██║╚██╗██║██╔══██║██╔═██╗ ",
u" ██║ ██║███████╗██║╚██████╔╝██║ ╚████║██║ ██║██║ ██╗",
u" ╚═╝ ╚═╝╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝",
u"-----",
u"Alignak %s - %s daemon" % (VERSION, daemon.name),
u"Copyright (c) 2015-2019: Alignak Team",
u"License: AGPL",
u"-----",
u"Python: %s" % sys.version,
u"-----",
u"My pid: %s" % daemon.pid
]
assert daemon.get_header()[:7] == expected_result
assert daemon.get_header()[:14] == expected_result

def test_daemon_environment(self):
""" Test daemon environment variables
Expand Down

0 comments on commit 9f8c30d

Please sign in to comment.