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` at `path/{name}.log`.
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    path.mkdir()
22    logger = logging.getLogger(name)
23    # TODO: Add option for a stream handler
24    logpath = path / f"{name}.log"
25    handler = logging.FileHandler(logpath, encoding="utf-8")
26    if handler.baseFilename not in [
27        existing_handler.baseFilename
28        for existing_handler in logger.handlers
29        if isinstance(existing_handler, logging.FileHandler)
30    ]:
31        handler.setFormatter(
32            logging.Formatter(
33                "{levelname}|-|{asctime}|-|{message}",
34                style="{",
35                datefmt="%x %X",
36            )
37        )
38        logger.addHandler(handler)
39    logger.setLevel(logging.INFO)
40    return logger
41
42
43def load_log(logpath: Pathish) -> models.Log:
44    """Return a `Log` object for the log file at `logpath`."""
45    return models.Log.load_log(Pathier(logpath))
46
47
48def get_logpaths(logger: logging.Logger) -> list[Pathier]:
49    """Loop through the handlers for `logger` and return a list of paths for any handler of type `FileHandler`."""
50    return [
51        Pathier(handler.baseFilename)
52        for handler in logger.handlers
53        if isinstance(handler, logging.FileHandler)
54    ]
55
56
57def get_logpath(logger: logging.Logger) -> Pathier | None:
58    """Search `logger.handlers` for a `FileHandler` that has a file stem matching `logger.name`.
59
60    Returns `None` if not found."""
61    for path in get_logpaths(logger):
62        if path.stem == logger.name:
63            return path
64
65
66def get_log(logger: logging.Logger) -> models.Log | None:
67    """Find the corresponding log file for `logger`, load it into a `models.Log` instance, and then return it.
68
69    Returns `None` if a log file can't be found."""
70    path = get_logpath(logger)
71    if path:
72        return load_log(path)
73
74
75def close(logger: logging.Logger):
76    """Removes and closes handlers for `logger`."""
77    for handler in logger.handlers:
78        logger.removeHandler(handler)
79        handler.close()
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` at `path/{name}.log`.
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    path.mkdir()
23    logger = logging.getLogger(name)
24    # TODO: Add option for a stream handler
25    logpath = path / f"{name}.log"
26    handler = logging.FileHandler(logpath, encoding="utf-8")
27    if handler.baseFilename not in [
28        existing_handler.baseFilename
29        for existing_handler in logger.handlers
30        if isinstance(existing_handler, logging.FileHandler)
31    ]:
32        handler.setFormatter(
33            logging.Formatter(
34                "{levelname}|-|{asctime}|-|{message}",
35                style="{",
36                datefmt="%x %X",
37            )
38        )
39        logger.addHandler(handler)
40    logger.setLevel(logging.INFO)
41    return logger

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

The log file will be located in path at path/{name}.log.

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:
44def load_log(logpath: Pathish) -> models.Log:
45    """Return a `Log` object for the log file at `logpath`."""
46    return models.Log.load_log(Pathier(logpath))

Return a Log object for the log file at logpath.

def get_logpaths(logger: logging.Logger) -> list[pathier.pathier.Pathier]:
49def get_logpaths(logger: logging.Logger) -> list[Pathier]:
50    """Loop through the handlers for `logger` and return a list of paths for any handler of type `FileHandler`."""
51    return [
52        Pathier(handler.baseFilename)
53        for handler in logger.handlers
54        if isinstance(handler, logging.FileHandler)
55    ]

Loop through the handlers for logger and return a list of paths for any handler of type FileHandler.

def get_logpath(logger: logging.Logger) -> pathier.pathier.Pathier | None:
58def get_logpath(logger: logging.Logger) -> Pathier | None:
59    """Search `logger.handlers` for a `FileHandler` that has a file stem matching `logger.name`.
60
61    Returns `None` if not found."""
62    for path in get_logpaths(logger):
63        if path.stem == logger.name:
64            return path

Search logger.handlers for a FileHandler that has a file stem matching logger.name.

Returns None if not found.

def get_log(logger: logging.Logger) -> loggi.models.Log | None:
67def get_log(logger: logging.Logger) -> models.Log | None:
68    """Find the corresponding log file for `logger`, load it into a `models.Log` instance, and then return it.
69
70    Returns `None` if a log file can't be found."""
71    path = get_logpath(logger)
72    if path:
73        return load_log(path)

Find the corresponding log file for logger, load it into a models.Log instance, and then return it.

Returns None if a log file can't be found.

def close(logger: logging.Logger):
76def close(logger: logging.Logger):
77    """Removes and closes handlers for `logger`."""
78    for handler in logger.handlers:
79        logger.removeHandler(handler)
80        handler.close()

Removes and closes handlers for logger.