Skip to content

Commit

Permalink
Manage logs when log file is not accessible
Browse files Browse the repository at this point in the history
Fixes grnet#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.
  • Loading branch information
saxtouri committed Jun 17, 2014
1 parent c7198ab commit 4b8763c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
17 changes: 12 additions & 5 deletions kamaki/cli/logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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
Expand Down Expand Up @@ -33,6 +33,7 @@

from os import chmod
from os.path import expanduser
from sys import stderr
import logging


Expand Down Expand Up @@ -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):
Expand All @@ -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)

Expand Down
7 changes: 5 additions & 2 deletions kamaki/cli/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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
Expand Down Expand Up @@ -250,9 +250,12 @@ def test_add_file_logger(self, GL, GLF):
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:
Expand Down

0 comments on commit 4b8763c

Please sign in to comment.