Skip to content

Instantly share code, notes, and snippets.

@haginara
Last active November 11, 2015 21:26
Show Gist options
  • Save haginara/689f516fb45143d1f502 to your computer and use it in GitHub Desktop.
Save haginara/689f516fb45143d1f502 to your computer and use it in GitHub Desktop.
import logging
import logging.handlers
import argparse
import socket
def arguments():
parser = argparse.ArgumentParser(__file__, description='syslog logger')
parser.add_argument("--address", "-n", default="localhost")
parser.add_argument("--port", "-p", type=int, default=514)
parser.add_argument("--tcp", "-t", action='store_true')
parser.add_argument("message", nargs=1)
return parser.parse_args()
if __name__ == '__main__':
args = arguments()
socktype = socket.SOCK_STREAM if args.tcp else socket.SOCK_DGRAM
syslogger = logging.getLogger('SyslogLogger')
syslogger.setLevel(logging.INFO)
handler = logging.handlers.SysLogHandler(
address=(args.address, args.port), facility=19, socktype=socktype)
syslogger.addHandler(handler)
msg = args.message[0]
print("{} {} {} {}".format(args.address, args.port, socktype, msg))
syslogger.log(logging.INFO, msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment