Skip to content

Commit

Permalink
[rqd] Refactor rqd logging (#1504)
Browse files Browse the repository at this point in the history
Logging was not following the configured constants. Now there's also a
way to deactivate file logging by not setting a level on the config file
(rqd.conf)
  • Loading branch information
DiegoTavares authored Sep 6, 2024
1 parent 7dbefee commit 61a976f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
47 changes: 24 additions & 23 deletions rqd/rqd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,30 +149,30 @@ def daemonize(log_path=None, chdir_to_root=True):
pass

def setupLogging():
"""Sets up the logging for RQD. Logs to /var/log/messages"""

consolehandler = logging.StreamHandler()
consolehandler.setLevel(rqd.rqconstants.CONSOLE_LOG_LEVEL)
consolehandler.setFormatter(logging.Formatter(rqd.rqconstants.LOG_FORMAT))
logging.getLogger('').addHandler(consolehandler)

if platform.system() in ('Linux', 'Darwin'):
if platform.system() == 'Linux':
syslogAddress = '/dev/log'
else:
syslogAddress = '/var/run/syslog'
if os.path.exists(syslogAddress):
logfile = logging.handlers.SysLogHandler(address=syslogAddress)
"""Sets up the logging for RQD.
Logs to /var/log/messages"""
logger = logging.getLogger()
logger.setLevel(rqd.rqconstants.CONSOLE_LOG_LEVEL)
for handler in logger.handlers:
handler.setFormatter(logging.Formatter(rqd.rqconstants.LOG_FORMAT))

if rqd.rqconstants.FILE_LOG_LEVEL is not None:
if platform.system() in ('Linux', 'Darwin'):
if platform.system() == 'Linux':
syslogAddress = '/dev/log'
else:
syslogAddress = '/var/run/syslog'
if os.path.exists(syslogAddress):
logfile = logging.handlers.SysLogHandler(address=syslogAddress)
else:
logfile = logging.handlers.SysLogHandler()
elif platform.system() == 'Windows':
logfile = logging.FileHandler(os.path.expandvars('%TEMP%/openrqd.log'))
else:
logfile = logging.handlers.SysLogHandler()
elif platform.system() == 'Windows':
logfile = logging.FileHandler(os.path.expandvars('%TEMP%/openrqd.log'))
else:
logfile = logging.handlers.SysLogHandler()
logfile.setLevel(rqd.rqconstants.FILE_LOG_LEVEL)
logfile.setFormatter(logging.Formatter(rqd.rqconstants.LOG_FORMAT))
logging.getLogger('').addHandler(logfile)
logging.getLogger('').setLevel(logging.DEBUG)
logfile.setLevel(rqd.rqconstants.FILE_LOG_LEVEL)
logfile.setFormatter(logging.Formatter(rqd.rqconstants.LOG_FORMAT))
logger.addHandler(logfile)


def setup_sentry():
Expand Down Expand Up @@ -210,6 +210,7 @@ def usage():
def main():
"""Entrypoint for RQD."""
setupLogging()
logger = logging.getLogger()

if platform.system() == 'Linux' and os.getuid() != 0 and \
rqd.rqconstants.RQD_BECOME_JOB_USER:
Expand All @@ -234,7 +235,7 @@ def main():

rqd.rqutil.permissionsLow()

logging.warning('RQD Starting Up')
logger.warning('RQD Starting Up')

setup_sentry()

Expand Down
7 changes: 4 additions & 3 deletions rqd/rqd/rqconstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@
ALLOW_GPU = False
LOAD_MODIFIER = 0 # amount to add/subtract from load

LOG_FORMAT = '%(asctime)s %(levelname)-9s openrqd-%(module)-10s %(message)s'
CONSOLE_LOG_LEVEL = logging.DEBUG
FILE_LOG_LEVEL = logging.WARNING # Equal to or greater than the consoleLevel
LOG_FORMAT = '%(levelname)-9s openrqd-%(module)-10s: %(message)s'
CONSOLE_LOG_LEVEL = logging.WARNING
# Equal to or greater than the consoleLevel. None deactives logging to file
FILE_LOG_LEVEL = None

if subprocess.getoutput('/bin/su --help').find('session-command') != -1:
SU_ARGUMENT = '--session-command'
Expand Down

0 comments on commit 61a976f

Please sign in to comment.