|
10 | 10 | from json.decoder import JSONDecodeError
|
11 | 11 | from tempfile import NamedTemporaryFile
|
12 | 12 | from typing import Any
|
13 |
| -from unittest.mock import patch |
| 13 | +from unittest.mock import patch, MagicMock |
14 | 14 | from uuid import uuid4
|
15 | 15 |
|
16 | 16 | import pytest
|
17 | 17 |
|
| 18 | +from vllm.entrypoints.logger import RequestLogger |
18 | 19 | from vllm.logger import (_DATE_FORMAT, _FORMAT, _configure_vllm_root_logger,
|
19 | 20 | enable_trace_function_call, init_logger)
|
20 | 21 | from vllm.logging_utils import NewLineFormatter
|
@@ -253,3 +254,202 @@ class CustomClass:
|
253 | 254 |
|
254 | 255 | assert (prepare_object_to_dump(CustomClass(
|
255 | 256 | 1, 'b')) == "CustomClass(a=1, b='b')")
|
| 257 | + |
| 258 | + |
| 259 | +def test_request_logger_log_outputs(): |
| 260 | + """Test the new log_outputs functionality.""" |
| 261 | + # Create a mock logger to capture log calls |
| 262 | + mock_logger = MagicMock() |
| 263 | + |
| 264 | + with patch('vllm.entrypoints.logger.logger', mock_logger): |
| 265 | + request_logger = RequestLogger(max_log_len=None) |
| 266 | + |
| 267 | + # Test basic output logging |
| 268 | + request_logger.log_outputs( |
| 269 | + request_id="test-123", |
| 270 | + outputs="Hello, world!", |
| 271 | + output_token_ids=[1, 2, 3, 4], |
| 272 | + finish_reason="stop", |
| 273 | + is_streaming=False, |
| 274 | + delta=False |
| 275 | + ) |
| 276 | + |
| 277 | + mock_logger.info.assert_called_once() |
| 278 | + call_args = mock_logger.info.call_args[0] |
| 279 | + assert "Generated response test-123" in call_args[0] |
| 280 | + assert "Hello, world!" in call_args[1] |
| 281 | + assert [1, 2, 3, 4] == call_args[2] |
| 282 | + assert "stop" == call_args[3] |
| 283 | + |
| 284 | + |
| 285 | +def test_request_logger_log_outputs_streaming_delta(): |
| 286 | + """Test log_outputs with streaming delta mode.""" |
| 287 | + mock_logger = MagicMock() |
| 288 | + |
| 289 | + with patch('vllm.entrypoints.logger.logger', mock_logger): |
| 290 | + request_logger = RequestLogger(max_log_len=None) |
| 291 | + |
| 292 | + # Test streaming delta logging |
| 293 | + request_logger.log_outputs( |
| 294 | + request_id="test-456", |
| 295 | + outputs="Hello", |
| 296 | + output_token_ids=[1], |
| 297 | + finish_reason=None, |
| 298 | + is_streaming=True, |
| 299 | + delta=True |
| 300 | + ) |
| 301 | + |
| 302 | + mock_logger.info.assert_called_once() |
| 303 | + call_args = mock_logger.info.call_args[0] |
| 304 | + assert "Generated response test-456 (streaming delta)" in call_args[0] |
| 305 | + assert "Hello" == call_args[1] |
| 306 | + assert [1] == call_args[2] |
| 307 | + assert call_args[3] is None |
| 308 | + |
| 309 | + |
| 310 | +def test_request_logger_log_outputs_streaming_complete(): |
| 311 | + """Test log_outputs with streaming complete mode.""" |
| 312 | + mock_logger = MagicMock() |
| 313 | + |
| 314 | + with patch('vllm.entrypoints.logger.logger', mock_logger): |
| 315 | + request_logger = RequestLogger(max_log_len=None) |
| 316 | + |
| 317 | + # Test streaming complete logging |
| 318 | + request_logger.log_outputs( |
| 319 | + request_id="test-789", |
| 320 | + outputs="Complete response", |
| 321 | + output_token_ids=[1, 2, 3], |
| 322 | + finish_reason="length", |
| 323 | + is_streaming=True, |
| 324 | + delta=False |
| 325 | + ) |
| 326 | + |
| 327 | + mock_logger.info.assert_called_once() |
| 328 | + call_args = mock_logger.info.call_args[0] |
| 329 | + assert "Generated response test-789 (streaming complete)" in call_args[0] |
| 330 | + assert "Complete response" == call_args[1] |
| 331 | + assert [1, 2, 3] == call_args[2] |
| 332 | + assert "length" == call_args[3] |
| 333 | + |
| 334 | + |
| 335 | +def test_request_logger_log_outputs_with_truncation(): |
| 336 | + """Test log_outputs respects max_log_len setting.""" |
| 337 | + mock_logger = MagicMock() |
| 338 | + |
| 339 | + with patch('vllm.entrypoints.logger.logger', mock_logger): |
| 340 | + # Set max_log_len to 10 |
| 341 | + request_logger = RequestLogger(max_log_len=10) |
| 342 | + |
| 343 | + # Test output truncation |
| 344 | + long_output = "This is a very long output that should be truncated" |
| 345 | + long_token_ids = list(range(20)) # 20 tokens |
| 346 | + |
| 347 | + request_logger.log_outputs( |
| 348 | + request_id="test-truncate", |
| 349 | + outputs=long_output, |
| 350 | + output_token_ids=long_token_ids, |
| 351 | + finish_reason="stop", |
| 352 | + is_streaming=False, |
| 353 | + delta=False |
| 354 | + ) |
| 355 | + |
| 356 | + mock_logger.info.assert_called_once() |
| 357 | + call_args = mock_logger.info.call_args |
| 358 | + |
| 359 | + # Check that output was truncated to first 10 characters |
| 360 | + logged_output = call_args[0][1] |
| 361 | + assert logged_output == "This is a " |
| 362 | + assert len(logged_output) == 10 |
| 363 | + |
| 364 | + # Check that token IDs were truncated to first 10 tokens |
| 365 | + logged_token_ids = call_args[0][2] |
| 366 | + assert logged_token_ids == list(range(10)) |
| 367 | + assert len(logged_token_ids) == 10 |
| 368 | + |
| 369 | + |
| 370 | +def test_request_logger_log_outputs_none_values(): |
| 371 | + """Test log_outputs handles None values correctly.""" |
| 372 | + mock_logger = MagicMock() |
| 373 | + |
| 374 | + with patch('vllm.entrypoints.logger.logger', mock_logger): |
| 375 | + request_logger = RequestLogger(max_log_len=None) |
| 376 | + |
| 377 | + # Test with None output_token_ids |
| 378 | + request_logger.log_outputs( |
| 379 | + request_id="test-none", |
| 380 | + outputs="Test output", |
| 381 | + output_token_ids=None, |
| 382 | + finish_reason="stop", |
| 383 | + is_streaming=False, |
| 384 | + delta=False |
| 385 | + ) |
| 386 | + |
| 387 | + mock_logger.info.assert_called_once() |
| 388 | + call_args = mock_logger.info.call_args[0] |
| 389 | + assert "Generated response test-none" in call_args[0] |
| 390 | + assert "Test output" == call_args[1] |
| 391 | + assert call_args[2] is None |
| 392 | + assert "stop" == call_args[3] |
| 393 | + |
| 394 | + |
| 395 | +def test_request_logger_log_outputs_empty_output(): |
| 396 | + """Test log_outputs handles empty output correctly.""" |
| 397 | + mock_logger = MagicMock() |
| 398 | + |
| 399 | + with patch('vllm.entrypoints.logger.logger', mock_logger): |
| 400 | + request_logger = RequestLogger(max_log_len=5) |
| 401 | + |
| 402 | + # Test with empty output |
| 403 | + request_logger.log_outputs( |
| 404 | + request_id="test-empty", |
| 405 | + outputs="", |
| 406 | + output_token_ids=[], |
| 407 | + finish_reason="stop", |
| 408 | + is_streaming=False, |
| 409 | + delta=False |
| 410 | + ) |
| 411 | + |
| 412 | + mock_logger.info.assert_called_once() |
| 413 | + call_args = mock_logger.info.call_args[0] |
| 414 | + assert "Generated response test-empty" in call_args[0] |
| 415 | + assert "" == call_args[1] |
| 416 | + assert [] == call_args[2] |
| 417 | + assert "stop" == call_args[3] |
| 418 | + |
| 419 | + |
| 420 | +def test_request_logger_log_outputs_integration(): |
| 421 | + """Test that log_outputs can be called alongside log_inputs.""" |
| 422 | + mock_logger = MagicMock() |
| 423 | + |
| 424 | + with patch('vllm.entrypoints.logger.logger', mock_logger): |
| 425 | + request_logger = RequestLogger(max_log_len=None) |
| 426 | + |
| 427 | + # Test that both methods can be called without interference |
| 428 | + request_logger.log_inputs( |
| 429 | + request_id="test-integration", |
| 430 | + prompt="Test prompt", |
| 431 | + prompt_token_ids=[1, 2, 3], |
| 432 | + prompt_embeds=None, |
| 433 | + params=None, |
| 434 | + lora_request=None, |
| 435 | + prompt_adapter_request=None |
| 436 | + ) |
| 437 | + |
| 438 | + request_logger.log_outputs( |
| 439 | + request_id="test-integration", |
| 440 | + outputs="Test output", |
| 441 | + output_token_ids=[4, 5, 6], |
| 442 | + finish_reason="stop", |
| 443 | + is_streaming=False, |
| 444 | + delta=False |
| 445 | + ) |
| 446 | + |
| 447 | + # Should have been called twice - once for inputs, once for outputs |
| 448 | + assert mock_logger.info.call_count == 2 |
| 449 | + |
| 450 | + # Check that the calls were made with correct patterns |
| 451 | + input_call = mock_logger.info.call_args_list[0][0] |
| 452 | + output_call = mock_logger.info.call_args_list[1][0] |
| 453 | + |
| 454 | + assert "Received request test-integration" in input_call[0] |
| 455 | + assert "Generated response test-integration" in output_call[0] |
0 commit comments