Skip to content

Commit

Permalink
feat: Add logLevel option
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Nov 25, 2020
1 parent ebf79d6 commit 80c4524
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/lib/application.ts
Expand Up @@ -145,6 +145,7 @@ export class Application extends ChildableComponent<
this.logger = new Logger();
this.options.setLogger(this.logger);
}
this.logger.level = this.options.getValue("logLevel");

this.plugins.load();

Expand Down
11 changes: 11 additions & 0 deletions src/lib/utils/loggers.ts
@@ -1,5 +1,8 @@
import * as ts from "typescript";
import * as Util from "util";
import { url } from "inspector";

const isDebugging = () => Boolean(url());

/**
* List of known log levels. Used to specify the urgency of a log message.
Expand Down Expand Up @@ -29,6 +32,11 @@ export class Logger {
*/
warningCount = 0;

/**
* The minimum logging level to print.
*/
level = LogLevel.Info;

/**
* Has an error been raised through the log method?
*/
Expand Down Expand Up @@ -208,6 +216,9 @@ export class ConsoleLogger extends Logger {
newLine?: boolean
) {
super.log(message, level, newLine);
if (level < this.level && !isDebugging()) {
return;
}

let output = "";
if (level === LogLevel.Error) {
Expand Down
16 changes: 16 additions & 0 deletions src/lib/utils/options/declaration.ts
@@ -1,4 +1,5 @@
import * as _ from "lodash";
import { LogLevel } from "../loggers";

/**
* An interface describing all TypeDoc specific options options. Generated from a
Expand All @@ -14,6 +15,20 @@ export type TypeDocOptions = {
: TypeDocOptionMap[K];
};

/**
* Describes all TypeDoc specific options as returned by {@link Options.getValue}, this is
* slightly more restrictive than the {@link TypeDocOptions} since it does not allow both
* keys and values for mapped option types.
*/
export type TypeDocOptionValues = {
[K in keyof TypeDocOptionMap]: TypeDocOptionMap[K] extends Record<
string,
infer U
>
? Exclude<U, string>
: TypeDocOptionMap[K];
};

/**
* Describes all TypeDoc options. Used internally to provide better types when fetching options.
* External consumers should likely use [[TypeDocOptions]] instead.
Expand Down Expand Up @@ -56,6 +71,7 @@ export interface TypeDocOptionMap {
version: boolean;
plugin: string[];
logger: unknown; // string | Function
logLevel: typeof LogLevel;
listInvalidSymbolLinks: boolean;
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib/utils/options/options.ts
Expand Up @@ -9,6 +9,7 @@ import {
TypeDocOptions,
KeyToDeclaration,
TypeDocOptionMap,
TypeDocOptionValues,
} from "./declaration";
import { Logger } from "../loggers";
import { insertPrioritySorted, unique } from "../array";
Expand Down Expand Up @@ -233,7 +234,7 @@ export class Options {
* Gets a value for the given option key, throwing if the option has not been declared.
* @param name
*/
getValue<K extends keyof TypeDocOptions>(name: K): TypeDocOptions[K];
getValue<K extends keyof TypeDocOptions>(name: K): TypeDocOptionValues[K];
getValue(name: NeverIfInternal<string>): unknown;
getValue(name: string): unknown {
const declaration = this.getDeclaration(name);
Expand Down Expand Up @@ -331,7 +332,7 @@ export function BindOption<K extends keyof TypeDocOptionMap>(
name: K
): <IK extends PropertyKey>(
target: ({ application: Application } | { options: Options }) &
{ [K2 in IK]: TypeDocOptions[K] },
{ [K2 in IK]: TypeDocOptionValues[K] },
key: IK
) => void;

Expand Down
8 changes: 8 additions & 0 deletions src/lib/utils/options/sources/typedoc.ts
@@ -1,4 +1,5 @@
import { Options } from "..";
import { LogLevel } from "../../loggers";
import { ParameterType, ParameterHint } from "../declaration";

export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
Expand Down Expand Up @@ -195,6 +196,13 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
defaultValue: "console",
type: ParameterType.Mixed,
});
options.addDeclaration({
name: "logLevel",
help: "Specify what level of logging should be used.",
type: ParameterType.Map,
map: LogLevel,
defaultValue: LogLevel.Info,
});
options.addDeclaration({
name: "listInvalidSymbolLinks",
help:
Expand Down

0 comments on commit 80c4524

Please sign in to comment.