-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move logging logic to its own module (#910)
* move logging logic to its own module * add log-setting functions, documentation
- Loading branch information
1 parent
d0afbb9
commit f4ed272
Showing
7 changed files
with
88 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Using PreTeXt as a library | ||
|
||
The PreTeXt-CLI includes `Project` and `Target` classes which can be used when this package is imported as a library. | ||
|
||
More details are coming soon. | ||
|
||
## Logging | ||
|
||
This package uses python's logging library and implements a logger with name `"ptxlogger"`. To get the same messages as the CLI gives (with default level of `INFO`), you can include the following. | ||
|
||
```python | ||
import logging | ||
from pretext import logger | ||
|
||
log = logging.getLogger("ptxlogger") | ||
logger.add_log_stream_handler() | ||
log.setLevel(logging.INFO) | ||
``` | ||
|
||
The `logger.add_log_stream_handler()` function simply creates a stream-handler that outputs to stdout. You could set up `log` however you like using the options provided by the `logging` library. If you would like to get spit out the logs to a file as the CLI does, you could include the line | ||
|
||
```python | ||
logger.add_log_file_handler(path_to_log_directory) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import datetime | ||
from pathlib import Path | ||
import sys | ||
import logging | ||
import click_log | ||
|
||
log = logging.getLogger("ptxlogger") | ||
|
||
|
||
def add_log_stream_handler() -> None: | ||
# Set up logging: | ||
# click_handler logs all messages to stdout as the CLI runs | ||
click_handler = logging.StreamHandler(sys.stdout) | ||
click_handler.setFormatter(click_log.ColorFormatter()) | ||
log.addHandler(click_handler) | ||
|
||
|
||
def get_log_error_flush_handler() -> logging.handlers.MemoryHandler: | ||
# error_flush_handler captures error/critical logs for flushing to stderr at the end of a CLI run | ||
sh = logging.StreamHandler(sys.stderr) | ||
sh.setFormatter(click_log.ColorFormatter()) | ||
sh.setLevel(logging.ERROR) | ||
error_flush_handler = logging.handlers.MemoryHandler( | ||
capacity=1024 * 100, | ||
flushLevel=100, | ||
target=sh, | ||
flushOnClose=False, | ||
) | ||
error_flush_handler.setLevel(logging.ERROR) | ||
log.addHandler(error_flush_handler) | ||
return error_flush_handler | ||
|
||
|
||
def add_log_file_handler(log_folder_path: Path) -> None: | ||
# create file handler which logs even debug messages | ||
log_folder_path.mkdir(exist_ok=True) | ||
logfile = ( | ||
log_folder_path / f"{datetime.datetime.now().strftime('%Y%m%d-%H%M%S')}.log" | ||
) | ||
fh = logging.FileHandler(logfile, mode="w") | ||
fh.setLevel(logging.DEBUG) | ||
file_log_format = logging.Formatter("{levelname:<8}: {message}", style="{") | ||
fh.setFormatter(file_log_format) | ||
log.addHandler(fh) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters