'python prevent logging writing log to syslog

This is my logging setting. It works fine and I can find log in test.log but I also find log in /var/log/messages something looks like

Dec  9 16:13:39 ip-10-80-48-6 python: DEBUG:__main__:Test Log

I don't want to log to /var/log/messages maybe other path is better because the log is too big

import logging
logger = logging.getLogger(__name__)
logging.basicConfig()
logger.setLevel(logging.DEBUG)

handler = logging.handlers.RotatingFileHandler('test.log', "a", maxBytes=50000000, backupCount=10)
handler.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))

logger.addHandler(handler)

#Update

This code is in my twisted service I use systemctl to start this service



Solution 1:[1]

Refer below links. Explained with examples.

https://docs.python.org/2.7/howto/logging-cookbook.html#logging-to-multiple-destinations

https://docs.python.org/3/howto/logging-cookbook.html#logging-to-multiple-destinations.

In the example (Explained in the link), they have redirected output to a file and console. If your requirement is to redirect to some other file instead of /var/log/messages; pass "test.log" as parameter to basicConfig() function and create one more handler for syslog as below.

sysloghandler = logging.handlers.SysLogHandler("/dev/log") 
sysloghandler.setLevel(logging.INFO) 
syslogformatter = logging.Formatter('%(name)s: %(level)s %(message)s') 
sysloghandler.setFormatter(syslogformatter) 
logging.getLogger('myapp').addHandler(sysloghandler)

Solution 2:[2]

logging.basicConfig()

is what obviously sets a standard handler that logs to /var/log/messages Try removing it or simply remove all handlers before adding yours. I have never used python :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 user7270881