@polywrap/logger-plugin-js

v0.12.0
Logger plugin wrapper, for use with the JS Polywrap client.

@polywrap/logger-plugin-js

The Logger plugin implements the logger-interface @ wrapscan.io/polywrap/[email protected] (see ./polywrap.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, ClientConfigBuilder } from "@polywrap/client-js";
import { loggerPlugin } from "@polywrap/logger-plugin-js";

const config = new ClientConfigBuilder()
  // 1. Add the plugin package @ an arbitrary URI
  .addPackage(
    "plugin/logger",
    loggerPlugin({ })
  )
  // 2. Register this plugin as an implementation of the interface
  .addInterfaceImplementation(
    "wrapscan.io/polywrap/[email protected]",
    "plugin/logger"
  )
  // 3. Redirect invocations @ the interface to the plugin (default impl)
  .addRedirect(
    "wrapscan.io/polywrap/[email protected]",
    "plugin/logger"
  )
  .build();

const client = new 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:

await client.invoke({
  uri: "wrapscan.io/polywrap/[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:

const config = new ClientConfigBuilder()
  .addPackage(
    "plugin/logger",
    loggerPlugin({
      logFunc: (level: string, message: string): void => {
        // add your own logic here...
      }
    })
  )
  .build();

const client = new PolywrapClient(config);

4. Add Multiple Loggers

Multiple logger implementations can be added to the client:

const config = new ClientConfigBuilder()
  .addPackage(
    "plugin/logger",
    loggerPlugin({ })
  )
  .addPackage(
    "plugin/custom-logger",
    loggerPlugin({ logFunc: ... })
  )
  .addInterfaceImplementations(
    "wrapscan.io/polywrap/[email protected]",
    ["plugin/logger", "plugin/custom-logger"]
  )
  .addRedirect(
    "wrapscan.io/polywrap/[email protected]",
    "plugin/logger"
  )
  .build();

const client = new PolywrapClient(config);

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(
  "wrapscan.io/polywrap/[email protected]"
);

const implementations: string[] = result.ok ? result.value : [];

for (const impl of implementations) {
  await client.invoke({
    uri: impl,
    method: "log",
    args: {
      level: "INFO",
      message: "message"
    }
  });
}
npm i @polywrap/logger-plugin-js

Metadata

  • MIT
  • Whatever
  • Unknown
  • released 8/20/2023

Downloads