Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksAsync callbacksCustom callback handlersCallbacks for custom chainsLogging to fileMultiple callback handlersTagsToken countingModulesSecurityGuidesMoreModulesCallbacksLogging to fileLogging to fileThis example shows how to print logs to file. It shows how to use the FileCallbackHandler, which does the same thing as StdOutCallbackHandler, but instead writes the output to file. It also uses the loguru library to log other outputs that are not captured by the handler.from loguru import loggerfrom langchain.callbacks import FileCallbackHandlerfrom langchain.chains import LLMChainfrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatelogfile = "output.log"logger.add(logfile, colorize=True, enqueue=True)handler = FileCallbackHandler(logfile)llm = OpenAI()prompt = PromptTemplate.from_template("1 + {number} = ")# this chain will both print to stdout (because verbose=True) and write to 'output.log'# if verbose=False, the FileCallbackHandler will still write to 'output.log'chain = LLMChain(llm=llm, prompt=prompt, callbacks=[handler], verbose=True)answer = chain.run(number=2)logger.info(answer)            > Entering new LLMChain chain...    Prompt after formatting:    1 + 2 =     [32m2023-06-01 18:36:38.929[0m | [1mINFO    [0m | [36m__main__[0m:[36m<module>[0m:[36m20[0m - [1m        3[0m        > Finished chain.Now we can open the file output.log to see that the output has been captured.pip install ansi2html > /dev/nullfrom IPython.display import display, HTMLfrom ansi2html import Ansi2HTMLConverterwith open("output.log", "r") as f:    content = f.read()conv = Ansi2HTMLConverter()html = conv.convert(content,
This example shows how to print logs to file. It shows how to use the FileCallbackHandler, which does the same thing as StdOutCallbackHandler, but instead writes the output to file. It also uses the loguru library to log other outputs that are not captured by the handler.
This example shows how to print logs to file. It shows how to use the FileCallbackHandler, which does the same thing as StdOutCallbackHandler, but instead writes the output to file. It also uses the loguru library to log other outputs that are not captured by the handler. ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksAsync callbacksCustom callback handlersCallbacks for custom chainsLogging to fileMultiple callback handlersTagsToken countingModulesSecurityGuidesMoreModulesCallbacksLogging to fileLogging to fileThis example shows how to print logs to file. It shows how to use the FileCallbackHandler, which does the same thing as StdOutCallbackHandler, but instead writes the output to file. It also uses the loguru library to log other outputs that are not captured by the handler.from loguru import loggerfrom langchain.callbacks import FileCallbackHandlerfrom langchain.chains import LLMChainfrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatelogfile = "output.log"logger.add(logfile, colorize=True, enqueue=True)handler = FileCallbackHandler(logfile)llm = OpenAI()prompt = PromptTemplate.from_template("1 + {number} = ")# this chain will both print to stdout (because verbose=True) and write to 'output.log'# if verbose=False, the FileCallbackHandler will still write to 'output.log'chain = LLMChain(llm=llm, prompt=prompt, callbacks=[handler], verbose=True)answer = chain.run(number=2)logger.info(answer)            > Entering new LLMChain chain...    Prompt after formatting:    1 + 2 =     [32m2023-06-01 18:36:38.929[0m | [1mINFO    [0m | [36m__main__[0m:[36m<module>[0m:[36m20[0m - [1m        3[0m        > Finished chain.Now we can open the file output.log to see that the output has been captured.pip install ansi2html > /dev/nullfrom IPython.display import display, HTMLfrom ansi2html import Ansi2HTMLConverterwith open("output.log", "r") as f:    content = f.read()conv = Ansi2HTMLConverter()html = conv.convert(content,