forked from Materials-Consortia/optimade-python-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce logging (Materials-Consortia#432)
* Add OPTIMADE logger: The logger can be imported from optimade.server.logger as LOGGER. It will write out logs at DEBUG level to a file `optimade.log` at the OPTIMADE_LOG_DIR environment variable, default is /var/log/optimade/, rotating between 5 files when reaching 1 MB. The validator logger now get it's logger as a Child logger from this. `run.sh` has been updated to set the new `OPTIMADE_LOG_LEVEL` configuration option, since this is the only way we can get the logging level to the terminal/console handler, while avoiding circular imports. If `OPTIMADE_DEBUG` is set to "true", the `OPTIMADE_LOG_LEVEL` will be overwritten to "DEBUG". * Convert various print-statements to log-statements: Keeping some print-statements still, mainly in tests, where it's nicer to print than log in order to not unneccesarily pollute the logs. * Add `log_dir` configuration option: The default for `log_dir` is `/var/log/optimade/`. If a `PermissionError` is raised during setting up the logging folder, a warning will be logged (to the console), which will include instructions on how to deal with the issue.
- Loading branch information
Showing
15 changed files
with
368 additions
and
246 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,3 +64,4 @@ venv.bak/ | |
# Package-specific | ||
local_openapi.json | ||
local_index_openapi.json | ||
logs |
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,4 @@ | ||
"""Logger for optimade.adapters""" | ||
import logging | ||
|
||
LOGGER = logging.getLogger("optimade").getChild("adapters") |
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
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,67 @@ | ||
"""Logging to both file and terminal""" | ||
import logging | ||
import os | ||
from pathlib import Path | ||
import sys | ||
|
||
from uvicorn.logging import DefaultFormatter | ||
|
||
|
||
# Instantiate LOGGER | ||
LOGGER = logging.getLogger("optimade") | ||
LOGGER.setLevel(logging.DEBUG) | ||
|
||
# Handler | ||
CONSOLE_HANDLER = logging.StreamHandler(sys.stdout) | ||
try: | ||
from optimade.server.config import CONFIG | ||
|
||
CONSOLE_HANDLER.setLevel(CONFIG.log_level.value.upper()) | ||
except ImportError: | ||
CONSOLE_HANDLER.setLevel(os.getenv("OPTIMADE_LOG_LEVEL", "INFO").upper()) | ||
|
||
# Formatter | ||
CONSOLE_FORMATTER = DefaultFormatter("%(levelprefix)s [%(name)s] %(message)s") | ||
CONSOLE_HANDLER.setFormatter(CONSOLE_FORMATTER) | ||
|
||
# Add handler to LOGGER | ||
LOGGER.addHandler(CONSOLE_HANDLER) | ||
|
||
# Save a file with all messages (DEBUG level) | ||
try: | ||
from optimade.server.config import CONFIG | ||
|
||
LOGS_DIR = CONFIG.log_dir | ||
except ImportError: | ||
LOGS_DIR = Path(os.getenv("OPTIMADE_LOG_DIR", "/var/log/optimade/")).resolve() | ||
|
||
try: | ||
LOGS_DIR.mkdir(exist_ok=True) | ||
except PermissionError: | ||
LOGGER.warning( | ||
"""Log files are not saved. | ||
This is usually due to not being able to access a specified log folder or write to files | ||
in the specified log location, i.e., a `PermissionError` has been raised. | ||
To solve this, either set the OPTIMADE_LOG_DIR environment variable to a location | ||
you have permission to write to or create the /var/log/optimade folder, which is | ||
the default logging folder, with write permissions for the Unix user running the server. | ||
""" | ||
) | ||
else: | ||
# Handlers | ||
FILE_HANDLER = logging.handlers.RotatingFileHandler( | ||
LOGS_DIR.joinpath("optimade.log"), maxBytes=1000000, backupCount=5 | ||
) | ||
FILE_HANDLER.setLevel(logging.DEBUG) | ||
|
||
# Formatter | ||
FILE_FORMATTER = logging.Formatter( | ||
"[%(levelname)-8s %(asctime)s %(filename)s:%(lineno)d][%(name)s] %(message)s", | ||
"%d-%m-%Y %H:%M:%S", | ||
) | ||
FILE_HANDLER.setFormatter(FILE_FORMATTER) | ||
|
||
# Add handler to LOGGER | ||
LOGGER.addHandler(FILE_HANDLER) |
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
Oops, something went wrong.