-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: use default logger that includes indexer name (#120)
- Loading branch information
Showing
9 changed files
with
116 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "cli: use default logger that includes indexer name", | ||
"packageName": "apibara", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { createIndexer } from "./internal/app"; | ||
export { createLogger } from "./internal/logger"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { | ||
ConsolaOptions, | ||
ConsolaReporter, | ||
LogLevel, | ||
LogObject, | ||
LogType, | ||
} from "consola"; | ||
import { type ColorName, colors, getColor } from "consola/utils"; | ||
import { murmurHash } from "ohash"; | ||
|
||
const INDEXER_COLOR_MAP = [ | ||
colors.red, | ||
colors.green, | ||
colors.yellow, | ||
colors.blue, | ||
colors.magenta, | ||
colors.cyan, | ||
]; | ||
|
||
const TYPE_COLOR_MAP: { [k in LogType]?: string } = { | ||
info: "cyan", | ||
fail: "red", | ||
success: "green", | ||
ready: "green", | ||
start: "magenta", | ||
}; | ||
|
||
const LEVEL_COLOR_MAP: { [k in LogLevel]?: string } = { | ||
0: "red", | ||
1: "yellow", | ||
}; | ||
|
||
const MAX_INDEXER_NAME_LENGTH = 20; | ||
|
||
class DefaultReporter implements ConsolaReporter { | ||
private tag: string; | ||
|
||
constructor(indexer: string, indexers: string[], preset?: string) { | ||
const color = | ||
INDEXER_COLOR_MAP[murmurHash(indexer) % INDEXER_COLOR_MAP.length]; | ||
|
||
const presetLength = preset ? preset.length : 0; | ||
|
||
const longestIndexerName = | ||
Math.max(...indexers.map((i) => i.length), indexer.length) + presetLength; | ||
|
||
const paddedIndexer = `${indexer}${preset ? `:${preset} ` : ""}` | ||
.padEnd(longestIndexerName, " ") | ||
.slice(0, Math.min(longestIndexerName, MAX_INDEXER_NAME_LENGTH)); | ||
|
||
this.tag = color(`${paddedIndexer} |`); | ||
} | ||
|
||
log(logObj: LogObject, ctx: { options: ConsolaOptions }) { | ||
const { args } = logObj; | ||
const typeColor = | ||
TYPE_COLOR_MAP[logObj.type] || LEVEL_COLOR_MAP[logObj.level] || "gray"; | ||
|
||
const type = getColor(typeColor as ColorName, "white")(logObj.type); | ||
console.log(`${this.tag} ${type}`, ...args); | ||
} | ||
} | ||
|
||
export function createLogger({ | ||
indexer, | ||
indexers, | ||
preset, | ||
}: { indexer: string; indexers: string[]; preset?: string }) { | ||
return new DefaultReporter(indexer, indexers, preset); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters