Skip to content

Commit af9614b

Browse files
committed
[FIX] Streamline log-level flags, allow overwrite with env variable
The Yargs defaulting of the log-level argument let to an unwanted enforcing of log-level "info", which could not be overwritten using the UI5_LOG_LVL environment variable. If a user does not specify any log-level related flags, no log level should be set by the CLI.
1 parent 0ece563 commit af9614b

File tree

7 files changed

+94
-23
lines changed

7 files changed

+94
-23
lines changed

lib/cli/base.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,24 @@ export default function(cli) {
1313
"This option will disable resolution of node package dependencies.",
1414
type: "string"
1515
})
16-
.option("verbose", {
17-
describe: "Enable verbose logging.",
18-
type: "boolean"
19-
})
2016
.option("loglevel", {
2117
alias: "log-level",
22-
describe: "Set the logging level (error|warn|info|verbose|silly).",
18+
describe: "Set the logging level (silent|error|warn|info|perf|verbose|silly).",
2319
default: "info",
2420
type: "string"
2521
})
26-
.option("x-perf", {
27-
describe: "Outputs performance measurements",
22+
.option("verbose", {
23+
describe: "Enable verbose logging.",
24+
default: false,
25+
type: "boolean"
26+
})
27+
.option("silent", {
28+
describe: "Disable all log output.",
29+
default: false,
30+
type: "boolean"
31+
})
32+
.option("perf", {
33+
describe: "Enable performance measurements and related logging.",
2834
default: false,
2935
type: "boolean"
3036
})

lib/cli/commands/tree.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ tree.builder = function(cli) {
2222
tree.handler = async function(argv) {
2323
let startTime;
2424
let elapsedTime;
25-
if (argv.xPerf) {
25+
if (argv.perf) {
2626
startTime = process.hrtime();
2727
}
2828
const {graphFromStaticFile, graphFromPackageDependencies} = await import("@ui5/project/graph");
@@ -39,7 +39,7 @@ tree.handler = async function(argv) {
3939
});
4040
}
4141

42-
if (argv.xPerf) {
42+
if (argv.perf) {
4343
elapsedTime = await getElapsedTime(startTime);
4444
}
4545

@@ -93,7 +93,7 @@ tree.handler = async function(argv) {
9393
} else {
9494
console.log(chalk.italic(`None`));
9595
}
96-
if (argv.xPerf) {
96+
if (argv.perf) {
9797
console.log("");
9898
console.log(chalk.blue(
9999
`Dependency graph generation took ${chalk.bold(elapsedTime)}`));

lib/cli/middlewares/logger.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {setLogLevel, getLogger} from "@ui5/logger";
1+
import {setLogLevel, isLogLevelEnabled, getLogger} from "@ui5/logger";
22
import ConsoleWriter from "@ui5/logger/writers/Console";
33
import {getVersion} from "../version.js";
44
/**
@@ -7,13 +7,26 @@ import {getVersion} from "../version.js";
77
* @param {object} argv logger arguments
88
*/
99
export async function initLogger(argv) {
10-
ConsoleWriter.init();
11-
12-
if (argv.loglevel) {
10+
if (argv.loglevel && argv.loglevel !== "info") {
11+
// argv.loglevel defaults to "info", which is anyways already the Logger's default
12+
// Therefore do not explicitly set it again in order to allow overwriting the log level
13+
// using the UI5_LOG_LVL environment variable
1314
setLogLevel(argv.loglevel);
1415
}
16+
if (argv.perf) {
17+
setLogLevel("perf");
18+
}
1519
if (argv.verbose) {
1620
setLogLevel("verbose");
21+
}
22+
if (argv.silent) {
23+
// Silent should overrule any other log-level related CLI flags
24+
setLogLevel("silent");
25+
}
26+
27+
// Initialize writer
28+
ConsoleWriter.init();
29+
if (isLogLevelEnabled("verbose")) {
1730
const log = getLogger("cli:middlewares:base");
1831
log.verbose(`using @ui5/cli version ${getVersion()}`);
1932
log.verbose(`using node version ${process.version}`);

test/lib/cli/commands/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function getDefaultArgv() {
99
"loglevel": "info",
1010
"log-level": "info",
1111
"logLevel": "info",
12-
"x-perf": false,
13-
"xPerf": false,
12+
"perf": false,
13+
"silent": false,
1414
"include-all-dependencies": false,
1515
"all": false,
1616
"a": false,

test/lib/cli/commands/serve.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ function getDefaultArgv() {
1010
"loglevel": "info",
1111
"log-level": "info",
1212
"logLevel": "info",
13-
"x-perf": false,
14-
"xPerf": false,
13+
"perf": false,
14+
"silent": false,
1515
"h2": false,
1616
"simple-index": false,
1717
"simpleIndex": false,

test/lib/cli/commands/tree.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ function getDefaultArgv() {
1010
"loglevel": "info",
1111
"log-level": "info",
1212
"logLevel": "info",
13-
"x-perf": false,
14-
"xPerf": false,
13+
"perf": false,
14+
"silent": false,
1515
"$0": "ui5"
1616
};
1717
}
@@ -225,7 +225,7 @@ test.serial("ui5 tree --x-perf", async (t) => {
225225
});
226226
});
227227

228-
argv.xPerf = true;
228+
argv.perf = true;
229229

230230
sinon.stub(process, "hrtime")
231231
.withArgs().returns([0, 0])

test/lib/cli/middlewares/logger.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,69 @@ import stripAnsi from "strip-ansi";
66
test.beforeEach(async (t) => {
77
t.context.verboseLogStub = sinon.stub();
88
t.context.setLogLevelStub = sinon.stub();
9+
t.context.isLogLevelEnabledStub = sinon.stub().returns(true);
10+
t.context.getVersionStub = sinon.stub().returns("1.0.0");
911
t.context.logger = await esmock("../../../../lib/cli/middlewares/logger.js", {
12+
"../../../../lib/cli/version.js": {
13+
getVersion: t.context.getVersionStub
14+
},
1015
"@ui5/logger": {
1116
getLogger: () => ({
1217
verbose: t.context.verboseLogStub,
1318
}),
1419
setLogLevel: t.context.setLogLevelStub,
20+
isLogLevelEnabled: t.context.isLogLevelEnabledStub,
1521
}
1622
});
1723
});
1824

1925
test.serial("init logger", async (t) => {
20-
const {logger, setLogLevelStub} = t.context;
26+
const {logger, setLogLevelStub, isLogLevelEnabledStub, verboseLogStub, getVersionStub} = t.context;
2127
await logger.initLogger({});
2228
t.is(setLogLevelStub.callCount, 0, "setLevel has not been called");
29+
t.is(isLogLevelEnabledStub.callCount, 1, "isLogLevelEnabled has been called once");
30+
t.is(isLogLevelEnabledStub.firstCall.firstArg, "verbose",
31+
"isLogLevelEnabled has been called with expected argument");
32+
t.is(getVersionStub.callCount, 1, "getVersion has been called once");
33+
t.is(verboseLogStub.callCount, 2, "log.verbose has been called twice");
34+
t.is(verboseLogStub.firstCall.firstArg, "using @ui5/cli version 1.0.0",
35+
"log.verbose has been called with expected argument on first call");
36+
t.is(verboseLogStub.secondCall.firstArg, `using node version ${process.version}`,
37+
"log.verbose has been called with expected argument on second call");
2338
});
2439

2540
test.serial("With log-level flag", async (t) => {
2641
const {logger, setLogLevelStub} = t.context;
2742
await logger.initLogger({loglevel: "silly"});
2843
t.is(setLogLevelStub.callCount, 1, "setLevel has been called once");
29-
t.is(setLogLevelStub.getCall(0).args[0], "silly", "sets log level to verbose");
44+
t.is(setLogLevelStub.getCall(0).args[0], "silly", "sets log level to silly");
45+
});
46+
47+
test.serial("With default log-level flag", async (t) => {
48+
const {logger, setLogLevelStub} = t.context;
49+
await logger.initLogger({loglevel: "info"});
50+
t.is(setLogLevelStub.callCount, 0, "setLevel has not been called");
51+
});
52+
53+
test.serial("With verbose flag", async (t) => {
54+
const {logger, setLogLevelStub} = t.context;
55+
await logger.initLogger({verbose: true});
56+
t.is(setLogLevelStub.callCount, 1, "setLevel has been called once");
57+
t.is(setLogLevelStub.getCall(0).args[0], "verbose", "sets log level to verbose");
58+
});
59+
60+
test.serial("With perf flag", async (t) => {
61+
const {logger, setLogLevelStub} = t.context;
62+
await logger.initLogger({perf: true});
63+
t.is(setLogLevelStub.callCount, 1, "setLevel has been called once");
64+
t.is(setLogLevelStub.getCall(0).args[0], "perf", "sets log level to perf");
65+
});
66+
67+
test.serial("With silent flag", async (t) => {
68+
const {logger, setLogLevelStub} = t.context;
69+
await logger.initLogger({silent: true});
70+
t.is(setLogLevelStub.callCount, 1, "setLevel has been called once");
71+
t.is(setLogLevelStub.getCall(0).args[0], "silent", "sets log level to silent");
3072
});
3173

3274
test.serial("With log-level and verbose flag", async (t) => {
@@ -37,6 +79,16 @@ test.serial("With log-level and verbose flag", async (t) => {
3779
t.is(setLogLevelStub.getCall(1).args[0], "verbose", "sets log level to verbose");
3880
});
3981

82+
test.serial("With log-level, verbose, perf and silent flag", async (t) => {
83+
const {logger, setLogLevelStub} = t.context;
84+
await logger.initLogger({loglevel: "silly", verbose: true, perf: true, silent: true});
85+
t.is(setLogLevelStub.callCount, 4, "setLevel has been called four times");
86+
t.is(setLogLevelStub.getCall(0).args[0], "silly", "sets log level to verbose");
87+
t.is(setLogLevelStub.getCall(1).args[0], "perf", "sets log level to perf");
88+
t.is(setLogLevelStub.getCall(2).args[0], "verbose", "Third sets log level to verbose");
89+
t.is(setLogLevelStub.getCall(3).args[0], "silent", "sets log level to silent");
90+
});
91+
4092
import path from "node:path";
4193
import {execa} from "execa";
4294
import {fileURLToPath} from "node:url";

0 commit comments

Comments
 (0)