What is the proper way to write a syslog function in python?
<p>Despite my efforts I have been unable to figure out how to create a function that works with logging in python. No matter what I do, I get a duplicate syslog entry.</p>

<pre><code>def send_syslog(input):
    my_logger = logging.getLogger()
    my_logger.setLevel(logging.DEBUG)
    handler = logging.handlers.SysLogHandler(address='/dev/log', facility=19)
    my_logger.addHandler(handler)
    my_logger.info("SYSTEM MESSAGE: " + str(input))

def main():
    send_syslog("Everything is working okay!")

if __name__ == "__main__":
    main()
</code></pre>

<p>I have tried re-ordering, writing like this but I get the same thing:  </p>

<pre><code>def send_syslog(input):
    my_logger.info("SYSTEM MESSAGE: " + str(input))

def main():
    my_logger = logging.getLogger()
    my_logger.setLevel(logging.DEBUG)
    handler = logging.handlers.SysLogHandler(address='/dev/log', facility=19)
    my_logger.addHandler(handler)
    send_syslog("Everything is working okay!")

if __name__ == "__main__":
    main()
</code></pre>

<p>What is the proper way to write a syslog function I can reuse in my own program?</p>

<p>You should use the python <a href="https://docs.python.org/2/library/syslog.html" rel="nofollow">syslog</a> module:</p>

<pre><code>import syslog
def main():
    syslog.syslog(syslog.LOG_INFO, "Everything is working okay!")
    syslog.syslog(syslog.LOG_ERR, 'This is logged at the error level.')

if __name__ == "__main__":
    main()
</code></pre>

python function logging syslog