Skip to content

Commit

Permalink
Merge pull request #2340 from PrefectHQ/better-logging-config
Browse files Browse the repository at this point in the history
Better logging config
  • Loading branch information
joshmeek committed Apr 16, 2020
2 parents 069be3b + e530735 commit a6f0eb1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
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

0 comments on commit a6f0eb1

Please sign in to comment.