/
index.ts
49 lines (42 loc) 路 1.26 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { isDebug, isTest, isCI } from "std-env";
import { LogLevels, LogLevel } from "./constants";
import type { ConsolaOptions } from "./types";
import { BasicReporter } from "./reporters/basic";
import { FancyReporter } from "./reporters/fancy";
import { ConsolaInstance, createConsola as _createConsola } from "./consola";
export * from "./shared";
export function createConsola(
options: Partial<ConsolaOptions & { fancy: boolean }> = {},
): ConsolaInstance {
// Log level
let level = _getDefaultLogLevel();
if (process.env.CONSOLA_LEVEL) {
level = Number.parseInt(process.env.CONSOLA_LEVEL) ?? level;
}
// Create new consola instance
const consola = _createConsola({
level: level as LogLevel,
defaults: { level },
stdout: process.stdout,
stderr: process.stderr,
prompt: (...args) => import("./prompt").then((m) => m.prompt(...args)),
reporters: options.reporters || [
options.fancy ?? !(isCI || isTest)
? new FancyReporter()
: new BasicReporter(),
],
...options,
});
return consola;
}
function _getDefaultLogLevel() {
if (isDebug) {
return LogLevels.debug;
}
if (isTest) {
return LogLevels.warn;
}
return LogLevels.info;
}
export const consola = createConsola();
export default consola;