Skip to content

Commit

Permalink
Add logger plugin (#119)
Browse files Browse the repository at this point in the history
Add a new logger plugin to standardize how users can log information.
This is a simple wrapper around consola for now and will be used later
to make the logger configurable by the user in `apibara.config.ts`.
  • Loading branch information
jaipaljadeja authored Nov 29, 2024
2 parents a177c8d + c402e05 commit 829a617
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Add logger plugin",
"packageName": "@apibara/indexer",
"email": "[email protected]",
"dependentChangeType": "patch"
}
7 changes: 7 additions & 0 deletions change/apibara-45ec1e62-bbea-48bc-ac29-b6ea1acc4024.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Add logger plugin",
"packageName": "apibara",
"email": "[email protected]",
"dependentChangeType": "patch"
}
15 changes: 7 additions & 8 deletions examples/cli/indexers/1-evm.indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import { EvmStream } from "@apibara/evm";
import { defineIndexer } from "@apibara/indexer";

export default defineIndexer(EvmStream)({
streamUrl: "http://localhost:7007",
streamUrl: "https://ethereum.preview.apibara.org",
finality: "accepted",
startingCursor: {
orderKey: 10_000_000n,
},
filter: {
header: "always",
transactions: [{}],
},
async transform({ cursor, endCursor }) {
console.log({ cursor, endCursor });
},
hooks: {
"message:invalidate"({ message }) {
console.warn(message);
},
async transform({ endCursor }) {
console.log({ endCursor });
},
});
5 changes: 4 additions & 1 deletion examples/cli/indexers/2-starknet.indexer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineIndexer, useSink } from "@apibara/indexer";
import { useLogger } from "@apibara/indexer/plugins/logger";
import { sqlite } from "@apibara/indexer/sinks/sqlite";
import { StarknetStream } from "@apibara/starknet";
import type { ApibaraRuntimeConfig } from "apibara/types";
Expand Down Expand Up @@ -31,7 +32,9 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) {
},
],
},
async transform({ block: { header }, context }) {
async transform({ endCursor, block: { header }, context }) {
const logger = useLogger();
logger.info("Transforming block ", endCursor);
const { writer } = useSink({ context });

// writer.insert([{
Expand Down
12 changes: 9 additions & 3 deletions packages/cli/src/runtime/internal/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createIndexer as _createIndexer } from "@apibara/indexer";
import { logger } from "@apibara/indexer/plugins/logger";

import { config } from "#apibara-internal-virtual/config";
import { indexers } from "#apibara-internal-virtual/indexers";
Expand Down Expand Up @@ -33,7 +34,12 @@ export function createIndexer(indexerName: string, preset?: string) {
);
}

return typeof indexerDefinition.indexer === "function"
? _createIndexer(indexerDefinition.indexer(runtimeConfig))
: _createIndexer(indexerDefinition.indexer);
const definition =
typeof indexerDefinition.indexer === "function"
? indexerDefinition.indexer(runtimeConfig)
: indexerDefinition.indexer;

definition.plugins = [...(definition.plugins ?? []), logger()];

return _createIndexer(definition);
}
1 change: 1 addition & 0 deletions packages/indexer/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default defineBuildConfig({
"./src/vcr/index.ts",
"./src/plugins/index.ts",
"./src/plugins/kv.ts",
"./src/plugins/logger.ts",
"./src/plugins/persistence.ts",
],
clean: true,
Expand Down
6 changes: 6 additions & 0 deletions packages/indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
"require": "./dist/plugins/index.cjs",
"default": "./dist/plugins/index.mjs"
},
"./plugins/logger": {
"types": "./dist/plugins/logger.d.ts",
"import": "./dist/plugins/logger.mjs",
"require": "./dist/plugins/logger.cjs",
"default": "./dist/plugins/logger.mjs"
},
"./plugins/kv": {
"types": "./dist/plugins/kv.d.ts",
"import": "./dist/plugins/kv.mjs",
Expand Down
30 changes: 30 additions & 0 deletions packages/indexer/src/plugins/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { type ConsolaInstance, type ConsolaReporter, consola } from "consola";
import { useIndexerContext } from "../context";
import { defineIndexerPlugin } from "./config";

export type { ConsolaReporter, ConsolaInstance } from "consola";

export function logger<TFilter, TBlock, TTxnParams>({
logger,
}: { logger?: ConsolaReporter } = {}) {
return defineIndexerPlugin<TFilter, TBlock, TTxnParams>((indexer) => {
indexer.hooks.hook("run:before", () => {
const ctx = useIndexerContext();

if (logger) {
ctx.logger = consola.create({ reporters: [logger] });
} else {
ctx.logger = consola.create({});
}
});
});
}

export function useLogger(): ConsolaInstance {
const ctx = useIndexerContext();

if (!ctx?.logger)
throw new Error("Logger plugin is not available in context");

return ctx.logger;
}

0 comments on commit 829a617

Please sign in to comment.