datahaven/test/utils/logger.ts
Facundo Farall 728c320926
feat: Add Polkadot API support to CLI and e2e testing infra (#68)
In this PR:
1. Add [Polkadot API](https://papi.how/) support to `test/` directory
(i.e. the e2e CLI and testing framework) to be able to interact with the
Substrate chain.
1. This allows typed interactions with transactions, constants, storage,
runtime APIs, and non-typed interaction with RPC methods.
2. Types are autogenerated when running `bun i`, from the
`datahaven.scale` file that is part of this repo's version control.
2. Add new utilities file to `papi` related functionalities. For the
time being, generating a new signer from a private key.
3. ~Add a new step to the CLI that sends a transaction to the DataHaven
network. _*THIS SHOULD BE REMOVED SOON, IT'S JUST FOR TESTING
PURPOSES*_~
1. Both steps that send test transactions have been removed from the
CLI, for convenience and being error prone. Their scripts remain usable
for testing purposes if needed.
4. Removes the `apis.rs` files from the runtime definitions. Having them
in a separate file meant that the runtime was not including the Runtime
APIs in the metadata blob, preventing `papi` from creating types from
it. This change can be reapplied after upgrading to `polkadot-sdk`
`stable-2503`. More info
[here](https://github.com/paritytech/polkadot-sdk/issues/6659).
5. Add script to re-generate types, and corresponding docs.
6. Makes logger synchronous to avoid prints happening before logs.
2025-05-13 03:03:21 +00:00

52 lines
1.3 KiB
TypeScript

import chalk from "chalk";
import pino from "pino";
import pinoPretty from "pino-pretty";
const logLevel = process.env.LOG_LEVEL || "info";
const stream = pinoPretty({
colorize: true,
// Log to STDERR so it doesn't interfere with CLI output
destination: 2,
sync: true
});
// Custom logger type with success method
interface CustomLogger extends pino.Logger {
success(msg: string, ...args: any[]): void;
}
// Create the base logger with proper configuration
export const logger: CustomLogger = pino(
{
level: logLevel
},
stream
) as CustomLogger;
// Add custom success method to the logger
logger.success = function (message: string) {
this.info(`${message}`);
};
// Simple progress bar function
export const printProgress = (percent: number) => {
const width = 30;
const completed = Math.floor(width * (percent / 100));
const remaining = width - completed;
const bar = chalk.green("█".repeat(completed)) + chalk.gray("░".repeat(remaining));
console.log(`\n${chalk.bold("Progress:")} ${bar} ${percent}%\n`);
};
// Print a section header
export const printHeader = (title: string) => {
console.log(`\n${chalk.bold.cyan(`${title}`)}`);
console.log(chalk.gray("─".repeat(title.length + 3)));
};
// Print a divider
export const printDivider = () => {
console.log(chalk.gray(`\n${"─".repeat(50)}\n`));
};