Skip to content

Commit

Permalink
Adjust default console formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Apr 17, 2024
1 parent d4dd53d commit a5a2d4f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
3 changes: 2 additions & 1 deletion logtape/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Deno.test("defaultConsoleFormatter()", () => {
assertEquals(
defaultConsoleFormatter(info),
[
"%cINFO%c %cmy-app·junk %cHello, %o & %o!",
"%c7.0:13:20.0.00 %cINF%c %cmy-app·junk %cHello, %o & %o!",
"color: gray;",
"background-color: white; color: black;",
"background-color: default;",
"color: gray;",
Expand Down
19 changes: 15 additions & 4 deletions logtape/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ export type ConsoleFormatter = (record: LogRecord) => readonly unknown[];
const logLevelStyles: Record<LogLevel, string> = {
"debug": "background-color: gray; color: white;",
"info": "background-color: white; color: black;",
"warning": "background-color: orange;",
"error": "background-color: red;",
"fatal": "background-color: maroon;",
"warning": "background-color: orange; color: black;",
"error": "background-color: red; color: white;",
"fatal": "background-color: maroon; color: white;",
};

/**
Expand All @@ -99,10 +99,21 @@ export function defaultConsoleFormatter(record: LogRecord): readonly unknown[] {
values.push(record.message[i]);
}
}
const date = new Date(record.timestamp);
const time = `${
date.getHours().toLocaleString("en", { minimumSignificantDigits: 2 })
}:${
date.getMinutes().toLocaleString("en", { minimumSignificantDigits: 2 })
}:${
date.getSeconds().toLocaleString("en", { minimumSignificantDigits: 2 })
}.${
date.getMilliseconds().toLocaleString("en", { minimumSignificantDigits: 3 })
}`;
return [
`%c${record.level.toUpperCase()}%c %c${
`%c${time} %c${levelAbbreviations[record.level]}%c %c${
record.category.join("\xb7")
} %c${msg}`,
"color: gray;",
logLevelStyles[record.level],
"background-color: default;",
"color: gray;",
Expand Down
21 changes: 13 additions & 8 deletions logtape/sink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Deno.test("getConsoleSink()", () => {
assertEquals(mock.history(), [
{
DEBUG: [
"%cDEBUG%c %cmy-app·junk %cHello, %o & %o!",
"%c7.0:13:20.0.00 %cDBG%c %cmy-app·junk %cHello, %o & %o!",
"color: gray;",
"background-color: gray; color: white;",
"background-color: default;",
"color: gray;",
Expand All @@ -63,7 +64,8 @@ Deno.test("getConsoleSink()", () => {
},
{
INFO: [
"%cINFO%c %cmy-app·junk %cHello, %o & %o!",
"%c7.0:13:20.0.00 %cINF%c %cmy-app·junk %cHello, %o & %o!",
"color: gray;",
"background-color: white; color: black;",
"background-color: default;",
"color: gray;",
Expand All @@ -74,8 +76,9 @@ Deno.test("getConsoleSink()", () => {
},
{
WARN: [
"%cWARNING%c %cmy-app·junk %cHello, %o & %o!",
"background-color: orange;",
"%c7.0:13:20.0.00 %cWRN%c %cmy-app·junk %cHello, %o & %o!",
"color: gray;",
"background-color: orange; color: black;",
"background-color: default;",
"color: gray;",
"color: default;",
Expand All @@ -85,8 +88,9 @@ Deno.test("getConsoleSink()", () => {
},
{
ERROR: [
"%cERROR%c %cmy-app·junk %cHello, %o & %o!",
"background-color: red;",
"%c7.0:13:20.0.00 %cERR%c %cmy-app·junk %cHello, %o & %o!",
"color: gray;",
"background-color: red; color: white;",
"background-color: default;",
"color: gray;",
"color: default;",
Expand All @@ -96,8 +100,9 @@ Deno.test("getConsoleSink()", () => {
},
{
ERROR: [
"%cFATAL%c %cmy-app·junk %cHello, %o & %o!",
"background-color: maroon;",
"%c7.0:13:20.0.00 %cFTL%c %cmy-app·junk %cHello, %o & %o!",
"color: gray;",
"background-color: maroon; color: white;",
"background-color: default;",
"color: gray;",
"color: default;",
Expand Down

0 comments on commit a5a2d4f

Please sign in to comment.