loggi.logger

 1import logging
 2
 3from pathier import Pathier, Pathish
 4
 5from loggi import models
 6
 7root = Pathier(__file__).parent
 8
 9
10def getLogger(name: str, path: Pathish = Pathier.cwd()) -> logging.Logger:
11    """Get a configured `logging.Logger` instance for `name` with a file handler.
12
13    The log file will be located in `path`.
14
15    Default level is `INFO`.
16
17    Logs are in the format: `{levelname}|-|{asctime}|-|{message}
18
19    asctime is formatted as `%x %X`"""
20    path = Pathier(path)
21    logger = logging.getLogger(name)
22    # TODO: Add option for a stream handler
23    handler = logging.FileHandler((path / name).with_suffix(".log"), encoding="utf-8")
24    handler.setFormatter(
25        logging.Formatter(
26            "{levelname}|-|{asctime}|-|{message}",
27            style="{",
28            datefmt="%x %X",
29        )
30    )
31    if handler.baseFilename not in [
32        existing_handler.baseFilename
33        for existing_handler in logger.handlers
34        if isinstance(existing_handler, logging.FileHandler)
35    ]:
36        logger.addHandler(handler)
37    logger.setLevel(logging.INFO)
38    return logger
39
40
41def load_log(logpath: Pathish) -> models.Log:
42    """Return a `Log` object for the log file at `logpath`."""
43    return models.Log.load_log(Pathier(logpath))
def getLogger( name: str, path: pathier.pathier.Pathier | pathlib.Path | str = WindowsPath('E:/1vsCode/python/loggi')) -> logging.Logger:
11def getLogger(name: str, path: Pathish = Pathier.cwd()) -> logging.Logger:
12    """Get a configured `logging.Logger` instance for `name` with a file handler.
13
14    The log file will be located in `path`.
15
16    Default level is `INFO`.
17
18    Logs are in the format: `{levelname}|-|{asctime}|-|{message}
19
20    asctime is formatted as `%x %X`"""
21    path = Pathier(path)
22    logger = logging.getLogger(name)
23    # TODO: Add option for a stream handler
24    handler = logging.FileHandler((path / name).with_suffix(".log"), encoding="utf-8")
25    handler.setFormatter(
26        logging.Formatter(
27            "{levelname}|-|{asctime}|-|{message}",
28            style="{",
29            datefmt="%x %X",
30        )
31    )
32    if handler.baseFilename not in [
33        existing_handler.baseFilename
34        for existing_handler in logger.handlers
35        if isinstance(existing_handler, logging.FileHandler)
36    ]:
37        logger.addHandler(handler)
38    logger.setLevel(logging.INFO)
39    return logger

Get a configured logging.Logger instance for name with a file handler.

The log file will be located in path.

Default level is INFO.

Logs are in the format: `{levelname}|-|{asctime}|-|{message}

asctime is formatted as %x %X

def load_log( logpath: pathier.pathier.Pathier | pathlib.Path | str) -> loggi.models.Log:
42def load_log(logpath: Pathish) -> models.Log:
43    """Return a `Log` object for the log file at `logpath`."""
44    return models.Log.load_log(Pathier(logpath))

Return a Log object for the log file at logpath.