diff --git a/rattail/logging.py b/rattail/logging.py index 94d697a4c9cf5e83b81cf9b0093086b0f523ae1d..b646d4632994ca144c0f3663539ce34200eb0dc3 100644 --- a/rattail/logging.py +++ b/rattail/logging.py @@ -28,9 +28,11 @@ Logging Utilities from __future__ import absolute_import +import os +import sys import logging import socket -import sys +import getpass class RattailAdapter(logging.LoggerAdapter): @@ -43,6 +45,8 @@ class RattailAdapter(logging.LoggerAdapter): * ``'hostname'`` - The fully-qualified domain name of the host machine. * ``'hostip'`` - The IPv4 address of the host machine. * ``'argv'`` - The value of ``sys.argv`` (a list). + * ``'uid'`` - The effective UID of the running process. + * ``'username'`` - The login name of the effective user. """ def __init__(self, logger): @@ -51,6 +55,8 @@ class RattailAdapter(logging.LoggerAdapter): 'hostname': hostname, 'hostip': socket.gethostbyname(hostname), 'argv': sys.argv, + 'uid': os.getuid(), + 'username': getpass.getuser(), } # LoggerAdapter is a new-style class only as of Python 2.7; must not # use super() in case we're running on Python 2.6. diff --git a/tests/test_logging.py b/tests/test_logging.py index 08fe3b0ffd2857ccb2daf8faa4a3e73f77d7e2d6..f821c81a77e69bd976af409916580a504e4f9ff8 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -10,13 +10,17 @@ from rattail import logging as rattail_logging class TestLogging(TestCase): + @patch('rattail.logging.os') @patch('rattail.logging.sys') @patch('rattail.logging.socket') - def test_adapter_adds_all_context(self, socket, sys): + @patch('rattail.logging.getpass') + def test_adapter_adds_all_context(self, getpass, socket, sys, os): socket.getfqdn.return_value = 'testing.rattailproject.org' socket.gethostbyname.return_value = '127.0.0.1' sys.argv = ['just', 'testing'] - formatter = logging.Formatter(u"%(hostname)s %(hostip)s %(argv)s %(levelname)s %(message)s") + os.getuid.return_value = 420 + getpass.getuser.return_value = 'joeschmoe' + formatter = logging.Formatter(u"%(hostname)s %(hostip)s %(argv)s %(username)s %(uid)s %(levelname)s %(message)s") string = StringIO() handler = logging.StreamHandler(string) handler.setFormatter(formatter) @@ -26,5 +30,5 @@ class TestLogging(TestCase): log = rattail_logging.RattailAdapter(log) self.assertEqual(string.getvalue(), "") log.debug("some random thing") - self.assertEqual(string.getvalue(), u"testing.rattailproject.org 127.0.0.1 ['just', 'testing'] DEBUG some random thing\n") + self.assertEqual(string.getvalue(), u"testing.rattailproject.org 127.0.0.1 ['just', 'testing'] joeschmoe 420 DEBUG some random thing\n") string.close()