Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.
61 changes: 42 additions & 19 deletions supervisor_logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,49 @@ 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())
)
DEFAULT_DATE_FORMAT = '%Y-%m-%dT%H:%M:%S%z'
DEFAULT_MESSAGE_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):
"""
Use user defined message format via
os.environ['SYSLOG_MESSAGE_FORMAT'] or
DEFAULT_MESSAGE_FORMAT as default.
"""

fmt = os.environ.get(
'SYSLOG_MESSAGE_FORMAT',
self.DEFAULT_MESSAGE_FORMAT
)
return fmt.replace(
'%(hostname)s',
self.HOSTNAME
) # Accepts hostname in the form of %(hostname)s

def date_format(self):
"""
Use user defined date format via
os.environ['SYSLOG_DATE_FORMAT'] or
DEFAULT_DATE_FORMAT as default.
"""
return os.environ.get(
'SYSLOG_DATE_FORMAT',
self.DEFAULT_DATE_FORMAT
)


class SysLogHandler(logging.handlers.SysLogHandler):
Expand Down Expand Up @@ -102,15 +130,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
47 changes: 47 additions & 0 deletions tests/test_date_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# Copyright 2014 Infoxchange Australia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Test date_format.
"""

import os
import datetime
from unittest import TestCase
from supervisor_logging import PalletFormatter


class SupervisorLoggingDateFormatTestCase(TestCase):
"""
Tests date format.
"""

def test_default_date_format(self):
"""
Test default date format.
"""
date = datetime.datetime(2000, 1, 1, 1, 0, 0)
date_format = PalletFormatter().date_format()
self.assertEqual(date.strftime(date_format), '2000-01-01T01:00:00')

def test_custom_date_format(self):
"""
Test custom date format.
"""
date = datetime.datetime(2000, 1, 1, 1, 0, 0)
os.environ['SYSLOG_DATE_FORMAT'] = '%b %d %H:%M:%S'
date_format = PalletFormatter().date_format()
self.assertEqual(date.strftime(date_format), 'Jan 01 01:00:00')
os.environ['SYSLOG_DATE_FORMAT'] = PalletFormatter.DEFAULT_DATE_FORMAT
2 changes: 1 addition & 1 deletion tests/test_supervisor_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def strip_volatile(message):

volatile = (
(socket.gethostname(), 'HOST'),
(r'\w{3} \d{2} \d{2}:\d{2}:\d{2}', 'DATE'),
(r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}-\d{4}', 'DATE'),
)

for regexp, replacement in volatile:
Expand Down