From e1aed953c5001c8a148a65d3bd8dba99f6e5c742 Mon Sep 17 00:00:00 2001 From: Piotr Date: Thu, 31 Jan 2019 15:39:48 +0100 Subject: [PATCH] logger.py: create directory if not exists --- webapp/graphite/logger.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/webapp/graphite/logger.py b/webapp/graphite/logger.py index 9cbc46992d..fda244f065 100644 --- a/webapp/graphite/logger.py +++ b/webapp/graphite/logger.py @@ -12,7 +12,10 @@ See the License for the specific language governing permissions and limitations under the License.""" -import os, logging +import errno +import logging +import os + from logging.handlers import TimedRotatingFileHandler as Rotater try: from logging import NullHandler @@ -30,6 +33,18 @@ def emit(self, record): from django.conf import settings +# this can't be put in util.py because of circular depency between log.py +# and util.py +def _mkdir_p(path): + try: + os.makedirs(path) + except OSError as exc: # Directory already exists + if exc.errno == errno.EEXIST and os.path.isdir(path): + pass + else: + raise + + class GraphiteLogger: def __init__(self): self.infoLogger = self._config_logger( @@ -73,6 +88,7 @@ def _config_logger(log_file_name, name, activate, formatter = logging.Formatter( fmt='%(asctime)s.%(msecs)03d :: %(message)s', datefmt='%Y-%m-%d,%H:%M:%S') + _mkdir_p(settings.LOG_DIR) log_file = os.path.join(settings.LOG_DIR, log_file_name) if settings.LOG_ROTATION: # if we want to rotate logs handler = Rotater(log_file, when=when, backupCount=backupCount)