Skip to content

Get logger

get_logger(name)

Get a logger object with the given name

Parameters:

Name Type Description Default
name str

Name of the logger

required

Returns:

Type Description
Logger

logging.Logger: Logger object

Source code in Docs2KG/utils/get_logger.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def get_logger(name) -> logging.Logger:
    """
    Get a logger object with the given name

    Args:
        name (str): Name of the logger

    Returns:
        logging.Logger: Logger object

    """
    # Create a logger
    the_logger = logging.getLogger(name)
    the_logger.setLevel(logging.INFO)

    # Create console handler and set level to debug
    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setLevel(logging.DEBUG)

    # Create formatter, start with file name and line of code (line number) that issued the log statement
    formatter = logging.Formatter(
        "%(asctime)s|%(filename)s|Line: %(lineno)d -- %(name)s - %(levelname)s - %(message)s"
    )

    # Add formatter to console handler
    console_handler.setFormatter(formatter)

    # Add console handler to the_logger
    the_logger.addHandler(console_handler)
    return the_logger