Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better logging config #2340

Merged
merged 2 commits into from Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ These changes are available in the [master branch](https://github.com/PrefectHQ/

- Allow GraphQL endpoint configuration via `config.toml` for remote deployments of the UI - [#2338](https://github.com/PrefectHQ/prefect/pull/2338)
- Add option to connect containers created by Docker agent to an existing Docker network - [#2334](https://github.com/PrefectHQ/prefect/pull/2334)
- Expose `datefmt` as a configurable logging option in Prefect configuration - [#2340](https://github.com/PrefectHQ/prefect/pull/2340)

### Task Library

Expand Down
3 changes: 3 additions & 0 deletions src/prefect/config.toml
Expand Up @@ -81,6 +81,9 @@ level = "INFO"
# The log format
format = "[%(asctime)s] %(levelname)s - %(name)s | %(message)s"

# the timestamp format
datefmt = "%Y-%m-%d %H:%M:%S"

# Send logs to Prefect Cloud
log_to_cloud = false

Expand Down
8 changes: 6 additions & 2 deletions src/prefect/utilities/logging.py
Expand Up @@ -40,7 +40,9 @@ def __init__(self) -> None:
self.client = None
self.logger = logging.getLogger("CloudHandler")
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(context.config.logging.format)
formatter = logging.Formatter(
context.config.logging.format, context.config.logging.datefmt
)
formatter.converter = time.gmtime # type: ignore
handler.setFormatter(formatter)
self.logger.addHandler(handler)
Expand Down Expand Up @@ -198,7 +200,9 @@ def _create_logger(name: str) -> logging.Logger:

logger = logging.getLogger(name)
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(context.config.logging.format)
formatter = logging.Formatter(
context.config.logging.format, context.config.logging.datefmt
)
formatter.converter = time.gmtime # type: ignore
handler.setFormatter(formatter)
logger.addHandler(handler)
Expand Down
15 changes: 15 additions & 0 deletions tests/utilities/test_logging.py
@@ -1,3 +1,4 @@
import datetime
import json
import logging
import time
Expand Down Expand Up @@ -26,6 +27,20 @@ def test_root_logger_level_responds_to_config():
logger.handlers = []


@pytest.mark.parametrize("datefmt", ["%Y", "%Y -- %D"])
def test_root_logger_datefmt_responds_to_config(caplog, datefmt):
try:
with utilities.configuration.set_temporary_config({"logging.datefmt": datefmt}):
logger = utilities.logging.configure_logging(testing=True)
logger.error("badness")
logs = [r for r in caplog.records if r.levelname == "ERROR"]
assert logs[0].asctime == datetime.datetime.utcnow().strftime(datefmt)
finally:
# reset root_logger
logger = utilities.logging.configure_logging(testing=True)
logger.handlers = []


def test_remote_handler_is_configured_for_cloud():
try:
with utilities.configuration.set_temporary_config(
Expand Down