summaryrefslogtreecommitdiffstats
path: root/Lib/logging
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/logging')
-rw-r--r--Lib/logging/__init__.py16
-rw-r--r--Lib/logging/config.py21
-rw-r--r--Lib/logging/handlers.py8
3 files changed, 35 insertions, 10 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 073213f..3d7ff82 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -432,6 +432,12 @@ class Formatter(object):
s = s[:-1]
return s
+ def usesTime(self):
+ """
+ Check if the format uses the creation time of the record.
+ """
+ return self._fmt.find("%(asctime)") >= 0
+
def format(self, record):
"""
Format the specified record as text.
@@ -440,13 +446,13 @@ class Formatter(object):
string formatting operation which yields the returned string.
Before formatting the dictionary, a couple of preparatory steps
are carried out. The message attribute of the record is computed
- using LogRecord.getMessage(). If the formatting string contains
- "%(asctime)", formatTime() is called to format the event time.
- If there is exception information, it is formatted using
- formatException() and appended to the message.
+ using LogRecord.getMessage(). If the formatting string uses the
+ time (as determined by a call to usesTime(), formatTime() is
+ called to format the event time. If there is exception information,
+ it is formatted using formatException() and appended to the message.
"""
record.message = record.getMessage()
- if self._fmt.find("%(asctime)") >= 0:
+ if self.usesTime():
record.asctime = self.formatTime(record, self.datefmt)
s = self._fmt % record.__dict__
if record.exc_info:
diff --git a/Lib/logging/config.py b/Lib/logging/config.py
index 767c630..beb51e5 100644
--- a/Lib/logging/config.py
+++ b/Lib/logging/config.py
@@ -261,7 +261,6 @@ def _install_loggers(cp, handlers, disable_existing_loggers):
logger.disabled = 1
-
IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I)
@@ -448,7 +447,7 @@ class BaseConfigurator(object):
isinstance(value, tuple):
value = ConvertingTuple(value)
value.configurator = self
- elif isinstance(value, str):
+ elif isinstance(value, str): # str for py3k
m = self.CONVERT_PATTERN.match(value)
if m:
d = m.groupdict()
@@ -474,6 +473,12 @@ class BaseConfigurator(object):
setattr(result, name, value)
return result
+ def as_tuple(self, value):
+ """Utility function which converts lists to tuples."""
+ if isinstance(value, list):
+ value = tuple(value)
+ return value
+
class DictConfigurator(BaseConfigurator):
"""
Configure logging using a dictionary-like object to describe the
@@ -484,6 +489,10 @@ class DictConfigurator(BaseConfigurator):
"""Do the configuration."""
config = self.config
+ if 'version' not in config:
+ raise ValueError("dictionary doesn't specify a version")
+ if config['version'] != 1:
+ raise ValueError("Unsupported version: %s" % config['version'])
incremental = config.pop('incremental', False)
EMPTY_DICT = {}
logging._acquireLock()
@@ -684,6 +693,12 @@ class DictConfigurator(BaseConfigurator):
except Exception as e:
raise ValueError('Unable to set target handler '
'%r: %s' % (config['target'], e))
+ elif issubclass(klass, logging.handlers.SMTPHandler) and\
+ 'mailhost' in config:
+ config['mailhost'] = self.as_tuple(config['mailhost'])
+ elif issubclass(klass, logging.handlers.SysLogHandler) and\
+ 'address' in config:
+ config['address'] = self.as_tuple(config['address'])
factory = klass
kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
try:
@@ -788,7 +803,7 @@ def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
chunk = self.connection.recv(slen)
while len(chunk) < slen:
chunk = chunk + conn.recv(slen - len(chunk))
- chunk = chunk.decode('utf-8')
+ chunk = chunk.decode("utf-8")
try:
import json
d =json.loads(chunk)
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
index 1e174e6..692d104 100644
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -25,7 +25,7 @@ To use, simply 'import logging.handlers' and log away!
"""
import logging, socket, os, pickle, struct, time, re
-from stat import ST_DEV, ST_INO
+from stat import ST_DEV, ST_INO, ST_MTIME
try:
import codecs
@@ -203,7 +203,11 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
self.extMatch = re.compile(self.extMatch, re.ASCII)
self.interval = self.interval * interval # multiply by units requested
- self.rolloverAt = self.computeRollover(int(time.time()))
+ if os.path.exists(filename):
+ t = os.stat(filename)[ST_MTIME]
+ else:
+ t = int(time.time())
+ self.rolloverAt = self.computeRollover(t)
def computeRollover(self, currentTime):
"""