@polywrap/logger-plugin-js
The Logger plugin implements the logger-interface
@ ens/wrappers.polywrap.eth:logger@1.0.0 (see ./src/schema.graphql). By default, it logs all events using the Javascript console
global object. You can circumvent this functionality by setting the logFunc
property on the plugin's config (examples below).
Usage
1. Configure Client
When creating your Polywrap JS client, add the logger plugin:
import { PolywrapClient } from "@polywrap/client-js";
import { loggerPlugin } from "@polywrap/logger-plugin-js";
const client = new PolywrapClient({
packages: [{
uri: "plugin/logger",
package: loggerPlugin({ })
}],
interfaces: [{
interface: "ens/wrappers.polywrap.eth:logger@1.0.0",
implementations: ["plugin/logger"]
}],
redirects: [{
from: "ens/wrappers.polywrap.eth:logger@1.0.0",
to: "plugin/logger",
}]
});
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:
await client.invoke({
uri: "ens/wrappers.polywrap.eth:logger@1.0.0" | "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:
new PolywrapClient({
packages: [{
uri: "plugin/logger",
package: loggerPlugin({
logFunc: (level: string, message: string): void => {
}
})
}],
...
})
4. Add Multiple Loggers
Multiple logger implementations can be added to the client:
const client = new PolywrapClient({
packages: [
{
uri: "plugin/logger",
package: loggerPlugin({ })
},
{
uri: "plugin/custom-logger",
package: loggerPlugin({ logFunc: ... })
}
],
redirects: [{
from: "ens/wrappers.polywrap.eth:logger@1.0.0",
to: "plugin/logger"
}],
interfaces: [{
interface: "ens/wrappers.polywrap.eth:logger@1.0.0",
implementations: ["plugin/logger", "plugin/custom-logger"]
}]
});
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:
const result = await client.getImplementations(
"ens/wrappers.polywrap.eth:logger@1.0.0"
);
const implementations: string[] = result.ok ? result.value : [];
for (const impl of implementations) {
await client.invoke({
uri: impl,
method: "log",
args: {
level: "INFO",
message: "message"
}
});
}