mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #47548 from Ch3LL/syslog
Catch Sysloghandler errors when log file does not exist
This commit is contained in:
commit
8a7913ccc3
2 changed files with 56 additions and 0 deletions
|
@ -103,6 +103,20 @@ class SysLogHandler(ExcInfoOnLogLevelFormatMixIn, logging.handlers.SysLogHandler
|
|||
'''
|
||||
Syslog handler which properly handles exc_info on a per handler basis
|
||||
'''
|
||||
def handleError(self, record):
|
||||
'''
|
||||
Override the default error handling mechanism for py3
|
||||
Deal with syslog os errors when the log file does not exist
|
||||
'''
|
||||
handled = False
|
||||
if sys.stderr and sys.version_info >= (3, 5, 4):
|
||||
t, v, tb = sys.exc_info()
|
||||
if t.__name__ in 'FileNotFoundError':
|
||||
sys.stderr.write('[WARNING ] The log_file does not exist. Logging not setup correctly or syslog service not started.\n')
|
||||
handled = True
|
||||
|
||||
if not handled:
|
||||
super(SysLogHandler, self).handleError(record)
|
||||
|
||||
|
||||
class RotatingFileHandler(ExcInfoOnLogLevelFormatMixIn, logging.handlers.RotatingFileHandler, NewStyleClassMixIn):
|
||||
|
|
|
@ -340,6 +340,48 @@ class CallTest(ShellCase, testprogram.TestProgramCase, ShellCaseCommonTestsMixin
|
|||
if os.path.isdir(config_dir):
|
||||
shutil.rmtree(config_dir)
|
||||
|
||||
def test_syslog_file_not_found(self):
|
||||
'''
|
||||
test when log_file is set to a syslog file that does not exist
|
||||
'''
|
||||
old_cwd = os.getcwd()
|
||||
config_dir = os.path.join(TMP, 'log_file_incorrect')
|
||||
if not os.path.isdir(config_dir):
|
||||
os.makedirs(config_dir)
|
||||
|
||||
os.chdir(config_dir)
|
||||
|
||||
with salt.utils.fopen(self.get_config_file_path('minion'), 'r') as fh_:
|
||||
minion_config = yaml.load(fh_.read())
|
||||
minion_config['log_file'] = 'file:///dev/doesnotexist'
|
||||
with salt.utils.fopen(os.path.join(config_dir, 'minion'), 'w') as fh_:
|
||||
fh_.write(
|
||||
yaml.dump(minion_config, default_flow_style=False)
|
||||
)
|
||||
ret = self.run_script(
|
||||
'salt-call',
|
||||
'--config-dir {0} cmd.run "echo foo"'.format(
|
||||
config_dir
|
||||
),
|
||||
timeout=60,
|
||||
catch_stderr=True,
|
||||
with_retcode=True
|
||||
)
|
||||
try:
|
||||
if sys.version_info >= (3, 5, 4):
|
||||
self.assertIn('local:', ret[0])
|
||||
self.assertIn('[WARNING ] The log_file does not exist. Logging not setup correctly or syslog service not started.', ret[1])
|
||||
self.assertEqual(ret[2], 0)
|
||||
else:
|
||||
self.assertIn(
|
||||
'Failed to setup the Syslog logging handler', '\n'.join(ret[1])
|
||||
)
|
||||
self.assertEqual(ret[2], 2)
|
||||
finally:
|
||||
self.chdir(old_cwd)
|
||||
if os.path.isdir(config_dir):
|
||||
shutil.rmtree(config_dir)
|
||||
|
||||
def test_issue_15074_output_file_append(self):
|
||||
output_file_append = os.path.join(TMP, 'issue-15074')
|
||||
try:
|
||||
|
|
Loading…
Add table
Reference in a new issue