0.12.0 • Published 9 months ago

@polywrap/logger-plugin-js v0.12.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@polywrap/logger-plugin-js

The Logger plugin implements the logger-interface @ wrapscan.io/polywrap/logger@1.0.0 (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/logger@1.0",
    "plugin/logger"
  )
  // 3. Redirect invocations @ the interface to the plugin (default impl)
  .addRedirect(
    "wrapscan.io/polywrap/logger@1.0",
    "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/logger@1.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:

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/logger@1.0",
    ["plugin/logger", "plugin/custom-logger"]
  )
  .addRedirect(
    "wrapscan.io/polywrap/logger@1.0",
    "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/logger@1.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"
    }
  });
}
0.12.0-pre.0

10 months ago

0.12.0

9 months ago

0.11.0-pre.0

10 months ago

0.10.1

1 year ago

0.9.7

1 year ago

0.10.0-pre.10

1 year ago

0.9.6

1 year ago

0.9.5

1 year ago

0.10.0-pre.6

1 year ago

0.10.0-pre.3

1 year ago

0.10.0-pre.4

1 year ago

0.10.0-pre.1

1 year ago

0.10.0-pre.5

1 year ago

0.10.0

1 year ago

0.9.4

2 years ago

0.9.3

2 years ago

0.10.0-pre.0

2 years ago

0.9.0

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.8.0

2 years ago

0.5.0

2 years ago

0.4.1

2 years ago

0.7.0

2 years ago

0.6.0

2 years ago

0.4.0

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago