-
Notifications
You must be signed in to change notification settings - Fork 15
Allows custom message and date format. #4
base: master
Are you sure you want to change the base?
Changes from 4 commits
ab9bde9
b52f896
944eee0
da88faf
286f307
f9b92c8
90c6c54
479f9a2
1819c76
860015a
7c5f7e0
20716dd
696ba2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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' | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the reason; I'm just saying you shouldn't use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would that work?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
|
@@ -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), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please spell
DEFAULTout, it's not like we have identifier length restrictions.