Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.
50 changes: 31 additions & 19 deletions supervisor_logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,38 @@ class PalletFormatter(logging.Formatter):
"""

HOSTNAME = re.sub(
r':\d+$', '', os.environ.get('SITE_DOMAIN', socket.gethostname()))
FORMAT = '%(asctime)s {hostname} %(name)s[%(process)d]: %(message)s'.\
format(hostname=HOSTNAME)
DATE_FORMAT = '%b %d %H:%M:%S'
r':\d+$',
'',
os.environ.get('SITE_DOMAIN', socket.gethostname())
)
DFLT_DATE_FORMAT = '%b %d %H:%M:%S'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please spell DEFAULT out, it's not like we have identifier length restrictions.

DFLT_MSG_FORMAT = \
'%(asctime)s %(hostname)s %(name)s[%(process)d]: %(message)s'

def __init__(self):
super(PalletFormatter, self).__init__(fmt=self.FORMAT,
datefmt=self.DATE_FORMAT)
super(PalletFormatter, self).__init__(
fmt=self.message_format(), datefmt=self.date_format()
)

def format(self, record):
# strip newlines
message = super(PalletFormatter, self).format(record)
message = message.replace('\n', ' ')
message += '\n'
return message
return message.replace('\n', ' ') + '\n'

def message_format(self):
fmt = os.environ.get(
'SYSLOG_MESSAGE_FORMAT',
self.__class__.DFLT_MSG_FORMAT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not self.DFLT_MSG_FORMAT (here and below)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log routers such as Heka or Logstash are picky about which syslog RFC it is accepting.
So I figured why not let user defines which format they want through os.environ[SYSLOG_MESSAGE_FORMAT]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the reason; I'm just saying you shouldn't use .__class__ here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that work? message_format is an instance method and DEFAULT_MESSAGE_FORMAT is class variable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will.

)
return fmt.replace(
'%(hostname)s',
self.__class__.HOSTNAME
) # Accepts hostname in the form of %(hostname)s

def date_format(self):
return os.environ.get(
'SYSLOG_DATE_FORMAT',
self.__class__.DFLT_DATE_FORMAT
)


class SysLogHandler(logging.handlers.SysLogHandler):
Expand Down Expand Up @@ -102,15 +119,10 @@ def main():
Main application loop.
"""

env = os.environ

try:
host = env['SYSLOG_SERVER']
port = int(env['SYSLOG_PORT'])
socktype = socket.SOCK_DGRAM if env['SYSLOG_PROTO'] == 'udp' \
else socket.SOCK_STREAM
except KeyError:
sys.exit("SYSLOG_SERVER, SYSLOG_PORT and SYSLOG_PROTO are required.")
host = os.environ.get('SYSLOG_SERVER', '127.0.0.1')
port = int(os.environ.get('SYSLOG_PORT', '514'))
proto = os.environ.get('SYSLOG_PROTO', 'udp')
socktype = socket.SOCK_DGRAM if proto == 'udp' else socket.SOCK_STREAM

handler = SysLogHandler(
address=(host, port),
Expand Down