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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move logger plugin to client monorepo #274

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ profile_default/
ipython_config.py

# pyenv
.python-version
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10
219 changes: 115 additions & 104 deletions packages/config-bundles/polywrap-sys-config-bundle/poetry.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from polywrap_core import Uri
from polywrap_fs_plugin import file_system_plugin
from polywrap_http_plugin import http_plugin
from polywrap_logger_plugin import logger_plugin
from polywrap_uri_resolvers import ExtendableUriResolver

from .embeds import get_embedded_wrap
Expand Down Expand Up @@ -87,6 +88,12 @@
Uri.from_str("wrapscan.io/polywrap/[email protected]")
],
),
"logger": BundlePackage(
uri=Uri.from_str("plugin/[email protected]"),
package=logger_plugin(),
implements=[Uri.from_str("wrapscan.io/polywrap/[email protected]")],
redirects_from=[Uri.from_str("wrapscan.io/polywrap/[email protected]")],
),
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ polywrap-manifest = {path = "../../polywrap-manifest", develop = true}
polywrap-wasm = {path = "../../polywrap-wasm", develop = true}
polywrap-fs-plugin = {path = "../../plugins/polywrap-fs-plugin", develop = true}
polywrap-http-plugin = {path = "../../plugins/polywrap-http-plugin", develop = true}
polywrap-logger-plugin = {path = "../../plugins/polywrap-logger-plugin", develop = true}

[tool.poetry.group.dev.dependencies]
polywrap-client = {path = "../../polywrap-client", develop = true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from polywrap_client import PolywrapClient
from polywrap_sys_config_bundle import sys_bundle


def test_http_plugin():
config = PolywrapClientConfigBuilder().add_bundle(sys_bundle).build()
client = PolywrapClient(config)
Expand All @@ -18,6 +17,17 @@ def test_http_plugin():
assert response["status"] == 200
assert response["body"] is not None

def test_logger_plugin():
config = PolywrapClientConfigBuilder().add_bundle(sys_bundle).build()
client = PolywrapClient(config)

response = client.invoke(
uri=Uri.from_str("wrapscan.io/polywrap/[email protected]"),
method="log",
args={"level": 1, "message": "Hello Polywrap!"},
)

assert response is True

def test_file_system_resolver():
config = PolywrapClientConfigBuilder().add_bundle(sys_bundle).build()
Expand Down
8 changes: 8 additions & 0 deletions packages/plugins/polywrap-logger-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__pycache__
.idea
.vscode
dist
**/.pytest_cache/
**/node_modules/
**/.tox/
wrap/
82 changes: 82 additions & 0 deletions packages/plugins/polywrap-logger-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Logger Python Plugin
The Logger plugin implements the `logger-interface` from the `ens/wraps.eth:[email protected]` package (see [./src/schema.graphql](./src/schema.graphql)). By default, it logs all events using the Python `logging` module. You can customize this behavior by setting the `Logger` property on the plugin's configuration object (examples below).

## Usage
### 1. Configure Client
When creating your Polywrap Python client, add the logger plugin:
```python
from polywrap_client_config_builder import PolywrapClientConfigBuilder
from polywrap_logger_plugin import logger_plugin
from polywrap_client import PolywrapClient

config = PolywrapClientConfigBuilder().set_package(
uri=Uri.from_str("plugin/logger"),
package=logger_plugin()
).set_interface(
interface=Uri.from_str("ens/wraps.eth:[email protected]"),
implementations=[Uri.from_str("plugin/logger")]
).set_redirect(
Uri.from_str("ens/wraps.eth:[email protected]"),
Uri.from_str("plugin/logger")
).build()
client = PolywrapClient(config)
```

### 2. Invoke The Logger
Invocations to the logger plugin can be made via the interface URI (which will get redirected), or the plugin's URI directly:
```python
await client.invoke({
'uri': 'ens/wraps.eth:[email protected]' | 'plugin/logger',
'method': 'log',
'args': {
'level': 'INFO',
'message': 'foo bar baz'
}
})
```

### 3. Customize The Logger
When adding the logger to your client, you can add your own custom log function:
```python
config = PolywrapClientConfigBuilder().set_package(
uri=Uri.from_str("plugin/logger"),
package=logger_plugin(LoggerConfig(logger=YourLogger(), level=LogLevel.INFO))
).set_interface(
interface=Uri.from_str("ens/wraps.eth:[email protected]"),
implementations=[Uri.from_str("plugin/logger")]
).set_redirect(
Uri.from_str("ens/wraps.eth:[email protected]"),
Uri.from_str("plugin/logger")
).build()
```

### 4. Add Multiple Loggers
Multiple logger implementations can be added to the client:
```python
config = PolywrapClientConfigBuilder().set_package(
uri=Uri.from_str("plugin/logger"),
package=logger_plugin(LoggerConfig(logger=YourLogger(), level=LogLevel.INFO))
).set_interface(
interface=Uri.from_str("ens/wraps.eth:[email protected]"),
implementations=[Uri.from_str("plugin/logger"), Uri.from_str("plugin/custom-logger")]
).set_package(
uri=Uri.from_str("plugin/custom-logger"),
package=custom_logger_plugin()
).build()
```

### 5. Invoke All Logger Implementations
When you'd like to log something to more than one logger, you can invoke all implementations of the logger interface:
```python
implementations = client.get_implementations('ens/wraps.eth:[email protected]')

for impl in implementations:
await client.invoke({
'uri': impl,
'method': 'log',
'args': {
'level': 'INFO',
'message': 'message'
}
})
```
12 changes: 12 additions & 0 deletions packages/plugins/polywrap-logger-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@polywrap/python-logger-plugin",
"description": "Polywrap Python Logger Plugin",
"version": "0.10.1",
"private": true,
"devDependencies": {
"polywrap": "0.10.2"
},
"scripts": {
"codegen": "npx polywrap codegen"
}
}
Loading