I am using Python with logging module and would like to add the socket.hostname() to every log message, I have to run this query every message and can not use
You can use filter to add information to every message :
import logging
import socket
class ContextFilter(logging.Filter):
def filter(self, record):
record.hostname = socket.gethostname()
return True
if __name__ == '__main__':
levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL)
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)-15s hostname: %(hostname)-15s : %(message)s')
a1 = logging.getLogger('a.b.c')
f = ContextFilter()
a1.addFilter(f)
a1.debug('A debug message')