Skip to content

Commit

Permalink
refactor!: move log levels and types to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 10, 2023
1 parent da1bc73 commit 514f5b3
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 104 deletions.
23 changes: 17 additions & 6 deletions src/consola.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { normalizeLogLevel } from "./log.levels";
import { Types } from "./log.types";
import { LogTypes, LogType } from "./constants";
import { isLogObj } from "./utils/index";
import type {
ConsolaOptions,
ConsolaReporter,
ConsolaLogObject,
LogType,
ConsolaReporterLogObject,
} from "./types";
import type { PromptOptions } from "./prompt";
Expand All @@ -26,7 +24,7 @@ export class Consola {

constructor(options: Partial<ConsolaOptions> = {}) {
// Options
const types = options.types || Types;
const types = options.types || LogTypes;
this.options = {
// Defaults
throttle: 1000,
Expand All @@ -35,7 +33,7 @@ export class Consola {
...options,
// Overrides, Normalizations and Clones
defaults: { ...options.defaults },
level: normalizeLogLevel(options.level, types),
level: _normalizeLogLevel(options.level, types),
reporters: [...(options.reporters || [])],
types,
};
Expand Down Expand Up @@ -65,7 +63,7 @@ export class Consola {
}

set level(level) {
this.options.level = normalizeLogLevel(
this.options.level = _normalizeLogLevel(
level,
this.options.types,
this.options.level
Expand Down Expand Up @@ -353,6 +351,19 @@ export class Consola {
}
}

function _normalizeLogLevel(input: any, types: any = {}, defaultLevel = 3) {
if (input === undefined) {
return defaultLevel;
}
if (typeof input === "number") {
return input;
}
if (types[input] && types[input].level !== undefined) {
return types[input].level;
}
return defaultLevel;
}

export interface ConsolaInstance extends Consola {
fatal(message: ConsolaLogObject | any, ...args: any[]): void;
error(message: ConsolaLogObject | any, ...args: any[]): void;
Expand Down
99 changes: 99 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { ConsolaLogObject } from "./types";

// eslint-disable-next-line @typescript-eslint/ban-types
export type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | (number & {});

export const LogLevels: Record<LogType, number> = {
silent: Number.NEGATIVE_INFINITY,
fatal: 0,
error: 0,
warn: 1,
log: 2,
info: 3,
success: 3,
fail: 3,
ready: 3,
start: 3,
debug: 4,
trace: 5,
verbose: Number.POSITIVE_INFINITY,
};

export type LogType =
// 0
| "silent"
| "fatal"
| "error"
// 1
| "warn"
// 2
| "log"
// 3
| "info"
| "success"
| "fail"
| "ready"
| "start"
// Verbose
| "debug"
| "trace"
| "verbose"
// eslint-disable-next-line @typescript-eslint/ban-types
| (string & {});

export const LogTypes: Record<LogType, ConsolaLogObject> = {
// Silent
silent: {
level: -1,
},

// Level 0
fatal: {
level: LogLevels.Fatal,
},
error: {
level: LogLevels.Error,
},

// Level 1
warn: {
level: LogLevels.Warn,
},

// Level 2
log: {
level: LogLevels.Log,
},

// Level 3
info: {
level: LogLevels.Info,
},
success: {
level: LogLevels.Success,
},
fail: {
level: LogLevels.Fail,
},
ready: {
level: LogLevels.Info,
},
start: {
level: LogLevels.Info,
},

// Level 4
debug: {
level: LogLevels.Debug,
},

// Level 5
trace: {
level: LogLevels.Trace,
},

// Verbose
verbose: {
level: LogLevels.Verbose,
},
};
4 changes: 2 additions & 2 deletions src/index.shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Consola } from "./consola";
export { Types } from "./log.types";
export { LogLevels } from "./log.levels";
export { LogLevels, LogTypes } from "./constants";
export type { LogLevel, LogType } from "./constants";
export { isLogObj } from "./utils";
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isDebug, isTest, isCI } from "std-env";
import { LogLevels } from "./log.levels";
import type { ConsolaOptions, LogLevel } from "./types";
import { LogLevels, LogLevel } from "./constants";
import type { ConsolaOptions } from "./types";
import { BasicReporter, FancyReporter } from "./reporters";
import { createConsola as _createConsola } from "./consola";

Expand Down
59 changes: 0 additions & 59 deletions src/log.types.ts

This file was deleted.

22 changes: 1 addition & 21 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
import { InspectOptions } from "node:util";

export type LogLevel = number; // Built-in: 0 | 1 | 2 | 3 | 4 | 5;

export type LogType =
// 0
| "silent"
| "fatal"
| "error"
// 1
| "warn"
// 2
| "log"
// 3
| "info"
| "success"
| "ready"
| "start"
// Verbose
| "debug"
| "trace"
| "verbose";
import type { LogLevel, LogType } from "./constants";

export interface ConsolaLogObject {
level?: LogLevel | LogType;
Expand Down
14 changes: 0 additions & 14 deletions src/log.levels.ts → src/utils/level.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
export const LogLevels = {
Fatal: 0,
Error: 0,
Warn: 1,
Log: 2,
Info: 3,
Success: 3,
Fail: 3,
Debug: 4,
Trace: 5,
Silent: Number.NEGATIVE_INFINITY,
Verbose: Number.POSITIVE_INFINITY,
};

export function normalizeLogLevel(
input: any,
types: any = {},
Expand Down

0 comments on commit 514f5b3

Please sign in to comment.