Python logging how to track hostname in logs?
Question:
I am using Python logging in this manner:
logger = logging.getLogger("my_logger")
logger.info("Some text.")
I have a bunch of IoT devices all running (making logs). They stream their log data to a database. To differentiate the source, I need to include the source IP address.
Is there a way to get hostname using logging? Is IP Address or hostname tracked/trackable in LogRecords?
In general, what is the best way to add in hostname to a LogRecord?
Answers:
You can do that by adding a custom log filter and formatter that puts the host name in the log messages.
import logging, platform
class HostnameFilter(logging.Filter):
hostname = platform.node()
def filter(self, record):
record.hostname = HostnameFilter.hostname
return True
handler = logging.StreamHandler()
handler.addFilter(HostnameFilter())
handler.setFormatter(logging.Formatter('%(asctime)s %(hostname)s: %(message)s', datefmt='%b %d %H:%M:%S'))
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.info('Hello, world!')
A simpler solution would be to pre-populate the format line with the hostname. For example:
logging.basicConfig(
#... your logging configuration,
format="%(asctime)s {} %(message)s".format(socket.gethostname())
)
would generate a format line like
"%(asctime)s some-hostname %(message)s"
The drawback is that if the host name changes you’d need to restart your service/application. This is usually not a problem for most applications, especially if running on docker/k8s
The easiest way to do this is available through the Polog library.
Install it:
$ pip install pip
And use:
from polog import log, config, file_writer
config.add_handlers(file_writer())
config.set(service_name='my_host_name')
log('message')
After executing the code, you will see in the console something similar:
[2022-10-26 19:26:04.759310] | 1 | UNKNOWN | MANUAL | "message" | where: my_host_name.?
I am using Python logging in this manner:
logger = logging.getLogger("my_logger")
logger.info("Some text.")
I have a bunch of IoT devices all running (making logs). They stream their log data to a database. To differentiate the source, I need to include the source IP address.
Is there a way to get hostname using logging? Is IP Address or hostname tracked/trackable in LogRecords?
In general, what is the best way to add in hostname to a LogRecord?
You can do that by adding a custom log filter and formatter that puts the host name in the log messages.
import logging, platform
class HostnameFilter(logging.Filter):
hostname = platform.node()
def filter(self, record):
record.hostname = HostnameFilter.hostname
return True
handler = logging.StreamHandler()
handler.addFilter(HostnameFilter())
handler.setFormatter(logging.Formatter('%(asctime)s %(hostname)s: %(message)s', datefmt='%b %d %H:%M:%S'))
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.info('Hello, world!')
A simpler solution would be to pre-populate the format line with the hostname. For example:
logging.basicConfig(
#... your logging configuration,
format="%(asctime)s {} %(message)s".format(socket.gethostname())
)
would generate a format line like
"%(asctime)s some-hostname %(message)s"
The drawback is that if the host name changes you’d need to restart your service/application. This is usually not a problem for most applications, especially if running on docker/k8s
The easiest way to do this is available through the Polog library.
Install it:
$ pip install pip
And use:
from polog import log, config, file_writer
config.add_handlers(file_writer())
config.set(service_name='my_host_name')
log('message')
After executing the code, you will see in the console something similar:
[2022-10-26 19:26:04.759310] | 1 | UNKNOWN | MANUAL | "message" | where: my_host_name.?