Skip to content

Commit 52c7aae

Browse files
josephperrottmatsko
authored andcommitted
feat(dev-infra): add group functions to logging system and remove color param (angular#37232)
Adds .group and .groupEnd functions to each of the logging functions to allow creating groups in the logged output. Additionally removes the color parameter from logging functions, in favor of the color being applied to the string at the call site. PR Close angular#37232
1 parent 87b1aea commit 52c7aae

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

dev-infra/utils/console.ts

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,56 +44,54 @@ export enum LOG_LEVELS {
4444
export const DEFAULT_LOG_LEVEL = LOG_LEVELS.INFO;
4545

4646
/** Write to the console for at INFO logging level */
47-
export function info(...text: string[]): void;
48-
export function info(color: typeof chalk, ...text: string[]): void;
49-
export function info(color: typeof chalk|string, ...text: string[]) {
50-
runConsoleCommand(console.info, LOG_LEVELS.INFO, color, ...text);
51-
}
47+
export const info = buildLogLevelFunction(() => console.info, LOG_LEVELS.INFO);
5248

5349
/** Write to the console for at ERROR logging level */
54-
export function error(...text: string[]): void;
55-
export function error(color: typeof chalk, ...text: string[]): void;
56-
export function error(color: typeof chalk|string, ...text: string[]) {
57-
runConsoleCommand(console.error, LOG_LEVELS.ERROR, color, ...text);
58-
}
50+
export const error = buildLogLevelFunction(() => console.error, LOG_LEVELS.ERROR);
5951

6052
/** Write to the console for at DEBUG logging level */
61-
export function debug(...text: string[]): void;
62-
export function debug(color: typeof chalk, ...text: string[]): void;
63-
export function debug(color: typeof chalk|string, ...text: string[]) {
64-
runConsoleCommand(console.debug, LOG_LEVELS.DEBUG, color, ...text);
65-
}
53+
export const debug = buildLogLevelFunction(() => console.debug, LOG_LEVELS.DEBUG);
6654

6755
/** Write to the console for at LOG logging level */
68-
export function log(...text: string[]): void;
69-
export function log(color: typeof chalk, ...text: string[]): void;
70-
export function log(color: typeof chalk|string, ...text: string[]) {
71-
// tslint:disable-next-line: no-console
72-
runConsoleCommand(console.log, LOG_LEVELS.LOG, color, ...text);
73-
}
56+
// tslint:disable-next-line: no-console
57+
export const log = buildLogLevelFunction(() => console.log, LOG_LEVELS.LOG);
7458

7559
/** Write to the console for at WARN logging level */
76-
export function warn(...text: string[]): void;
77-
export function warn(color: typeof chalk, ...text: string[]): void;
78-
export function warn(color: typeof chalk|string, ...text: string[]) {
79-
runConsoleCommand(console.warn, LOG_LEVELS.WARN, color, ...text);
60+
export const warn = buildLogLevelFunction(() => console.warn, LOG_LEVELS.WARN);
61+
62+
/** Build an instance of a logging function for the provided level. */
63+
function buildLogLevelFunction(loadCommand: () => Function, level: LOG_LEVELS) {
64+
/** Write to stdout for the LOG_LEVEL. */
65+
const loggingFunction = (...text: string[]) => {
66+
runConsoleCommand(loadCommand, level, ...text);
67+
};
68+
69+
/** Start a group at the LOG_LEVEL, optionally starting it as collapsed. */
70+
loggingFunction.group = (text: string, collapsed = false) => {
71+
const command = collapsed ? console.groupCollapsed : console.group;
72+
runConsoleCommand(() => command, level, text);
73+
};
74+
75+
/** End the group at the LOG_LEVEL. */
76+
loggingFunction.groupEnd = () => {
77+
runConsoleCommand(() => console.groupEnd, level);
78+
};
79+
80+
return loggingFunction;
8081
}
8182

8283
/**
8384
* Run the console command provided, if the environments logging level greater than the
8485
* provided logging level.
86+
*
87+
* The loadCommand takes in a function which is called to retrieve the console.* function
88+
* to allow for jasmine spies to still work in testing. Without this method of retrieval
89+
* the console.* function, the function is saved into the closure of the created logging
90+
* function before jasmine can spy.
8591
*/
86-
function runConsoleCommand(
87-
command: Function, logLevel: LOG_LEVELS, color: typeof chalk|string, ...text: string[]) {
92+
function runConsoleCommand(loadCommand: () => Function, logLevel: LOG_LEVELS, ...text: string[]) {
8893
if (getLogLevel() >= logLevel) {
89-
if (typeof color === 'function') {
90-
text = text.map(entry => color(entry));
91-
} else {
92-
text = [color as string, ...text];
93-
}
94-
for (const textEntry of text) {
95-
command(textEntry);
96-
}
94+
loadCommand()(...text);
9795
}
9896
}
9997

0 commit comments

Comments
 (0)