def some_route(): log = logger.new(request_id=str(uuid.uuid4())) # do something # ... log.info("user logged in", user="******") # gives you: # event='user logged in' request_id='ffcdc44f-b952-4b5f-95e6-0f1f3a9ee5fd' user='******' # ... some_function() # ... return "logged in!"
def test_load_json_again(self): with mock.patch( "some_module.json" ) as mock_json: # * Can also use a context manager instead of a decorator o = mock.Mock() o.ttl = 1500.0 mock_json.loads.return_value = o self.assertEqual(some_module.some_function("test"), 1500.0)
def some_route(): # You would put this into some kind of middleware or processor so it's set # automatically for all requests in all views. structlog.threadlocal.clear_threadlocal() structlog.threadlocal.bind_threadlocal( view=flask.request.path, request_id=str(uuid.uuid4()), peer=flask.request.access_route[0], ) # End of belongs-to-middleware. log = logger.bind() # do something # ... log.info("user logged in", user="******") # ... some_function() # ... return "logged in!"
def main(): some_function()
def test_json_load(self, mock_json): # * Argument is the patched object o = mock.Mock() o.ttl = 1500.0 mock_json.loads.return_value = o self.assertEqual(some_module.some_function("test"), 1500.0)
def test_load_json_again_again(self, mock_json): self.assertEqual(some_module.some_function("test"), 1500.0)
def do_something(x): """A silly function.""" y = x z = some_function(y) # Stepping in opens the relevant file return z + " rules!"