Commit 8240a4c5 authored by Stavros Sachtouris's avatar Stavros Sachtouris

Manage logs when log file is not accessible

Fixes #25

If the default log file location is not accessile, inform user
about the error (e.g., no write permission, location does not
exist). Also, make non-debug log messages shorter and make sure
all instructions and warnings are printed to standard error.
parent f4c2917e
# Copyright 2013 GRNET S.A. All rights reserved.
# Copyright 2013-2014 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -33,6 +33,7 @@
from os import chmod
from os.path import expanduser
from sys import stderr
import logging
......@@ -69,10 +70,15 @@ def get_log_filename():
with open(logfile, 'a+') as f:
f.seek(0)
chmod(logfile, 0600)
except IOError:
except IOError as e:
stderr.write(
'WARNING: Failed to use file "%s" for logging\n' % logfile)
stderr.write('\t%s\n' % e)
stderr.flush()
continue
return logfile
print('Failed to open any logging locations, file-logging aborted')
stderr.write('WARNING: No valid log files, redirect logs to std err\n')
stderr.flush()
def set_log_filename(filename):
......@@ -94,9 +100,10 @@ def _add_logger(name, level=None, filename=None, fmt=None):
@if_logger_enabled
def add_file_logger(name, level=None, filename=None):
try:
fmt = '%(name)s(%(levelname)s) %(asctime)s\n %(message)s' if (
level == logging.DEBUG) else '%(name)s: %(message)s'
return _add_logger(
name, level, filename or get_log_filename(),
fmt='%(name)s(%(levelname)s) %(asctime)s\n\t%(message)s')
name, level, filename or get_log_filename(), fmt=fmt)
except Exception:
return get_logger(name)
......
# Copyright 2013 GRNET S.A. All rights reserved.
# Copyright 2013-2014 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -250,9 +250,12 @@ class LoggerMethods(TestCase):
for name, level, filename in product(
('my name'), ('my level', None), ('my filename', None)):
self.assertEqual(add_file_logger(name, level, filename), 'AL')
from logging import DEBUG as dbg
fmt = '%(name)s(%(levelname)s) %(asctime)s\n\t%(message)s' if (
level == dbg) else '%(name)s: %(message)s'
self.assertEqual(AL.mock_calls[-1], call(
name, level, filename or 'my log fname',
fmt='%(name)s(%(levelname)s) %(asctime)s\n\t%(message)s'))
fmt=fmt))
if filename:
self.assertEqual(GLFcount, GLF.call_count)
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment