Add hostname field to JSONFormatter

This change adds 'hostname' field to JSONFormatter.
The field is helpful to distinguish which host sent the logs.

Closes-Bug: #1501232
Change-Id: Ie4ba197543bc8ebd0e22c509a9fe3190069aa5a6
This commit is contained in:
Masaki Matsushita 2015-09-30 17:42:40 +09:00
parent 69e0ba3d75
commit 7d190970e3
1 changed files with 7 additions and 1 deletions

View File

@ -14,6 +14,7 @@ import itertools
import logging
import logging.config
import logging.handlers
import socket
import sys
import traceback
@ -66,6 +67,10 @@ class JSONFormatter(logging.Formatter):
# NOTE(jkoelker) we ignore the fmt argument, but its still there
# since logging.config.fileConfig passes it.
self.datefmt = datefmt
try:
self.hostname = socket.gethostname()
except socket.error:
self.hostname = None
def formatException(self, ei, strip_newlines=True):
lines = traceback.format_exception(*ei)
@ -96,7 +101,8 @@ class JSONFormatter(logging.Formatter):
'thread_name': record.threadName,
'process_name': record.processName,
'process': record.process,
'traceback': None}
'traceback': None,
'hostname': self.hostname}
# Build the extra values that were given to us, including
# the context.