This page contains information on creating and assigning a logger of an index, as well as on the implementation of a custom logger.
Use of standard file logger
In order to save information about events and errors in an index into a file, you should use the standard file logger or your own custom logger. It is important to remember that each time you open an existing index, you must create and assign an instance of the logger again, because the logger is not saved. The logger receives, among other things, the messages that the index reports while it is being opened, for example, about an interrupted indexing operation (see Checking index integrity). The same code can be used to create a new index and to open an existing one: when an existing index is opened, the logger is taken from the index settings passed to the constructor, and the other index settings are loaded from the index.
The following example demonstrates the use of the standard file logger. The constructor parameters of the FileLogger class are the path to the log file and its maximum size in MB. The maximum size should be in the range from 0.1 to 1000 MB; the value is not checked. When the log file exceeds the maximum size, the older part of the log is removed, and the most recent half of the maximum size is kept. The example prints the log file at the end.
importosfromgroupdocs.searchimportIndex,IndexSettingsfromgroupdocs.search.commonimportFileLoggerquery="Einstein"log_path="./output/log.txt"os.makedirs("./output",exist_ok=True)settings=IndexSettings()settings.logger=FileLogger(log_path,4.0)# Specifying the path to the log file and a maximum length of 4 MBindex=Index("./index",settings)# Creating or loading an index from the specified folderindex.add("./documents")# Indexing documents from the specified folderresult=index.search(query)# Search in indexwithopen(log_path,encoding="utf-8")asf:print(f.read())
Sometimes you may need to implement your own logger, for example, to display the log in the console. A custom logger implements the ILogger interface. In Python, it is a plain class with the methods of the interface; do not derive it from the ILogger class. The error method logs errors, and the trace method logs events. An example implementation of such a logger is presented below.
The custom logger is assigned in the same way as the standard one. A function that takes the level ("error" or "trace") and the message can be assigned as a logger too, for example settings.logger = lambda level, message: print(level, message).
fromgroupdocs.searchimportIndex,IndexSettingsclassConsoleLogger(object):deferror(self,message):print("Error: "+message)deftrace(self,message):print(message)settings=IndexSettings()settings.logger=ConsoleLogger()# Assigning the custom loggerindex=Index("./index",settings)# Creating or loading an index from the specified folder
Along with the full-featured library, we provide a free online document search app. You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our Free Online Document Search App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.