Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment style #53

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const COLORS = {
export const LOG_LEVEL = {
FATAL: "fatal",
ERROR: "error",
WARN: "warn",
INFO: "info",
VERBOSE: "verbose",
DEBUG: "debug",
Expand Down
54 changes: 27 additions & 27 deletions src/logs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LOG_LEVEL } from "./constants";
import { PrettyLogs } from "./pretty-logs";
import { LogParams, LogReturn, Metadata, LogLevel } from "./types/log-types";
import { LogParams, LogReturn, Metadata, LogLevel, LogLevelWithOk } from "./types/log-types";

export class Logs {
private _maxLevel = -1;
Expand Down Expand Up @@ -68,6 +68,17 @@ export class Logs {
});
}

public warn(log: string, metadata?: Metadata): LogReturn {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LOG_LEVEL.WARN,
consoleLog: Logs.console.warn,
logMessage: log,
metadata,
type: "warn",
});
}

public error(log: string, metadata?: Metadata): LogReturn {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
Expand Down Expand Up @@ -131,40 +142,27 @@ export class Logs {
Logs.console = new PrettyLogs();
}

private _diffColorCommentMessage(type: string, message: string) {
const diffPrefix = {
fatal: "-", // - text in red
ok: "+", // + text in green
error: "!", // ! text in orange
info: "#", // # text in gray
debug: "@@@@", // @@ text in purple (and bold)@@
private _diffColorCommentMessage(type: LogLevelWithOk, message: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also no longer called diff

const diffPrefix: Record<LogLevelWithOk, string> = {
fatal: "> [!CAUTION]",
error: "> [!CAUTION]",
warn: "> [!WARNING]",
ok: "> [!TIP]",
info: "> [!NOTE]",
debug: "> [!IMPORTANT]",
verbose: "> [!NOTE]",
};
const selected = diffPrefix[type as keyof typeof diffPrefix];
const selected = diffPrefix[type];

if (selected) {
message = message
.trim()
.split("\n")
.map((line) => `${selected} ${line}`)
.join("\n");
} else if (type === "debug") {
// debug has special formatting
message = message
.split("\n")
.map((line) => `@@ ${line} @@`)
.join("\n"); // debug: "@@@@",
} else {
// default to gray
message = message
.split("\n")
.map((line) => `# ${line}`)
.map((line) => `> ${line}`)
.join("\n");
}

const diffHeader = "```diff";
const diffFooter = "```";

return [diffHeader, message, diffFooter].join("\n");
return [selected, message].join("\n");
}

private _getNumericLevel(level: LogLevel) {
Expand All @@ -173,8 +171,10 @@ export class Logs {
return 0;
case LOG_LEVEL.ERROR:
return 1;
case LOG_LEVEL.INFO:
case LOG_LEVEL.WARN:
return 2;
case LOG_LEVEL.INFO:
return 3;
case LOG_LEVEL.VERBOSE:
return 4;
case LOG_LEVEL.DEBUG:
Expand Down
7 changes: 7 additions & 0 deletions src/pretty-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class PrettyLogs {
this.info = this.info.bind(this);
this.error = this.error.bind(this);
this.fatal = this.fatal.bind(this);
this.warn = this.warn.bind(this);
this.debug = this.debug.bind(this);
this.verbose = this.verbose.bind(this);
}
Expand All @@ -18,6 +19,10 @@ export class PrettyLogs {
this._logWithStack(LOG_LEVEL.ERROR, message, metadata);
}

public warn(message: string, metadata?: Metadata | string) {
this._logWithStack(LOG_LEVEL.WARN, message, metadata);
}

public ok(message: string, metadata?: Metadata | string) {
this._logWithStack("ok", message, metadata);
}
Expand Down Expand Up @@ -99,6 +104,7 @@ export class PrettyLogs {
const defaultSymbols: Record<LogLevelWithOk, string> = {
fatal: "×",
ok: "✓",
warn: "⚠",
error: "⚠",
info: "›",
debug: "››",
Expand All @@ -124,6 +130,7 @@ export class PrettyLogs {
const colorMap: Record<LogLevelWithOk, [keyof typeof console, Colors]> = {
fatal: ["error", COLORS.fgRed],
ok: ["log", COLORS.fgGreen],
warn: ["warn", COLORS.fgYellow],
error: ["warn", COLORS.fgYellow],
info: ["info", COLORS.dim],
debug: ["debug", COLORS.fgMagenta],
Expand Down
Loading