Feat: Try read system set timezone and use this for es timestamp value
Replace hardcoded timezone offset. Noticed after we entered winter timezone.
This commit is contained in:
		| @@ -7,12 +7,13 @@ import uuid | |||||||
| from datetime import datetime, date | from datetime import datetime, date | ||||||
| import urllib.request | import urllib.request | ||||||
|  |  | ||||||
| from utils import getConfig | from utils import getConfig, timezoneOffset | ||||||
|  |  | ||||||
| config = getConfig() | config = getConfig() | ||||||
| LOGGER_NAME = config['logger']['name'] | LOGGER_NAME = config['logger']['name'] | ||||||
| esHost = config['elastic']['host'] | esHost = config['elastic']['host'] | ||||||
| esPort = config['elastic']['port'] | esPort = config['elastic']['port'] | ||||||
|  | systemTimezone = timezoneOffset() | ||||||
|  |  | ||||||
| class ESHandler(logging.Handler): | class ESHandler(logging.Handler): | ||||||
|   def __init__(self, *args, **kwargs): |   def __init__(self, *args, **kwargs): | ||||||
| @@ -25,7 +26,8 @@ class ESHandler(logging.Handler): | |||||||
|  |  | ||||||
|   def emit(self, record): |   def emit(self, record): | ||||||
|     self.format(record) |     self.format(record) | ||||||
|     timestamp = datetime.fromtimestamp(record.created).strftime('%Y-%m-%dT%H:%M:%S.%f+02:00') |     datetimeTemplate = '%Y-%m-%dT%H:%M:%S.%f{}'.format(systemTimezone) | ||||||
|  |     timestamp = datetime.fromtimestamp(record.created).strftime(datetimeTemplate) | ||||||
|  |  | ||||||
|     indexURL = 'http://{}:{}/{}-{}/_doc'.format(self.host, self.port, LOGGER_NAME, self.date.strftime('%Y.%m.%d')) |     indexURL = 'http://{}:{}/{}-{}/_doc'.format(self.host, self.port, LOGGER_NAME, self.date.strftime('%Y.%m.%d')) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| #!/bin/usr/python3 | #!/bin/usr/python3 | ||||||
| import os | import os | ||||||
|  | import subprocess | ||||||
| import yaml | import yaml | ||||||
|  |  | ||||||
| def loadYaml(filePath): | def loadYaml(filePath): | ||||||
| @@ -20,3 +21,22 @@ def getConfig(): | |||||||
|  |  | ||||||
|   return loadYaml(path) |   return loadYaml(path) | ||||||
|  |  | ||||||
|  | def timezoneOffset(): | ||||||
|  |   localTimezoneCommand = 'date +%z' | ||||||
|  |   process = subprocess.Popen(localTimezoneCommand.split(), stdout=subprocess.PIPE) | ||||||
|  |   output, error = process.communicate() | ||||||
|  |  | ||||||
|  |   fallbackTimezone = '+0100' | ||||||
|  |   if error: | ||||||
|  |     print('Error when trying to fetch timezone: {}. Returning fallbacktimezone: {}.'.format(error, fallbackTimezone)) | ||||||
|  |     return fallbackTimezone | ||||||
|  |  | ||||||
|  |   try: | ||||||
|  |     output = output.decode("utf-8") | ||||||
|  |     if '\n' in output: | ||||||
|  |       output = output.replace('\n', '') | ||||||
|  |     return output or fallbackTimezone | ||||||
|  |   except Error as error: | ||||||
|  |     print('Error occured while decoding output from system timezone: {}'.format(error)) | ||||||
|  |     return fallbackTimezone | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user