Skip to content

Commit

Permalink
logger.py: create directory if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr1212 committed Jan 31, 2019
1 parent fa230fc commit e1aed95
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion webapp/graphite/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e1aed95

Please sign in to comment.