I have Python script to send JSON encoded logs to Logstash via TCP: import socket import…
- User
I have Python script to send JSON encoded logs to Logstash via TCP:
import socket import json import time
Configuration for Logstash
LOGSTASH_HOST = 'HOST' # Replace with your Logstash server address LOGSTASH_PORT = PORT # Logstash input port
def send_log_to_logstash(message):
try: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect((LOGSTASH_HOST, LOGSTASH_PORT)) print(f"Connected to Logstash server at {LOGSTASH_HOST}:{LOGSTASH_PORT}") # Send the message as a JSON-formatted string sock.sendall((json.dumps(message) + "\n").encode('utf-8')) print("Message sent:", message) except Exception as e: print(f"Failed to send log to Logstash: {e}")def main():
log_message = { "@timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "level": "INFO", "message": "This is a test log message.", "service": "test-service" } send_log_to_logstash(log_message)if name == "main": main()
Please help me solve 3 problems:
-
Logstash has this output every connection: [INFO ][logstash.codecs.jsonlines][userspace][f039f77010b07546954c699afd8b51032fd785110e51d18431cf8cdf8080fb37] ECS compatibility is enabled but
targetoption was not specified. This may cause fields to be set at the top-level of the event where they are likely to clash with the Elastic Common Schema. It is recommended to set thetargetoption to avoid potential schema conflicts (if your data is ECS compliant or non-conflicting, feel free to ignore this message) -
How to send multiple logs by once? Give ability for this code to send multiple logs by one connection
-
Add milliseconds to timestamp in logs
-
- Assistant
- User
There is recommendation to set additional_codecs => {} which helps to solve this problem. But I dont know where I should put it
- Assistant
The
additional_codecsoption is used within thetcpinput plugin configuration in Logstash, allowing you to specify different codecs for different ports. However,additional_codecsis not directly related to resolving the ECS compatibility warning you're encountering. Instead, it is used to apply different codecs to different data formats concurrently on the same input but different ports.