mirror of
				https://github.com/KevinMidboe/transatlanticTorrentExpress.git
				synced 2025-10-29 18:00:19 +00:00 
			
		
		
		
	More metadata logged to es, also es apikey setting
This commit is contained in:
		@@ -6,9 +6,11 @@ user=
 | 
				
			|||||||
remote=
 | 
					remote=
 | 
				
			||||||
local=
 | 
					local=
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[DELUGE]
 | 
					[LOGGER]
 | 
				
			||||||
script=
 | 
					CH_LEVEL=INFO
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[ELASTIC]
 | 
					[ELASTIC]
 | 
				
			||||||
host=
 | 
					host=
 | 
				
			||||||
port=
 | 
					port=
 | 
				
			||||||
 | 
					ssl=
 | 
				
			||||||
 | 
					api_key=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										39
									
								
								logger.py
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								logger.py
									
									
									
									
									
								
							@@ -3,6 +3,7 @@
 | 
				
			|||||||
import logging
 | 
					import logging
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import json
 | 
					import json
 | 
				
			||||||
 | 
					import math
 | 
				
			||||||
import uuid
 | 
					import uuid
 | 
				
			||||||
import datetime
 | 
					import datetime
 | 
				
			||||||
import urllib.request
 | 
					import urllib.request
 | 
				
			||||||
@@ -28,21 +29,33 @@ ch.setLevel(logging.ERROR)
 | 
				
			|||||||
class ESHandler(logging.Handler):
 | 
					class ESHandler(logging.Handler):
 | 
				
			||||||
  def __init__(self, *args, **kwargs):
 | 
					  def __init__(self, *args, **kwargs):
 | 
				
			||||||
    self.host = kwargs.get('host')
 | 
					    self.host = kwargs.get('host')
 | 
				
			||||||
    self.port = kwargs.get('port') or 9200
 | 
					    self.port = kwargs.get('port')
 | 
				
			||||||
 | 
					    self.ssl = kwargs.get('ssl') or False
 | 
				
			||||||
 | 
					    self.apiKey = kwargs.get('apiKey')
 | 
				
			||||||
    self.date = datetime.date.today()
 | 
					    self.date = datetime.date.today()
 | 
				
			||||||
    self.sessionID = uuid.uuid4()
 | 
					    self.sessionID = uuid.uuid4()
 | 
				
			||||||
 | 
					    self.pid = str(os.getpid())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    logging.StreamHandler.__init__(self)
 | 
					    logging.StreamHandler.__init__(self)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def emit(self, record):
 | 
					  def emit(self, record):
 | 
				
			||||||
    self.format(record)
 | 
					    self.format(record)
 | 
				
			||||||
 | 
					    indexURL = 'http://{}/transatlantic_torrent_express/_doc'.format(self.host, self.date.strftime('%Y.%m'))
 | 
				
			||||||
 | 
					    headers = { 'Content-Type': 'application/json', 'User-Agent': 'transatlanticTorrentExpress/v0.1'}
 | 
				
			||||||
 | 
					    if self.ssl:
 | 
				
			||||||
 | 
					      indexURL = indexURL.replace('http', 'https')
 | 
				
			||||||
 | 
					    if self.port:
 | 
				
			||||||
 | 
					      indexURL = indexURL.replace(self.host, '{}:{}'.format(self.host, self.port))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if self.apiKey:
 | 
				
			||||||
 | 
					      headers['Authorization'] = 'ApiKey {}'.format(self.apiKey)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    indexURL = 'http://{}:{}/transatlantic_torrent_express-{}/_doc'.format(self.host, self.port, self.date.strftime('%Y.%m.%d'))
 | 
					 | 
				
			||||||
    doc = {
 | 
					    doc = {
 | 
				
			||||||
      'severity': record.levelname,
 | 
					      'severity': record.levelname,
 | 
				
			||||||
      'message': record.message,
 | 
					      'message': record.message,
 | 
				
			||||||
      '@timestamp': int(record.created*1000),
 | 
					      '@timestamp': math.trunc(record.created*1000),
 | 
				
			||||||
      'sessionID': str(self.sessionID)
 | 
					      'sessionID': str(self.sessionID),
 | 
				
			||||||
 | 
					      'pid': self.pid
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if hasattr(record, 'es'):
 | 
					    if hasattr(record, 'es'):
 | 
				
			||||||
@@ -53,11 +66,15 @@ class ESHandler(logging.Handler):
 | 
				
			|||||||
      doc = {**record.es, **doc}
 | 
					      doc = {**record.es, **doc}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    payload = json.dumps(doc).encode('utf8')
 | 
					    payload = json.dumps(doc).encode('utf8')
 | 
				
			||||||
    req = urllib.request.Request(indexURL, data=payload,
 | 
					    req = urllib.request.Request(indexURL, data=payload, headers=headers)
 | 
				
			||||||
                                 headers={'content-type': 'application/json'})
 | 
					
 | 
				
			||||||
    response = urllib.request.urlopen(req)
 | 
					    try:
 | 
				
			||||||
    response = response.read().decode('utf8')
 | 
					        response = urllib.request.urlopen(req)
 | 
				
			||||||
    return response
 | 
					        response = response.read().decode('utf8')
 | 
				
			||||||
 | 
					        return response
 | 
				
			||||||
 | 
					    except urllib.error.HTTPError as e:
 | 
				
			||||||
 | 
					        print('Unable to reach elastic, error:', e)
 | 
				
			||||||
 | 
					        return asdf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ElasticFieldParameterAdapter(logging.LoggerAdapter):
 | 
					class ElasticFieldParameterAdapter(logging.LoggerAdapter):
 | 
				
			||||||
  def __init__(self, logger, extra={}):
 | 
					  def __init__(self, logger, extra={}):
 | 
				
			||||||
@@ -74,7 +91,9 @@ class ElasticFieldParameterAdapter(logging.LoggerAdapter):
 | 
				
			|||||||
config = getConfig()
 | 
					config = getConfig()
 | 
				
			||||||
esHost = config['ELASTIC']['host']
 | 
					esHost = config['ELASTIC']['host']
 | 
				
			||||||
esPort = config['ELASTIC']['port']
 | 
					esPort = config['ELASTIC']['port']
 | 
				
			||||||
eh = ESHandler(host=esHost, port=esPort)
 | 
					esSSL = config['ELASTIC']['ssl']
 | 
				
			||||||
 | 
					esApiKey = config['ELASTIC']['api_key']
 | 
				
			||||||
 | 
					eh = ESHandler(host=esHost, port=esPort, ssl=esSSL, apiKey=esApiKey)
 | 
				
			||||||
eh.setLevel(logging.DEBUG)
 | 
					eh.setLevel(logging.DEBUG)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
formatter = logging.Formatter('%(asctime)s %(levelname)8s | %(message)s')
 | 
					formatter = logging.Formatter('%(asctime)s %(levelname)8s | %(message)s')
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user