Spaces:
Running
on
L4
Running
on
L4
import os | |
import ssl | |
import json | |
import socket | |
import datetime | |
def log_event(input_text, template, prediction): | |
timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat() | |
print(timestamp) | |
log_message = f'<6>1 {timestamp} 149.202.165.20 example.org - - [exampleSDID@8485 X-OVH-TOKEN="{os.environ["LOG_TOKEN"]}" template={json.dumps(template)} text={json.dumps(input_text)} prediction={json.dumps(prediction)}] {timestamp} / {json.dumps(input_text[:20])}\n' | |
print(log_message) | |
server = os.environ["LOG_SERVER"] | |
port = int(os.environ["LOG_PORT"]) | |
# Create a TCP connection and wrap it with SSL | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
ssl_sock = ssl.wrap_socket(sock) | |
print(server, port) | |
try: | |
# Connect to the server | |
ssl_sock.connect((server, port)) | |
# Send the log message | |
ssl_sock.sendall(log_message.encode('utf-8')) | |
print('Log message sent successfully.') | |
finally: | |
# Close the connection | |
ssl_sock.close() |