I am working on sysloghander python to send logs to a centralized syslog server. The code works, but I have to deal with some problems when setting up the format. Below is the code I wrote.
#!/usr/bin/python import logging from logging.handlers import SysLogHandler import subprocess hostname = subprocess.check_output(['hostname', '-f']) logger = logging.getLogger() logger.setLevel(logging.INFO) syslog = SysLogHandler(address=('log.central.log', 514)) formatter = logging.Formatter('%(asctime)s %(name)s: %(levelname)s %(message)s', '%b %e %H:%M:%S') syslog.setFormatter(formatter) logger.addHandler(syslog) logger.info("My Message")
Can I add my own tags / variables to the log formatter. I want to add the host name and application name to them, since I have a central logging server installed to create log files based on the host name and application name.
App_name can be anything, for example, I can set app_name to "mysql_communication_log" or "wsgi_log", and then separate files with the same name will be created on the central server.
What I'm looking for is something like
formatter = logging.Formatter('%(asctime)s %(hostname)s %(app_name)s: %(message)s', '%b %e %H:%M:%S')
source share