# @polywrap/logger-plugin-js

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

Latest version **0.12.0** (published 2023-08-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @polywrap/logger-plugin-js
pnpm add @polywrap/logger-plugin-js
yarn add @polywrap/logger-plugin-js
bun add @polywrap/logger-plugin-js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.12.0 |
| Published | 2023-08-20 |
| First published | 2022-06-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 16.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | polywrap-build-bot, dorgjelli |

## Links

- npm: https://www.npmjs.com/package/@polywrap/logger-plugin-js
- Repository: https://github.com/polywrap/logging
- npm.io page: https://npm.io/package/@polywrap/logger-plugin-js

## Dependencies (2)

- [@polywrap/core-js](https://npm.io/package/@polywrap/core-js.md) ~0.12.0
- [@polywrap/plugin-js](https://npm.io/package/@polywrap/plugin-js.md) ~0.12.0

## Recent versions

- 0.12.0 (latest) — 2023-08-20
- 0.12.0-pre.0 (pre) — 2023-07-26
- 0.11.0-pre.0 — 2023-07-26
- 0.10.1 — 2023-04-27
- 0.9.7 — 2023-03-28
- 0.10.0-pre.10 — 2023-03-04
- 0.9.6 — 2023-02-19
- 0.9.5 — 2023-02-10
- 0.10.0-pre.6 — 2022-12-21
- 0.10.0 — 2022-11-24
- 0.10.0-pre.5 — 2022-11-22
- 0.10.0-pre.4 — 2022-11-16
- 0.10.0-pre.3 — 2022-11-13
- 0.10.0-pre.1 — 2022-11-12
- 0.10.0-pre.0 — 2022-11-05
- … 23 more at https://npm.io/package/@polywrap/logger-plugin-js/versions

## README

# @polywrap/logger-plugin-js
The Logger plugin implements the `logger-interface` @ [wrapscan.io/polywrap/logger@1.0.0](../../interface/) (see [./polywrap.graphql](./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:
```typescript
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:
```typescript
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:
```typescript
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:
```typescript
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:
```typescript
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"
    }
  });
}
```

---
_Source: https://npm.io/package/@polywrap/logger-plugin-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
