Browse Source

Add to log which item a message is coming from

tags/v0.2.0
JustAnotherArchivist 4 years ago
parent
commit
ab22966fef
2 changed files with 16 additions and 5 deletions
  1. +5
    -4
      qwarc/__init__.py
  2. +11
    -1
      qwarc/cli.py

+ 5
- 4
qwarc/__init__.py View File

@@ -27,6 +27,7 @@ class Item:
self.headers = headers self.headers = headers
self.warc = warc self.warc = warc
self.stats = {'tx': 0, 'rx': 0, 'requests': 0} self.stats = {'tx': 0, 'rx': 0, 'requests': 0}
self.logger = logging.LoggerAdapter(logging.getLogger(), {'itemType': self.itemType, 'itemValue': self.itemValue})


self.childItems = [] self.childItems = []


@@ -62,7 +63,7 @@ class Item:
try: try:
try: try:
with _aiohttp.Timeout(60): with _aiohttp.Timeout(60):
logging.info(f'Fetching {url}')
self.logger.info(f'Fetching {url}')
response = await self.session.request(method, url, data = data, headers = headers, allow_redirects = False) response = await self.session.request(method, url, data = data, headers = headers, allow_redirects = False)
try: try:
ret = await response.read() ret = await response.read()
@@ -73,12 +74,12 @@ class Item:
else: else:
tx = len(response.rawRequestData) tx = len(response.rawRequestData)
rx = len(response.rawResponseData) rx = len(response.rawResponseData)
logging.info(f'Fetched {url}: {response.status} (tx {tx}, rx {rx})')
self.logger.info(f'Fetched {url}: {response.status} (tx {tx}, rx {rx})')
self.stats['tx'] += tx self.stats['tx'] += tx
self.stats['rx'] += rx self.stats['rx'] += rx
self.stats['requests'] += 1 self.stats['requests'] += 1
except (asyncio.TimeoutError, _aiohttp.ClientError) as e: except (asyncio.TimeoutError, _aiohttp.ClientError) as e:
logging.error(f'Request for {url} failed: {e!r}')
self.logger.error(f'Request for {url} failed: {e!r}')
action, writeToWarc = await responseHandler(url, attempt, response, e) action, writeToWarc = await responseHandler(url, attempt, response, e)
exc = e # Pass the exception outward for the history exc = e # Pass the exception outward for the history
else: else:
@@ -96,7 +97,7 @@ class Item:
data = None data = None
attempt = 0 attempt = 0
elif action == ACTION_RETRIES_EXCEEDED: elif action == ACTION_RETRIES_EXCEEDED:
logging.error(f'Request for {url} failed {attempt} times')
self.logger.error(f'Request for {url} failed {attempt} times')
return response, tuple(history) return response, tuple(history)
elif action == ACTION_RETRY: elif action == ACTION_RETRY:
# Nothing to do, just go to the next cycle # Nothing to do, just go to the next cycle


+ 11
- 1
qwarc/cli.py View File

@@ -8,12 +8,22 @@ import sys
import time import time




class Formatter(logging.Formatter):
def format(self, record):
if not hasattr(record, 'itemString'):
if hasattr(record, 'itemType') and hasattr(record, 'itemValue'):
record.itemString = f'{record.itemType}:{record.itemValue}'
else:
record.itemString = 'None'
return super().format(record)


def setup_logging(logFilename): def setup_logging(logFilename):
rootLogger = logging.getLogger() rootLogger = logging.getLogger()
rootLogger.handlers = [] rootLogger.handlers = []
rootLogger.setLevel(logging.INFO) rootLogger.setLevel(logging.INFO)


formatter = logging.Formatter('%(asctime)s.%(msecs)03dZ %(levelname)s %(message)s', datefmt = '%Y-%m-%d %H:%M:%S')
formatter = Formatter('%(asctime)s.%(msecs)03dZ %(levelname)s %(itemString)s %(message)s', datefmt = '%Y-%m-%d %H:%M:%S')
formatter.converter = time.gmtime formatter.converter = time.gmtime


fileHandler = logging.FileHandler(logFilename) fileHandler = logging.FileHandler(logFilename)


Loading…
Cancel
Save