Skip to content

Instantly share code, notes, and snippets.

@WilliamQLiu
Last active April 3, 2018 14:26
Show Gist options
  • Select an option

  • Save WilliamQLiu/cc3627079e773b3a5c975677cd79b7e3 to your computer and use it in GitHub Desktop.

Select an option

Save WilliamQLiu/cc3627079e773b3a5c975677cd79b7e3 to your computer and use it in GitHub Desktop.
Send to logstash
# send a sample message to logstash
import socket
import json
import sys
import pdb
HOST = 'localhost'
PORT = 5000
def log_msg(message=''):
""" Logs a message (python dict)
Example: message = {'@message': 'python test message', '@tags': ['python', 'test']}
"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, message:
sys.stderr.write("[ERROR] %s\n" % message[1])
sys.exit(1)
try:
sock.connect((HOST, PORT))
except socket.error, message:
sys.stderr.write("[ERROR] %s\n" % message[1])
sys.exit(2)
sock.send(json.dumps(message))
sock.close()
#sys.exit(0)
return "Logged Message"
if __name__ == '__main__':
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment