Commits

Chris Mutel committed 1acc804

Move random_string to utils

  • Participants
  • Parent commits 263b8ae

Comments (0)

Files changed (2)

File brightway2/logs.py

-# -*- coding: utf-8 -*
+# -*- coding: utf-8 -*-
 from . import config
+from logging.handlers import RotatingFileHandler
+from utils import random_string
+import codecs
 import datetime
 import logging
-from logging.handlers import RotatingFileHandler
 import os
 
 
     handler.setFormatter(formatter)
     logger.addHandler(handler)
     return logger
+
+
+def get_io_logger(name):
+    """Build a logger that records only relevent data for display later as HTML."""
+    dirname = config.request_dir("logs")
+    assert dirname, "No logs directory found"
+
+    filepath = os.path.join(dirname, "%s.%s.log" % (name, random_string(6)))
+    handler = logging.StreamHandler(codecs.open(filepath, "w", "utf-8"))
+    logger = logging.getLogger(name)
+    logger.setLevel(logging.INFO)
+    handler.setFormatter(logging.Formatter(u"%(message)s"))
+    logger.addHandler(handler)
+    return logger, filepath

File brightway2/utils.py

+# -*- coding: utf-8 -*-
+import random
 import re
+import string
 
 # Maximum value for unsigned integer stored in 4 bytes
 MAX_INT_32 = 4294967295
     convert = lambda text: int(text) if text.isdigit() else text
     alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
     return sorted(l, key=alphanum_key)
+
+def random_string(length):
+    return ''.join(random.choice(string.letters + string.digits
+        ) for i in xrange(length))