Created
August 14, 2014 16:08
-
-
Save nfarrar/884c72ec107a00606a86 to your computer and use it in GitHub Desktop.
A simple command line python syslog client for generating test messages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import logging | |
import logging.handlers | |
parser = argparse.ArgumentParser(__file__, | |
description="A syslog message generator") | |
parser.add_argument("--address", | |
"-a", | |
default="localhost", | |
help="The syslog message recipient address") | |
parser.add_argument("--port", | |
"-p", | |
type=int, | |
default=5514, | |
help="The syslog message recipient port") | |
parser.add_argument("--level", | |
"-l", | |
default="DEBUG", | |
help="The syslog message log level") | |
parser.add_argument("--message", | |
"-m", | |
required=True, | |
help="The syslog message") | |
def string_to_level(log_level): | |
""" Convert a commandline string to a proper log level | |
@param string log_level command line log level argument | |
@return logging.LEVEL the logging.LEVEL object to return | |
""" | |
if log_level == "CRITICAL": | |
return logging.CRITICAL | |
if log_level == "ERROR": | |
return logging.ERROR | |
if log_level == "WARNING": | |
return logging.WARNING | |
if log_level == "INFO": | |
return logging.INFO | |
if log_level == "DEBUG": | |
return logging.DEBUG | |
return logging.NOTSET | |
if __name__ == "__main__": | |
args = parser.parse_args() | |
syslogger = logging.getLogger('SyslogLogger') | |
syslogger.setLevel(string_to_level(args.level)) | |
handler = logging.handlers.SysLogHandler(address=(args.address, args.port), | |
facility=19) | |
syslogger.addHandler(handler) | |
syslogger.log(args.message) |
Thanks Nathan for this great script, I reused after doing some small changes and it is working fine.
The main change I did is using socktype=socket.SOCK_STREAM as argument of logging.handlers.SysLogHandler() function to user TCP instead of UDP.
Thanks again.
Hamid
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, I was looking for something quick that I can use to generate messages,; I understand the script is old, but this is what is needed to work with the latest version of the logging module
replace:
syslogger.log(args.message)
with:
syslogger.log(string_to_level(args.level),args.message)