code: def test_context_information_in_logging(
      serve_and_ray_shutdown, json_log_format
  ):
      """Make sure all context information exist in the log message"""
  
      logger = logging.getLogger("ray.serve")
  
      @serve.deployment(
          logging_config={
              "encoding": "JSON" if json_log_format else "TEXT"
          }
      )
      def fn(*args):
          logger.info("user func")
          request_context = (
              ray.serve.context._serve_request_context.get()
          )
          return {
              "request_id": request_context.request_id,
              "route": request_context.route,
              "app_name": request_context.app_name,
              "log_file": logger.handlers[1].baseFilename,
              "replica": serve.get_replica_context().replica_tag,
          }
  
      @serve.deployment(
          logging_config={
              "encoding": "JSON" if json_log_format else "TEXT"
          }
      )
      class Model:
          def __call__(self, req: starlette.requests.Request):
              logger.info("user log message from class method")
              request_context = (
                  ray.serve.context._serve_request_context.get()
              )
              return {
                  "request_id": request_context.request_id,
                  "route": request_context.route,
                  "app_name": request_context.app_name,
                  "log_file": logger.handlers[1].baseFilename,
                  "replica": serve.get_replica_context().replica_tag,
              }
  
      serve.run(fn.bind(), name="app1", route_prefix="/fn")
      serve.run(Model.bind(), name="app2", route_prefix="/class_method")
  
      f = io.StringIO()
      with redirect_stderr(f):
          resp = requests.get("http://127.0.0.1:8000/fn").json()
          resp2 = requests.get(
              "http://127.0.0.1:8000/class_method"
          ).json()
  
          # Check the component log
          expected_log_infos = [
              f"{resp['request_id']} {resp['route']} replica.py",
              f"{resp2['request_id']} {resp2['route']} replica.py",
          ]
  
          # Check User log
          user_log_regexes = [
              f".*{resp['request_id']} {resp['route']}.* user func.*",
              f".*{resp2['request_id']} {resp2['route']}.* user log "
              "message from class method.*",
          ]
  
          def check_log():
              logs_content = ""
              for _ in range(20):
                  time.sleep(0.1)
                  logs_content = f.getvalue()
                  if logs_content:
                      break
              for expected_log_info in expected_log_infos:
                  assert expected_log_info in logs_content
              for regex in user_log_regexes:
                  assert re.findall(regex, logs_content) != []
  
          # Check stream log
          check_log()
  
          # Check user log file
          method_replica_id = resp["replica"].split("#")[-1]
          class_method_replica_id = resp2["replica"].split("#")[-1]
          if json_log_format:
              user_method_log_regex = (
                  f'.*"deployment": "{resp["app_name"]}_fn", '
                  f'"replica": "{method_replica_id}", '
                  f'"request_id": "{resp["request_id"]}", '
                  f'"route": "{resp["route"]}", '
                  f'"application": "{resp["app_name"]}", "message":.* user func.*'
              )
              user_class_method_log_regex = (
                  f'.*"deployment": "{resp2["app_name"]}_Model", '
                  f'"replica": "{class_method_replica_id}", '
                  f'"request_id": "{resp2["request_id"]}", '
                  f'"route": "{resp2["route"]}", '
                  f'"application": "{resp2["app_name"]}", "message":.* user log '
                  "message from class method.*"
              )
          else:
              user_method_log_regex = f".*{resp['request_id']} {resp['route']}.* user func.*"
              user_class_method_log_regex = (
                  f".*{resp2['request_id']} {resp2['route']}.* "
                  "user log message from class method.*"
              )
  
          def check_log_file(log_file: str, expected_regex: list):
              with open(log_file, "r") as f:
                  s = f.read()
                  assert re.findall(expected_regex, s) != []
  
          check_log_file(resp["log_file"], user_method_log_regex)
          check_log_file(resp2["log_file"], user_class_method_log_regex)
  
imports: import io
  import json
  import logging
  import os
  import re
  import string
  import sys
  import time
  from contextlib import redirect_stderr
  import pytest
  import requests
  import starlette
  import ray
  import ray.util.state
  from ray import serve
  from ray._private.test_utils import wait_for_condition
  from ray.serve._private.common import ServeComponentType
  from ray.serve._private.constants import SERVE_LOG_EXTRA_FIELDS
  from ray.serve._private.logging_utils import ServeJSONFormatter, get_component_log_file_name, get_serve_logs_dir
label_desc: Use image node_id 9 for calling a global function with example usage: test_context_information_in_logging(serve_and_ray_shutdown, json_log_format) and returns: dict, dict