Skip to content

Commit

Permalink
feat: improve logging format on unicode supporting environments; add …
Browse files Browse the repository at this point in the history
…success log level
  • Loading branch information
rafamel committed Apr 7, 2021
1 parent cc6bb50 commit f410ac6
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 37 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"find-up": "^5.0.0",
"fs-extra": "^9.1.0",
"glob": "^7.1.6",
"is-unicode-supported": "^0.1.0",
"merge-strategies": "^0.2.0",
"pipettes": "^0.1.3",
"prefix-stream": "^1.0.1",
Expand Down
1 change: 0 additions & 1 deletion src/cli/bin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export function main(argv: string[], options: Required<CLI.Options>): Task {
style(':' + extension.name, { bold: true, color: 'blue' }),
stringifyArgvCommands(cmd._)
),
print(),
context(
{ cwd: project.directory },
create(
Expand Down
3 changes: 1 addition & 2 deletions src/cli/commands/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ export async function watch(params: CLI.Extension.Params): Promise<Task> {
style(params.options.bin, { bold: true }),
style(':watch', { bold: true, color: 'blue' }),
stringifyArgvCommands(params.argv)
),
print()
)
);
}),
combine(tasks, { include: names })
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const constants = into(
},
collections: {
restrict: [':'],
levels: ['silent', 'error', 'warn', 'info', 'debug', 'trace']
levels: ['silent', 'error', 'warn', 'success', 'info', 'debug', 'trace']
}
})
);
9 changes: 8 additions & 1 deletion src/definitions/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ export interface Context {
readonly cancellation: Promise<void>;
}

export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'silent';
export type LogLevel =
| 'trace'
| 'debug'
| 'info'
| 'success'
| 'warn'
| 'error'
| 'silent';

export type PrefixPolicy = 'none' | 'print' | 'exec' | 'all';

Expand Down
73 changes: 41 additions & 32 deletions src/tasks/stdio/log.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
import { Task, LogLevel, Context } from '../../definitions';
import { addPrefix } from '../../helpers/prefix';
import { style } from '../../utils/style';
import { Members } from 'type-core';
import { style, StyleColor } from '../../utils/style';
import isUnicodeSupported from 'is-unicode-supported';
import util from 'util';

const rank: Members<number> = {
silent: 0,
error: 1,
warn: 2,
info: 3,
debug: 4,
trace: 5
};

const color = {
trace: (str: string) =>
style(str, { bold: true, bg: 'magenta', color: 'white' }),
debug: (str: string) =>
style(str, { bold: true, bg: 'cyan', color: 'white' }),
info: (str: string) =>
style(str, { bold: true, bg: 'green', color: 'white' }),
warn: (str: string) =>
style(str, { bold: true, bg: 'yellow', color: 'white' }),
error: (str: string) => style(str, { bold: true, bg: 'red', color: 'white' })
const unicode = isUnicodeSupported();
// Symbols: 🅧, 🆇, ⓧ, ✖, ‼, ⚠, ✔︎, ✓, ⓘ, ⅈ, ℹ︎, 🅳, 🅓, ⓓ, », 🆃, 🅣, ⓣ, ⊗, ⊘
const levels: Record<LogLevel, [number, string, StyleColor | undefined]> = {
silent: [0, '', undefined],
error: [1, '🆇 ', 'red'],
warn: [2, '‼ ', 'yellow'],
success: [3, '✔︎✔︎', 'green'],
info: [4, 'ⅈ ', 'blue'],
debug: [5, '🅳 ', 'magenta'],
trace: [6, '🆃 ', 'grey']
};

/**
Expand All @@ -33,27 +24,45 @@ const color = {
* @returns Task
*/
export function log(level: LogLevel, item: any, ...data: any[]): Task.Sync {
level = String(level).toLowerCase() as LogLevel;
const nLevel = rank[level.toLowerCase()] || 5;
const levelArg = normalizeLevel(level);
const levelArgRank = levels[levelArg]
? levels[levelArg][0]
: Math.min(...Object.values(levels).map((arr) => arr[0]));

return (ctx: Context): void => {
if (level === 'silent' || level.toLowerCase() === 'silent') return;
if (levelArg === 'silent') return;

const levelCtx = normalizeLevel(ctx.level);
const levelCtxRank = levels[levelCtx]
? levels[levelCtx][0]
: Math.max(...Object.values(levels).map((arr) => arr[0]));

const nCurrent = rank[String(ctx.level).toLowerCase()] || 0;
if (nCurrent >= nLevel) {
if (levelCtxRank >= levelArgRank) {
const str = addPrefix(
util.format(item, ...data) + '\n',
getLoggerMessagePrefix(level),
getLoggerMessagePrefix(levelArg),
'print',
ctx
);
nLevel > rank.warn ? ctx.stdio[1].write(str) : ctx.stdio[2].write(str);

levelArgRank === levels.error[0]
? ctx.stdio[2].write(str)
: ctx.stdio[1].write(str);
}
};
}

function getLoggerMessagePrefix(level: Exclude<LogLevel, 'silent'>): string {
const fn = color[level];
const name = level.toUpperCase();
return fn ? fn(` ${name} `) : style(name, { bold: true });
function normalizeLevel(level: LogLevel): LogLevel {
return String(level).toLowerCase() as LogLevel;
}

function getLoggerMessagePrefix(level: LogLevel): string {
const params = levels[level];
if (!params) return style(level.toUpperCase(), { bold: true });
if (unicode) return style(params[1], { bold: true, color: params[2] });
return style(` ${level.toUpperCase()} `, {
bold: true,
bg: params[2],
color: 'white'
});
}

0 comments on commit f410ac6

Please sign in to comment.