Skip to content

Commit

Permalink
log messages now identify the task instance doing the logging (#107)
Browse files Browse the repository at this point in the history
* logging now identifies the task doing the logging
  • Loading branch information
Phil Varner authored Apr 19, 2024
1 parent e91a947 commit 5939ff3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
because the intention of the STAC Processing Extension is to add metadata about the
processing of the data, whereas stactask is frequently used only for processing
metadata. Users wishing to retain this field can call the method `Task.add_software_version_to_item(item)` on the resulting item to add it.
- Task logging now identifies the task instance that is logging, e.g.,
`INFO:my-task-name:[my-collection/workflow-my-workflow/task-1] Task did a thing.`

## Deprecated

Expand Down
16 changes: 16 additions & 0 deletions stactask/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations

import logging
from typing import Any, Optional


class TaskLoggerAdapter(logging.LoggerAdapter): # type: ignore
def __init__(self, logger: logging.Logger, prefix: Optional[str]) -> None:
super().__init__(logger, {})
self.prefix = prefix

def process(self, msg: str, kwargs: Any) -> tuple[str, Any]:
if self.prefix is not None:
return f"[{self.prefix}] {msg}", kwargs
else:
return msg, kwargs
7 changes: 6 additions & 1 deletion stactask/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
upload_item_assets_to_s3,
)
from .exceptions import FailedValidation
from .logging import TaskLoggerAdapter
from .utils import stac_jsonpath_match

# types
Expand Down Expand Up @@ -74,7 +75,6 @@ def __init__(
upload: bool = True,
validate: bool = True,
):
self.logger = logging.getLogger(self.name)

if not skip_validation and validate:
if not self.validate(payload):
Expand All @@ -100,6 +100,11 @@ def __init__(
# if a workdir was specified we don't want to rm by default
self._save_workdir = save_workdir if save_workdir is not None else True

self.logger = TaskLoggerAdapter(
logging.getLogger(self.name),
self._payload.get("id"),
)

@property
def process_definition(self) -> Dict[str, Any]:
process = self._payload.get("process", {})
Expand Down

0 comments on commit 5939ff3

Please sign in to comment.