Skip to content

Commit

Permalink
Add support for LOG_LEVEL env var in pkl-gen-typescript (#42)
Browse files Browse the repository at this point in the history
* Add support for LOG_LEVEL env var in pkl-gen-typescript

* remove unused import
  • Loading branch information
jasongwartz authored Apr 1, 2024
1 parent 0edbc07 commit c16f6ee
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions pkl-gen-typescript/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from "chalk";
import { boolean, command, flag, option, optional, restPositionals, run, string } from "cmd-ts";
import consola from "consola";
import consola, { LogLevels } from "consola";
import { access } from "fs/promises";
import { join } from "path";
import { cwd } from "process";
Expand Down Expand Up @@ -44,9 +44,24 @@ export const cli = command({
}),
},
handler: async ({ pklModules, settingsFilePath, outputDirectory, dryRun, verbose }) => {
consola.level = verbose
? 4
: (parseInt(process.env.CONSOLA_LEVEL ?? "") || null) ?? (process.env.DEBUG ? 4 : 3);
/*
Four ways to set the log level, in order of precedence:
- CLI flag "-v", sets log level to "debug"
- environment variable CONSOLA_LEVEL set to an integer
- environment variable LOG_LEVEL set to a string that is a valid log level name
- environment variable DEBUG, sets log level to "debug"
- defaults to "info"
*/
const logLevel = verbose
? LogLevels.debug
: (parseInt(process.env.CONSOLA_LEVEL ?? "") || null) ??
(process.env.LOG_LEVEL !== undefined &&
Object.keys(LogLevels).includes(process.env.LOG_LEVEL.toLowerCase()))
? LogLevels[process.env.LOG_LEVEL?.toLowerCase() as keyof typeof LogLevels]
: process.env.DEBUG
? LogLevels.debug
: LogLevels.info;
consola.level = logLevel;

if (!pklModules.length) {
consola.error("You must provide at least one file to evaluate.");
Expand Down

0 comments on commit c16f6ee

Please sign in to comment.