Browse Source

Merge pull request #39 from oseemann/timeout

Add timeout for requests.post() operation
opensearch
Ido Halevi 6 years ago
committed by GitHub
parent
commit
90d75b3041
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 5 deletions
  1. +3
    -1
      README.md
  2. +4
    -2
      logzio/handler.py
  3. +5
    -2
      logzio/sender.py

+ 3
- 1
README.md View File

@ -64,6 +64,7 @@ format={"additional_field": "value"}
- Logz.io Listener address (defaults to "https://listener.logz.io:8071") - Logz.io Listener address (defaults to "https://listener.logz.io:8071")
- Debug flag. Set to True, will print debug messages to stdout. (defaults to "False") - Debug flag. Set to True, will print debug messages to stdout. (defaults to "False")
- Backup logs flag. Set to False, will disable the local backup of logs in case of failure. (defaults to "True") - Backup logs flag. Set to False, will disable the local backup of logs in case of failure. (defaults to "True")
- Network timeout, in seconds, int or float, for sending the logs to logz.io. (defaults to 10)
Please note, that you have to configure those parameters by this exact order. Please note, that you have to configure those parameters by this exact order.
i.e. you cannot set Debug to true, without configuring all of the previous parameters as well. i.e. you cannot set Debug to true, without configuring all of the previous parameters as well.
@ -124,7 +125,8 @@ LOGGING = {
'logzio_type': "django", 'logzio_type': "django",
'logs_drain_timeout': 5, 'logs_drain_timeout': 5,
'url': 'https://listener.logz.io:8071', 'url': 'https://listener.logz.io:8071',
'debug': True
'debug': True,
'network_timeout': 10,
}, },
}, },
'loggers': { 'loggers': {


+ 4
- 2
logzio/handler.py View File

@ -17,7 +17,8 @@ class LogzioHandler(logging.Handler):
logs_drain_timeout=3, logs_drain_timeout=3,
url="https://listener.logz.io:8071", url="https://listener.logz.io:8071",
debug=False, debug=False,
backup_logs=True):
backup_logs=True,
network_timeout=10.0):
if not token: if not token:
raise LogzioException('Logz.io Token must be provided') raise LogzioException('Logz.io Token must be provided')
@ -29,7 +30,8 @@ class LogzioHandler(logging.Handler):
url=url, url=url,
logs_drain_timeout=logs_drain_timeout, logs_drain_timeout=logs_drain_timeout,
debug=debug, debug=debug,
backup_logs=backup_logs)
backup_logs=backup_logs,
network_timeout=network_timeout)
logging.Handler.__init__(self) logging.Handler.__init__(self)
def __del__(self): def __del__(self):


+ 5
- 2
logzio/sender.py View File

@ -33,12 +33,14 @@ class LogzioSender:
token, url='https://listener.logz.io:8071', token, url='https://listener.logz.io:8071',
logs_drain_timeout=5, logs_drain_timeout=5,
debug=False, debug=False,
backup_logs=True):
backup_logs=True,
network_timeout=10.0):
self.token = token self.token = token
self.url = '{}/?token={}'.format(url, token) self.url = '{}/?token={}'.format(url, token)
self.logs_drain_timeout = logs_drain_timeout self.logs_drain_timeout = logs_drain_timeout
self.logger = get_logger(debug) self.logger = get_logger(debug)
self.backup_logs = backup_logs self.backup_logs = backup_logs
self.network_timeout = network_timeout
# Function to see if the main thread is alive # Function to see if the main thread is alive
self.is_main_thread_active = lambda: any( self.is_main_thread_active = lambda: any(
@ -109,7 +111,8 @@ class LogzioSender:
should_retry = False should_retry = False
try: try:
response = requests.post( response = requests.post(
self.url, headers=headers, data='\n'.join(logs_list))
self.url, headers=headers, data='\n'.join(logs_list),
timeout=self.network_timeout)
if response.status_code != 200: if response.status_code != 200:
if response.status_code == 400: if response.status_code == 400:
self.logger.info( self.logger.info(


Loading…
Cancel
Save