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:
2021-11-13 18:27:46 +01:00
parent 5e0c5a359b
commit 86debb9abb
2 changed files with 24 additions and 2 deletions

View File

@@ -7,12 +7,13 @@ import uuid
from datetime import datetime, date
import urllib.request
from utils import getConfig
from utils import getConfig, timezoneOffset
config = getConfig()
LOGGER_NAME = config['logger']['name']
esHost = config['elastic']['host']
esPort = config['elastic']['port']
systemTimezone = timezoneOffset()
class ESHandler(logging.Handler):
def __init__(self, *args, **kwargs):
@@ -25,7 +26,8 @@ class ESHandler(logging.Handler):
def emit(self, 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'))

View File

@@ -1,5 +1,6 @@
#!/bin/usr/python3
import os
import subprocess
import yaml
def loadYaml(filePath):
@@ -20,3 +21,22 @@ def getConfig():
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