|
| 1 | +# Logger Python Plugin |
| 2 | +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). |
| 3 | + |
| 4 | +## Usage |
| 5 | +### 1. Configure Client |
| 6 | +When creating your Polywrap Python client, add the logger plugin: |
| 7 | +```python |
| 8 | +from polywrap_client_config_builder import PolywrapClientConfigBuilder |
| 9 | +from polywrap_logger_plugin import logger_plugin |
| 10 | +from polywrap_client import PolywrapClient |
| 11 | + |
| 12 | +config = PolywrapClientConfigBuilder().set_package( |
| 13 | + uri=Uri.from_str("plugin/logger"), |
| 14 | + package=logger_plugin() |
| 15 | +).set_interface( |
| 16 | + interface=Uri.from_str( "ens/wraps.eth:[email protected]"), |
| 17 | + implementations=[Uri.from_str("plugin/logger")] |
| 18 | +).set_redirect( |
| 19 | + Uri.from_str( "ens/wraps.eth:[email protected]"), |
| 20 | + Uri.from_str("plugin/logger") |
| 21 | +).build() |
| 22 | +client = PolywrapClient(config) |
| 23 | +``` |
| 24 | + |
| 25 | +### 2. Invoke The Logger |
| 26 | +Invocations to the logger plugin can be made via the interface URI (which will get redirected), or the plugin's URI directly: |
| 27 | +```python |
| 28 | +await client.invoke({ |
| 29 | + 'uri': 'ens/wraps.eth:[email protected]' | 'plugin/logger', |
| 30 | + 'method': 'log', |
| 31 | + 'args': { |
| 32 | + 'level': 'INFO', |
| 33 | + 'message': 'foo bar baz' |
| 34 | + } |
| 35 | +}) |
| 36 | +``` |
| 37 | + |
| 38 | +### 3. Customize The Logger |
| 39 | +When adding the logger to your client, you can add your own custom log function: |
| 40 | +```python |
| 41 | +config = PolywrapClientConfigBuilder().set_package( |
| 42 | + uri=Uri.from_str("plugin/logger"), |
| 43 | + package=logger_plugin(LoggerConfig(logger=YourLogger(), level=LogLevel.INFO)) |
| 44 | +).set_interface( |
| 45 | + interface=Uri.from_str( "ens/wraps.eth:[email protected]"), |
| 46 | + implementations=[Uri.from_str("plugin/logger")] |
| 47 | +).set_redirect( |
| 48 | + Uri.from_str( "ens/wraps.eth:[email protected]"), |
| 49 | + Uri.from_str("plugin/logger") |
| 50 | +).build() |
| 51 | +``` |
| 52 | + |
| 53 | +### 4. Add Multiple Loggers |
| 54 | +Multiple logger implementations can be added to the client: |
| 55 | +```python |
| 56 | +config = PolywrapClientConfigBuilder().set_package( |
| 57 | + uri=Uri.from_str("plugin/logger"), |
| 58 | + package=logger_plugin(LoggerConfig(logger=YourLogger(), level=LogLevel.INFO)) |
| 59 | +).set_interface( |
| 60 | + interface=Uri.from_str( "ens/wraps.eth:[email protected]"), |
| 61 | + implementations=[Uri.from_str("plugin/logger"), Uri.from_str("plugin/custom-logger")] |
| 62 | +).set_package( |
| 63 | + uri=Uri.from_str("plugin/custom-logger"), |
| 64 | + package=custom_logger_plugin() |
| 65 | +).build() |
| 66 | +``` |
| 67 | + |
| 68 | +### 5. Invoke All Logger Implementations |
| 69 | +When you'd like to log something to more than one logger, you can invoke all implementations of the logger interface: |
| 70 | +```python |
| 71 | +implementations = client.get_implementations( 'ens/wraps.eth:[email protected]') |
| 72 | + |
| 73 | +for impl in implementations: |
| 74 | + await client.invoke({ |
| 75 | + 'uri': impl, |
| 76 | + 'method': 'log', |
| 77 | + 'args': { |
| 78 | + 'level': 'INFO', |
| 79 | + 'message': 'message' |
| 80 | + } |
| 81 | + }) |
| 82 | +``` |
0 commit comments