mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-27 17:18:13 +00:00
Fix logger not working in FastAPI projects after upgrading to 0.108 (#2473)
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
41
tests/utilities/test_logger.py
Normal file
41
tests/utilities/test_logger.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import sys
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from io import StringIO
|
||||
|
||||
from crewai.utilities.logger import Logger
|
||||
|
||||
|
||||
class TestLogger(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.logger = Logger(verbose=True)
|
||||
self.output = StringIO()
|
||||
self.old_stdout = sys.stdout
|
||||
sys.stdout = self.output
|
||||
|
||||
def tearDown(self):
|
||||
sys.stdout = self.old_stdout
|
||||
|
||||
def test_log_in_sync_context(self):
|
||||
self.logger.log("info", "Test message")
|
||||
output = self.output.getvalue()
|
||||
self.assertIn("[INFO]: Test message", output)
|
||||
self.assertIn("\n", output)
|
||||
|
||||
@patch('sys.stdout.flush')
|
||||
def test_stdout_is_flushed(self, mock_flush):
|
||||
self.logger.log("info", "Test message")
|
||||
mock_flush.assert_called_once()
|
||||
|
||||
|
||||
class TestFastAPICompatibility(unittest.TestCase):
|
||||
def test_import_in_fastapi(self):
|
||||
try:
|
||||
import fastapi
|
||||
from crewai.utilities.logger import Logger
|
||||
logger = Logger(verbose=True)
|
||||
self.assertTrue(True)
|
||||
except ImportError:
|
||||
self.skipTest("FastAPI not installed")
|
||||
except Exception as e:
|
||||
self.fail(f"Unexpected error: {e}")
|
||||
Reference in New Issue
Block a user