File logging
LangChain provides the FileCallbackHandler
to write logs to a file. The FileCallbackHandler
is similar to the StdOutCallbackHandler
, but instead of printing logs to standard output it writes logs to a file.
We see how to use the FileCallbackHandler
in this example. Additionally we use the StdOutCallbackHandler
to print logs to the standard output. It also uses the loguru
library to log other outputs that are not captured by the handler.
from langchain_core.callbacks import FileCallbackHandler, StdOutCallbackHandler
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
from loguru import logger
logfile = "output.log"
logger.add(logfile, colorize=True, enqueue=True)
handler_1 = FileCallbackHandler(logfile)
handler_2 = StdOutCallbackHandler()
prompt = PromptTemplate.from_template("1 + {number} = ")
model = OpenAI()
# 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 = prompt | model
response = chain.invoke({"number": 2}, {"callbacks": [handler_1, handler_2]})
logger.info(response)
[1m> Entering new LLMChain chain...[0m
Prompt after formatting:
[32;1m[1;3m1 + 2 = [0m
``````output
[32m2023-06-01 18:36:38.929[0m | [1mINFO [0m | [36m__main__[0m:[36m<module>[0m:[36m20[0m - [1m
3[0m
``````output
[1m> Finished chain.[0m
Now we can open the file output.log
to see that the output has been captured.
%pip install --upgrade --quiet ansi2html > /dev/null
from ansi2html import Ansi2HTMLConverter
from IPython.display import HTML, display
with open("output.log", "r") as f:
content = f.read()
conv = Ansi2HTMLConverter()
html = conv.convert(content, full=True)
display(HTML(html))
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<style type="text/css">
.ansi2html-content { display: inline; white-space: pre-wrap; word-wrap: break-word; }
.body_foreground { color: #AAAAAA; }
.body_background { background-color: #000000; }
.inv_foreground { color: #000000; }
.inv_background { background-color: #AAAAAA; }
.ansi1 { font-weight: bold; }
.ansi3 { font-style: italic; }
.ansi32 { color: #00aa00; }
.ansi36 { color: #00aaaa; }
</style>
</head>
<body class="body_foreground body_background" style="font-size: normal;" >
<pre class="ansi2html-content">
<span class="ansi1">> Entering new LLMChain chain...</span>
Prompt after formatting:
<span class="ansi1 ansi32"></span><span class="ansi1 ansi3 ansi32">1 + 2 = </span>
<span class="ansi1">> Finished chain.</span>
<span class="ansi32">2023-06-01 18:36:38.929</span> | <span class="ansi1">INFO </span> | <span class="ansi36">__main__</span>:<span class="ansi36"><module></span>:<span class="ansi36">20</span> - <span class="ansi1">
3</span>
</pre>
</body>
</html>