Skip to content

Commit

Permalink
New Logger Options (microsoft#124)
Browse files Browse the repository at this point in the history
* Adding new logger options

* Change files

* minor fixes

Co-authored-by: Felipe Caminada <[email protected]>
  • Loading branch information
fcaminada and Felipe Caminada authored Jan 27, 2021
1 parent 79721fb commit 8cce330
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 2 deletions.
8 changes: 8 additions & 0 deletions change/lage-2021-01-22-13-51-38-logger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "minor",
"comment": "Adding new logger options",
"packageName": "lage",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2021-01-22T21:51:38.014Z"
}
1 change: 1 addition & 0 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function getPassThroughArgs(
"continue",
"safeExit",
"includeDependencies",
"logLevel",
"_",
];

Expand Down
2 changes: 2 additions & 0 deletions src/config/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,7 @@ export function getConfig(cwd: string): Config {
includeDependencies: parsedArgs.includeDependencies,
clear: parsedArgs.clear || false,
prune: parsedArgs.prune,
logLevel: parsedArgs.logLevel,
loggerOptions: configResults?.config.loggerOptions || {}
};
}
8 changes: 7 additions & 1 deletion src/logger/initReporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import { JsonReporter } from "./reporters/JsonReporter";

export function initReporters(config: Config) {
// Initialize logger
const logLevel = config.verbose ? LogLevel.verbose : LogLevel.info;
let logLevel = config.verbose ? LogLevel.verbose : LogLevel.info;
if (config.logLevel) {
type LogLevelString = keyof typeof LogLevel;
logLevel = LogLevel[config.logLevel as LogLevelString];
}

const reporters = [
config.reporter === "json"
? new JsonReporter({ logLevel })
: new NpmLogReporter({
logLevel,
grouped: config.grouped,
npmLoggerOptions: config.loggerOptions
}),
];

Expand Down
6 changes: 5 additions & 1 deletion src/logger/reporters/NpmLogReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { formatDuration, hrToSeconds } from "./formatDuration";
import { RunContext } from "../../types/RunContext";
import { getTaskId } from "@microsoft/task-scheduler";
import { NpmScriptTaskStatus } from "../../task/NpmScriptTask";
import { LoggerOptions } from "../../types/LoggerOptions";

const maxLengths = {
pkg: 0,
Expand Down Expand Up @@ -49,9 +50,12 @@ function isInfoData(data?: LogStructuredData): data is InfoData {
export class NpmLogReporter implements Reporter {
readonly groupedEntries = new Map<string, LogEntry[]>();

constructor(private options: { logLevel?: LogLevel; grouped?: boolean }) {
constructor(private options: { logLevel?: LogLevel; grouped?: boolean, npmLoggerOptions?: LoggerOptions }) {
options.logLevel = options.logLevel || LogLevel.info;
log.level = LogLevel[options.logLevel];
log.disp = { ...log.disp, ...options.npmLoggerOptions?.disp };
log.style = { ...log.style, ...options.npmLoggerOptions?.style };
log.levels = { ...log.levels, ...options.npmLoggerOptions?.levels };
}

log(entry: LogEntry) {
Expand Down
7 changes: 7 additions & 0 deletions src/types/CliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,11 @@ export interface CliOptions {
* A flag for the cache command only: prunes the cache older than 30 days by default, or specify a number of days
*/
prune: string;

/**
* The highest log level to report. All logs up to "info" are reported by default.
*
* Specifying "verbose" is equivalent to running with the `--verbose` argument.
*/
logLevel: string;
}
6 changes: 6 additions & 0 deletions src/types/ConfigOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CacheOptions } from "./CacheOptions";
import { Priority } from "./Priority";
import { Pipeline } from "./Pipeline";
import { LoggerOptions } from "./LoggerOptions";

export type NpmClient = "npm" | "yarn" | "pnpm";

Expand Down Expand Up @@ -47,4 +48,9 @@ export interface ConfigOptions {
* Run the tasks for the dependencies of scoped tasks
*/
includeDependencies: boolean;

/**
* Options that will be sent to all log reporters.
*/
loggerOptions: LoggerOptions;
}
15 changes: 15 additions & 0 deletions src/types/LoggerOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { LogLevel } from "../logger/LogLevel";

export interface LoggerOptions {
disp?: {
[level: string]: string;
},

style?: {
[level: string]: { fg?: string, bg?: string }
},

levels?: {
[level: string]: LogLevel
}
}

0 comments on commit 8cce330

Please sign in to comment.