修复上传日志服务器日志显示时间无变化;修改获取为CST时间

This commit is contained in:
sityliu 2024-06-10 22:01:23 +08:00
parent bec233e5b9
commit 8ac1487e37
3 changed files with 23 additions and 13 deletions

View File

@ -4,9 +4,9 @@ from datetime import datetime
import pytz
import os
os.environ['GLC_ENABLE'] = 'true'
os.environ['GLC_API_URL'] = 'http://10.168.1.160:10086/glc/v1/log/add'
os.environ['GLC_ENABLE_CONSOLE_LOG'] = 'false'
class CSTFormatter(logging.Formatter):
@ -64,5 +64,3 @@ class SaveLogger:
def critical(self, msg):
self.logger.critical(msg)

View File

@ -1,24 +1,27 @@
import os
import json
import datetime
import pytz
import requests
import platform
import socket
import netifaces
from decimal import Decimal
from datetime import datetime,timedelta
import datetime
import random
import sys
cached_ip = None
server_name = None
def get_server_name():
global server_name
if server_name is None:
server_name = platform.node()
return server_name
def get_intranet_ip():
global cached_ip
@ -35,7 +38,8 @@ def get_intranet_ip():
ip_addresses.append(ip)
# 对IP地址列表按照特定优先级排序(192优先)
sorted_ips = sorted(ip_addresses, key=lambda x: (x.startswith('192.'), x.startswith('172.'), x.startswith('10.')))
sorted_ips = sorted(ip_addresses,
key=lambda x: (x.startswith('192.'), x.startswith('172.'), x.startswith('10.')))
if sorted_ips:
cached_ip = sorted_ips[0]
else:
@ -43,7 +47,8 @@ def get_intranet_ip():
return cached_ip
def hash_string(input_str = ''):
def hash_string(input_str=''):
rs = 53653
i = len(input_str) if input_str is not None else 0
while i > 0:
@ -51,13 +56,17 @@ def hash_string(input_str = ''):
i -= 1
return str(Decimal(rs & 0xFFFFFFFF).to_eng_string())
# yyyy-MM-dd HH:mm:ss.SSS
def get_current_time():
current_time = datetime.datetime.now()
utc_now = datetime.datetime.now(pytz.utc)
cst_tz = pytz.timezone('Asia/Shanghai')
current_time = utc_now.astimezone(cst_tz)
return current_time.strftime('%Y-%m-%d %H:%M:%S.') + str(1000 + current_time.microsecond % 1000)[-3:]
class GlcData:
def __init__(self, logLevel = ''):
def __init__(self, logLevel=''):
self.text = ''
self.date = get_current_time()
self.system = os.getenv('GLC_SYSTEM')
@ -75,10 +84,10 @@ def post_glc_data(glc_data, logLevel):
url = os.getenv('GLC_API_URL')
if not url:
return
date = get_current_time()
data = {
'text': glc_data.text,
'date': glc_data.date,
'date': date,
'system': glc_data.system,
'servername': glc_data.serverName,
'serverip': glc_data.serverIp,
@ -90,7 +99,7 @@ def post_glc_data(glc_data, logLevel):
json_data = json.dumps(data)
headers = {'Content-Type': 'application/json', 'X-GLC-AUTH': 'glogcenter'}
glc_api_key = os.getenv('GLC_API_KEY') # X-GLC-AUTH:glogcenter
glc_api_key = os.getenv('GLC_API_KEY') # X-GLC-AUTH:glogcenter
if glc_api_key and ':' in glc_api_key:
key_parts = glc_api_key.split(':')
headers[key_parts[0]] = key_parts[1]
@ -133,6 +142,7 @@ def argsToGlcData(*args):
return glc_data
def get_log_level():
level = os.getenv('GLC_LOG_LEVEL')
if level is None:
@ -160,6 +170,7 @@ def debug(*args):
print(get_current_time(), '[DEBUG]', glc_data.text)
post_glc_data(glc_data, 'DEBUG')
def info(*args):
if get_log_level() <= 1:
glc_data = argsToGlcData(*args)
@ -170,6 +181,7 @@ def info(*args):
print(get_current_time(), ' [INFO]', glc_data.text)
post_glc_data(glc_data, 'INFO')
def warn(*args):
if get_log_level() <= 2:
glc_data = argsToGlcData(*args)
@ -180,6 +192,7 @@ def warn(*args):
print(get_current_time(), ' [WARN]', glc_data.text)
post_glc_data(glc_data, 'WARN')
def error(*args):
if get_log_level() <= 3:
glc_data = argsToGlcData(*args)

View File

@ -67,7 +67,6 @@ def get_voice_data(ser_data):
save_log.info(f'关闭电蚊香指令处理完返回,command:{com_input.hex()}')
glc.info(f'关闭电蚊香指令处理完返回,command:{com_input.hex()}',gd)
elif com_input.hex() in ('', 'aa15', 'aa19', 'aa12', 'ffffffffff'):
glc.info(f'存在或无效指令,command:{com_input.hex()}',gd)
pass
else:
save_log_error.error(f'Instruction error; error command: {com_input.hex()}')