logger#

This logger file provides easy configurability of the root and named loggers, along with a color print function for console output.

NOTE: If you are on Windows versions prior to Windows 10, the Command Prompt and PowerShell do not support ANSI color codes. You would need to use third-party libraries like colorama to enable ANSI color support. Please add the following colorama setting at the beginning of your code:

Installation:

pip install colorama

Initialization:

import colorama
colorama.init(autoreset=True)

Functions

get_current_script_and_line()

Get the function name, script name, and line of where the print is called.

get_logger([name, level, save_dir, ...])

Get or configure a logger, including both the root logger and named logger.

printc(text[, color])

Color enhanced print function with logger-like format.

get_logger(name: str | None = None, level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] = 'INFO', save_dir: str | None = None, filename: str | None = None, enable_console: bool = True, enable_file: bool = True) Logger[source]#

Get or configure a logger, including both the root logger and named logger.

Parameters:
  • name (Optional[str]) – Name of the logger. Defaults to None which means root logger.

  • level (str) – Log level. Defaults to “INFO”. Options: “DEBUG”, “INFO”, “WARNING”, “ERROR”, “CRITICAL”.

  • save_dir (Optional[str]) – Directory to save log files. Defaults to “./logs”.

  • filename (Optional[str]) – Name of the output log file. Defaults to “lib.log” for root logger and “{name}.log” for named logger.

  • enable_console (bool) – Control the console output. Defaults to True.

  • enable_file (bool) – Control the file output. Defaults to True.

Returns:

The logger with the specified configuration.

Return type:

logging.Logger

Example:

  1. Get the root logger and have it log to both console and file (~/.adalflow/logs/lib.log):

from adalflow.utils.logger import get_logger

root_logger = get_logger(enable_console=True, enable_file=True)
  1. Get a named logger and have it log to both console and file (./logs/app.log):

logger = get_logger(name="app", enable_console=True, enable_file=True)
  1. Use two loggers, one for the library and one for the application:

root_logger = enable_library_logging(level="DEBUG", enable_console=True, enable_file=True)
logger = get_logger(name="app", level="DEBUG", enable_console=True, enable_file=True)
logger.info("This is an application log, and it will be saved in app.log separately from the library log at lib.log.")
get_current_script_and_line() Tuple[str, str, int][source]#

Get the function name, script name, and line of where the print is called.

Returns:

the name of the function that is calling. script_name (str): the name of the script that is calling. line_number (str): the line number of the code that is calling.

Return type:

function_name (str)

printc(text: str, color: Literal['black', 'blue', 'cyan', 'green', 'magenta', 'red', 'white', 'yellow'] = 'cyan')[source]#

Color enhanced print function with logger-like format.

LightRAG’s customized print with colored text, position of code block the print is set, and current timestamp.

Parameters:
  • text (str) – Text to be printed.

  • color (str) – Color of the text. Defaults to “cyan”. Options:

  • 'black'

  • 'blue'

  • 'cyan'

  • 'green'

  • 'magenta'

  • 'red'

  • 'white'

  • "cyan". ('yellow'. Defaults to)

Example

from utils.logger import colored_print
printc("hello", color="green")
printc("hello")