@polywrap/logger-plugin-js
Advanced tools
Comparing version 0.10.0-pre.5 to 0.10.0
import { Module, Args_log, Logger_LogLevel } from "./wrap"; | ||
import { PluginFactory } from "@polywrap/plugin-js"; | ||
export declare type LogFunc = (level: Logger_LogLevel, message: string) => boolean; | ||
export declare type LogFunc = (level: Logger_LogLevel, message: string) => void; | ||
export interface LoggerPluginConfig { | ||
@@ -5,0 +5,0 @@ logFunc?: LogFunc; |
@@ -26,3 +26,4 @@ "use strict"; | ||
if (this.config.logFunc) { | ||
return this.config.logFunc(args.level, args.message); | ||
this.config.logFunc(args.level, args.message); | ||
return true; | ||
} | ||
@@ -29,0 +30,0 @@ switch (args.level) { |
@@ -49,3 +49,3 @@ "use strict"; | ||
/// Imported Objects START /// | ||
/* URI: "ens/logger.core.polywrap.eth" */ | ||
/* URI: "ens/wrappers.polywrap.eth:logger@1.0.0" */ | ||
var Logger_LogLevelEnum; | ||
@@ -58,3 +58,3 @@ (function (Logger_LogLevelEnum) { | ||
})(Logger_LogLevelEnum = exports.Logger_LogLevelEnum || (exports.Logger_LogLevelEnum = {})); | ||
/* URI: "ens/logger.core.polywrap.eth" */ | ||
/* URI: "ens/wrappers.polywrap.eth:logger@1.0.0" */ | ||
exports.Logger_Module = { | ||
@@ -64,3 +64,3 @@ log: function (args, client) { return __awaiter(void 0, void 0, void 0, function () { | ||
return [2 /*return*/, client.invoke({ | ||
uri: "ens/logger.core.polywrap.eth", | ||
uri: "ens/wrappers.polywrap.eth:logger@1.0.0", | ||
method: "log", | ||
@@ -67,0 +67,0 @@ args: args, |
@@ -5,3 +5,3 @@ "use strict"; | ||
exports.manifest = { | ||
name: "Logger", | ||
name: "logger-plugin", | ||
type: "plugin", | ||
@@ -22,3 +22,3 @@ version: "0.1", | ||
"type": "Logger_LogLevel", | ||
"uri": "ens/logger.core.polywrap.eth" | ||
"uri": "ens/wrappers.polywrap.eth:logger@1.0.0" | ||
} | ||
@@ -79,3 +79,3 @@ ], | ||
"type": "Logger_Module", | ||
"uri": "ens/logger.core.polywrap.eth" | ||
"uri": "ens/wrappers.polywrap.eth:logger@1.0.0" | ||
} | ||
@@ -82,0 +82,0 @@ ], |
{ | ||
"name": "@polywrap/logger-plugin-js", | ||
"description": "Polywrap Javascript Logger Plugin", | ||
"description": "Logger plugin wrapper, for use with the JS Polywrap client.", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/polywrap/monorepo.git" | ||
"url": "https://github.com/polywrap/logging.git" | ||
}, | ||
@@ -12,11 +12,9 @@ "files": [ | ||
], | ||
"version": "0.10.0-pre.5", | ||
"version": "0.10.0", | ||
"main": "build/index.js", | ||
"scripts": { | ||
"build": "rimraf ./build && yarn codegen && tsc --project tsconfig.build.json", | ||
"codegen": "node ../../../../dependencies/node_modules/polywrap/bin/polywrap codegen", | ||
"lint": "eslint --color -c ../../../../.eslintrc.js src/", | ||
"build": "rimraf ./build && yarn codegen && tsc", | ||
"codegen": "polywrap codegen", | ||
"test": "jest --passWithNoTests --runInBand --verbose", | ||
"test:ci": "jest --passWithNoTests --runInBand --verbose", | ||
"test:watch": "jest --watch --passWithNoTests --verbose" | ||
"publish:npm": "yarn publish --access public --non-interactive --verbose" | ||
}, | ||
@@ -31,4 +29,4 @@ "dependencies": { | ||
"@types/jest": "26.0.8", | ||
"@types/prettier": "2.6.0", | ||
"jest": "26.6.3", | ||
"polywrap": "0.10.0-pre.5", | ||
"rimraf": "3.0.2", | ||
@@ -39,3 +37,2 @@ "ts-jest": "26.5.4", | ||
}, | ||
"gitHead": "7346adaf5adb7e6bbb70d9247583e995650d390a", | ||
"publishConfig": { | ||
@@ -42,0 +39,0 @@ "access": "public" |
115
README.md
# @polywrap/logger-plugin-js | ||
The Logger plugin implements the `logger-interface` @ [ens/wrappers.polywrap.eth:logger@1.0.0](https://app.ens.domains/name/wrappers.polywrap.eth/details) (see [./src/schema.graphql](./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). | ||
Console Logger plugin implements the `wrap://ens/logger.core.polywrap.eth` core Polywrap interface. By default it logs all events using the Javascript `console` module. Different logging mechanisms can be set using the `LoggerConfig`. | ||
## Usage | ||
### 1. Configure Client | ||
When creating your Polywrap JS client, add the logger plugin: | ||
```typescript | ||
import { PolywrapClient } from "@polywrap/client-js"; | ||
import { loggerPlugin } from "@polywrap/logger-plugin-js"; | ||
## Log levels | ||
const client = new PolywrapClient({ | ||
// 1. Add the plugin package @ an arbitrary URI | ||
packages: [{ | ||
uri: "plugin/logger", | ||
package: loggerPlugin({ }) | ||
}], | ||
// 2. Register this plugin as an implementation of the interface | ||
interfaces: [{ | ||
interface: "ens/wrappers.polywrap.eth:logger@1.0.0", | ||
implementations: ["plugin/logger"] | ||
}], | ||
// 3. Redirect invocations @ the interface to the plugin (default impl) | ||
redirects: [{ | ||
from: "ens/wrappers.polywrap.eth:logger@1.0.0", | ||
to: "plugin/logger", | ||
}] | ||
}); | ||
``` | ||
- DEBUG | ||
- INFO | ||
- WARN | ||
- ERROR | ||
### 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: "ens/wrappers.polywrap.eth:logger@1.0.0" | "plugin/logger", | ||
method: "log", | ||
args: { | ||
level: "INFO", | ||
message: "foo bar baz" | ||
} | ||
}); | ||
``` | ||
## Example | ||
### 3. Customize The Logger | ||
When adding the logger to your client, you can add your own custom log function: | ||
```typescript | ||
new PolywrapClient({ | ||
packages: [{ | ||
uri: "plugin/logger", | ||
package: loggerPlugin({ | ||
logFunc: (level: string, message: string): void => { | ||
// add your own logic here... | ||
} | ||
}) | ||
}], | ||
... | ||
}) | ||
``` | ||
```ts | ||
import { loggerPlugin, LogLevel } from "@polywrap/logger-plugin-js"; | ||
### 4. Add Multiple Loggers | ||
Multiple logger implementations can be added to the client: | ||
```typescript | ||
const client = new PolywrapClient({ | ||
plugins: [{ | ||
from: "wrap://ens/js-logger.polywrap.eth", | ||
to: loggerPlugin() | ||
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: "wrap://ens/logger.core.polywrap.eth", | ||
implementations: ["wrap://ens/js-logger.polywrap.eth"], | ||
interface: "ens/wrappers.polywrap.eth:logger@1.0.0", | ||
implementations: ["plugin/logger", "plugin/custom-logger"] | ||
}] | ||
}); | ||
``` | ||
// For custom logging logic, initialize the logger like so: | ||
// loggerPlugin((level: LogLevel, message: string) => { ... }) | ||
### 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( | ||
"ens/wrappers.polywrap.eth:logger@1.0.0" | ||
); | ||
const response = await client.query<{ log: boolean }>({ | ||
uri: "wrap://ens/js-logger.polywrap.eth", | ||
query: ` | ||
query { | ||
log( | ||
level: ${LogLevel.INFO} | ||
message: "Informational message" | ||
) | ||
const implementations: string[] = result.ok ? result.value : []; | ||
for (const impl of implementations) { | ||
await client.invoke({ | ||
uri: impl, | ||
method: "log", | ||
args: { | ||
level: "INFO", | ||
message: "message" | ||
} | ||
` | ||
}); | ||
}); | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38832
21
595
104
63