Skip to content

Commit 169fd32

Browse files
fix(logger): add optional cache to configure; update caching behavior (#9532)
* fix: update logger configuration to use environment variable for log level * fix: remove default log level configuration and set logger initialization * fix: enhance logger configuration to prevent redundant setup and improve cache handling * fix: improve cache handling in logger configuration to prevent unintended defaults * fix: enhance logger configuration to prevent redundant setup and improve early-exit logic * fix: remove defensive comment in logger configuration for clarity --------- Co-authored-by: Jordan Frazier <[email protected]>
1 parent 8d26d52 commit 169fd32

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/backend/base/langflow/logging/logger.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,22 @@ def configure(
208208
log_env: str | None = None,
209209
log_format: str | None = None,
210210
log_rotation: str | None = None,
211+
cache: bool | None = None,
211212
) -> None:
212213
"""Configure the logger."""
214+
# Early-exit only if structlog is configured AND current min level matches the requested one.
215+
cfg = structlog.get_config() if structlog.is_configured() else {}
216+
wrapper_class = cfg.get("wrapper_class")
217+
current_min_level = getattr(wrapper_class, "min_level", None)
213218
if os.getenv("LANGFLOW_LOG_LEVEL", "").upper() in VALID_LOG_LEVELS and log_level is None:
214219
log_level = os.getenv("LANGFLOW_LOG_LEVEL")
220+
221+
requested_min_level = LOG_LEVEL_MAP.get(
222+
(log_level or os.getenv("LANGFLOW_LOG_LEVEL", "ERROR")).upper(), logging.ERROR
223+
)
224+
if current_min_level == requested_min_level:
225+
return
226+
215227
if log_level is None:
216228
log_level = "ERROR"
217229

@@ -260,15 +272,19 @@ def configure(
260272
# Get numeric log level
261273
numeric_level = LOG_LEVEL_MAP.get(log_level.upper(), logging.ERROR)
262274

275+
# Create wrapper class and attach the min level for later comparison
276+
wrapper_class = structlog.make_filtering_bound_logger(numeric_level)
277+
wrapper_class.min_level = numeric_level
278+
263279
# Configure structlog
264280
structlog.configure(
265281
processors=processors,
266-
wrapper_class=structlog.make_filtering_bound_logger(numeric_level),
282+
wrapper_class=wrapper_class,
267283
context_class=dict,
268284
logger_factory=structlog.PrintLoggerFactory(file=sys.stdout)
269285
if not log_file
270286
else structlog.stdlib.LoggerFactory(),
271-
cache_logger_on_first_use=True,
287+
cache_logger_on_first_use=cache if cache is not None else True,
272288
)
273289

274290
# Set up file logging if needed
@@ -366,4 +382,4 @@ def emit(self, record: logging.LogRecord) -> None:
366382
# Initialize logger - will be reconfigured when configure() is called
367383
# Set it to critical level
368384
logger: structlog.BoundLogger = structlog.get_logger()
369-
configure(log_level="CRITICAL", disable=True)
385+
configure(log_level="CRITICAL", cache=False)

0 commit comments

Comments
 (0)