chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

Chainlit sets logging format before application code

Open hughdbrown opened this issue 1 year ago • 1 comments

Describe the bug chainlit sets up the logging for the application it is in. A library should not do this.

To Reproduce If you import chainlit first:

>>> import chainlit
>>> import logging
>>>
>>> logging_args = {
...     "format": "%(asctime)s %(levelname)s %(name)s %(filename)s %(funcName)s %(lineno)d %(message)s",
...     "level": logging.INFO,
...     "datefmt": "%Y-%m-%d %H:%M:%S",
...     "encoding": "utf-8",
... }
>>> logging.basicConfig(**logging_args)
>>> logger = logging.getLogger(__name__)
>>>
>>> logger.info("sadasd")
2025-01-10 15:14:09 - sadasd
>>> def x():
...     logger.info("asasd")
...
>>> x()
2025-01-10 15:14:48 - asasd

Expected behavior The logging format should be what the application sets.

f you import and setup logging first then import chainlit:

>>> import logging
>>>
>>> logging_args = {
...     "format": "%(asctime)s %(levelname)s %(name)s %(filename)s %(funcName)s %(lineno)d %(message)s",
...     "level": logging.INFO,
...     "datefmt": "%Y-%m-%d %H:%M:%S",
...     "encoding": "utf-8",
... }
>>> logging.basicConfig(**logging_args)
>>> logger = logging.getLogger(__name__)
>>>
>>> logger.info("foo!")
2025-01-10 15:12:32 INFO __main__ <stdin> <module> 1 foo!
>>> def x():
...     logger.info("bar!")
...
>>> x()
2025-01-10 15:12:54 INFO __main__ <stdin> x 2 bar!
>>> import chainlit as cl
>>> x()
2025-01-10 15:13:13 INFO __main__ <stdin> x 2 bar!
>>> import openai
>>> x()
2025-01-10 15:13:22 INFO __main__ <stdin> x 2 bar!

Additional context When running chainlit as a module, there is currently nothing you can do to stop chainlit from setting the logging format before the application code is called.

hughdbrown avatar Jan 10 '25 22:01 hughdbrown

Has there been a workaround for this bug? I am currently running chainlit by mounting it to a FastAPI server, but I don't see any logs whether I import logging before/after chainlit.

sashank3 avatar Apr 22 '25 03:04 sashank3

This issue is stale because it has been open for 14 days with no activity.

github-actions[bot] avatar Jul 25 '25 23:07 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Aug 02 '25 02:08 github-actions[bot]

Obviously due to https://github.com/Chainlit/chainlit/blob/1aa9ad1b218acf3617dabdbaa1919a0809d01a66/backend/chainlit/logger.py#L4

Please fix this break if you mount chainlit to a fastapi app. Use may want their custom logging

goatwu1993 avatar Nov 21 '25 07:11 goatwu1993

/unstale

goatwu1993 avatar Nov 21 '25 07:11 goatwu1993