Skip to content

Commit

Permalink
tweak logging outputs again
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Nov 5, 2024
1 parent dceb713 commit 2cc4100
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-ties-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ryanatkn/belt": patch
---

tweak logging outputs again
49 changes: 36 additions & 13 deletions src/lib/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ export interface Logger_State {
debug_suffixes: Logger_Prefixes_And_Suffixes_Getter;
}

export type Logger_Prefixes_And_Suffixes_Getter = (st: typeof styleText) => unknown[];
export type Logger_Prefixes_And_Suffixes_Getter = (
st: typeof styleText,
args: unknown[],
) => unknown[];

const EMPTY_GETTER: Logger_Prefixes_And_Suffixes_Getter = () => EMPTY_ARRAY;

Expand Down Expand Up @@ -130,39 +133,59 @@ export class Base_Logger {
error(...args: unknown[]): void {
if (!should_log(this.state.level, 'error')) return;
this.state.console.error(
...this.#resolve_values(this.state.prefixes, this.state.error_prefixes, this.prefixes).concat(
...this.#resolve_values(
args,
this.state.prefixes,
this.state.error_prefixes,
this.prefixes,
).concat(
args,
this.#resolve_values(this.suffixes, this.state.error_suffixes, this.state.suffixes),
this.#resolve_values(args, this.suffixes, this.state.error_suffixes, this.state.suffixes),
),
);
}

warn(...args: unknown[]): void {
if (!should_log(this.state.level, 'warn')) return;
this.state.console.warn(
...this.#resolve_values(this.state.prefixes, this.state.warn_prefixes, this.prefixes).concat(
...this.#resolve_values(
args,
this.state.prefixes,
this.state.warn_prefixes,
this.prefixes,
).concat(
args,
this.#resolve_values(this.suffixes, this.state.warn_suffixes, this.state.suffixes),
this.#resolve_values(args, this.suffixes, this.state.warn_suffixes, this.state.suffixes),
),
);
}

info(...args: unknown[]): void {
if (!should_log(this.state.level, 'info')) return;
this.state.console.log(
...this.#resolve_values(this.state.prefixes, this.state.info_prefixes, this.prefixes).concat(
...this.#resolve_values(
args,
this.#resolve_values(this.suffixes, this.state.info_suffixes, this.state.suffixes),
this.state.prefixes,
this.state.info_prefixes,
this.prefixes,
).concat(
args,
this.#resolve_values(args, this.suffixes, this.state.info_suffixes, this.state.suffixes),
),
);
}

debug(...args: unknown[]): void {
if (!should_log(this.state.level, 'debug')) return;
this.state.console.log(
...this.#resolve_values(this.state.prefixes, this.state.debug_prefixes, this.prefixes).concat(
...this.#resolve_values(
args,
this.state.prefixes,
this.state.debug_prefixes,
this.prefixes,
).concat(
args,
this.#resolve_values(this.suffixes, this.state.debug_suffixes, this.state.suffixes),
this.#resolve_values(args, this.suffixes, this.state.debug_suffixes, this.state.suffixes),
),
);
}
Expand All @@ -176,11 +199,11 @@ export class Base_Logger {
this.state.console.log('\n'.repeat(count));
}

#resolve_values(...getters: Logger_Prefixes_And_Suffixes_Getter[]): unknown[] {
#resolve_values(args: unknown[], ...getters: Logger_Prefixes_And_Suffixes_Getter[]): unknown[] {
let resolved: unknown[] | undefined;
const {st} = this.state;
for (const getter of getters) {
const values = getter(st);
const values = getter(st, args);
for (const value of values) {
(resolved ??= []).push(value);
}
Expand Down Expand Up @@ -210,13 +233,13 @@ export class Logger extends Base_Logger {
static prefixes: Logger_Prefixes_And_Suffixes_Getter = EMPTY_GETTER;
static suffixes: Logger_Prefixes_And_Suffixes_Getter = EMPTY_GETTER;
static error_prefixes: Logger_Prefixes_And_Suffixes_Getter = (st) => [
st('red', `${Logger.char_error.repeat(3)}error`),
st('red', `${Logger.char_error.repeat(3)}error\n`),
];
static error_suffixes: Logger_Prefixes_And_Suffixes_Getter = (st) => [
st('red', `\n${Logger.char_error.repeat(3)}`),
];
static warn_prefixes: Logger_Prefixes_And_Suffixes_Getter = (st) => [
st('red', `${Logger.char_warn.repeat(3)}warn`),
st('red', `${Logger.char_warn.repeat(3)}warn\n`),
];
static warn_suffixes: Logger_Prefixes_And_Suffixes_Getter = (st) => [
st('red', `\n${Logger.char_warn.repeat(3)}`),
Expand Down

0 comments on commit 2cc4100

Please sign in to comment.