Skip to content

Commit

Permalink
Simplify interface (#6)
Browse files Browse the repository at this point in the history
Remove the not-so-useful StructLog class in favor of a simpler builder function
Remove useless __call__
  • Loading branch information
multani authored May 13, 2023
1 parent e1e9c9d commit a5cb1d3
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 39 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ provides:
import structlog
import structlog_gcp

gcp_logs = structlog_gcp.StructlogGCP()

structlog.configure(processors=gcp_logs.build_processors())
processors = structlog_gcp.build_processors()
structlog.configure(processors=processors)
```

Then, you can use `structlog` as usual:
Expand Down
4 changes: 2 additions & 2 deletions examples/cloud-function/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import structlog_gcp

gcp_logs = structlog_gcp.StructlogGCP()
structlog.configure(processors=gcp_logs.build_processors())
processors = structlog_gcp.build_processors()
structlog.configure(processors=processors)


@functions_framework.http
Expand Down
6 changes: 5 additions & 1 deletion structlog_gcp/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from .base import StructlogGCP # noqa: F401
from .base import build_processors # noqa: F401

__all__ = [
"build_processors",
]
24 changes: 10 additions & 14 deletions structlog_gcp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
from . import errors, processors


class StructlogGCP:
def __init__(self) -> None:
pass
def build_processors() -> list[Processor]:
procs = []

def build_processors(self) -> list[Processor]:
procs = []
procs.extend(processors.CoreCloudLogging().setup())
procs.extend(processors.LogSeverity().setup())
procs.extend(processors.CodeLocation().setup())
procs.extend(errors.ReportException().setup())
procs.extend(errors.ReportError(["CRITICAL"]).setup())
procs.append(errors.add_service_context)
procs.extend(processors.FormatAsCloudLogging().setup())

procs.extend(processors.CoreCloudLogging().setup())
procs.extend(processors.LogSeverity().setup())
procs.extend(processors.CodeLocation().setup())
procs.extend(errors.ReportException().setup())
procs.extend(errors.ReportError(["CRITICAL"]).setup())
procs.append(errors.add_service_context)
procs.extend(processors.FormatAsCloudLogging().setup())

return procs
return procs
8 changes: 2 additions & 6 deletions structlog_gcp/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ def __init__(self, log_level: str = "CRITICAL") -> None:
self.log_level = log_level

def setup(self) -> list[Processor]:
return [
# structlog.processors.ExceptionRenderer(self.format_exception),
structlog.processors.format_exc_info,
self.__call__,
]
return [structlog.processors.format_exc_info, self]

def __call__(
self, logger: WrappedLogger, method_name: str, event_dict: EventDict
Expand Down Expand Up @@ -77,7 +73,7 @@ def __init__(self, severities: list[str]) -> None:
self.severities = severities

def setup(self) -> list[Processor]:
return [self.__call__]
return [self]

def _build_service_context(self) -> dict[str, str]:
# https://cloud.google.com/error-reporting/reference/rest/v1beta1/ServiceContext
Expand Down
16 changes: 5 additions & 11 deletions structlog_gcp/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def setup(self) -> list[Processor]:
structlog.processors.UnicodeDecoder(),
# Add a timestamp in ISO 8601 format.
structlog.processors.TimeStamper(fmt="iso"),
self.__call__,
self,
]

def __call__(
Expand All @@ -37,10 +37,7 @@ class FormatAsCloudLogging:
"""Finalize the Google Cloud Logging event message and replace the logging event"""

def setup(self) -> list[Processor]:
return [
self.__call__,
structlog.processors.JSONRenderer(),
]
return [self, structlog.processors.JSONRenderer()]

def __call__(
self, logger: WrappedLogger, method_name: str, event_dict: EventDict
Expand Down Expand Up @@ -74,11 +71,8 @@ def __init__(self) -> None:
}

def setup(self) -> list[Processor]:
return [
# Add log level to event dict.
structlog.processors.add_log_level,
self.__call__,
]
# Add log level to event dict.
return [structlog.processors.add_log_level, self]

def __call__(
self, logger: WrappedLogger, method_name: str, event_dict: EventDict
Expand Down Expand Up @@ -108,7 +102,7 @@ def setup(self) -> list[Processor]:
structlog.processors.CallsiteParameter.LINENO,
]
)
return [call_site_proc, self.__call__]
return [call_site_proc, self]

def __call__(
self, logger: WrappedLogger, method_name: str, event_dict: EventDict
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def logger():
"structlog.processors.format_exc_info", side_effect=fakes.format_exc_info
),
):
gcp_logs = structlog_gcp.StructlogGCP()
structlog.configure(processors=gcp_logs.build_processors())
processors = structlog_gcp.build_processors()
structlog.configure(processors=processors)
logger = structlog.get_logger()
yield logger

Expand Down

0 comments on commit a5cb1d3

Please sign in to comment.