From 017d9c950538c0ebef537d42e1a8d2db8516bc44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20L=C3=B3pez=20Le=C3=B3n?= Date: Sat, 12 Feb 2022 22:07:48 -0300 Subject: [PATCH] Convert logging level option type to string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Diego López León --- .../options/stable/LoggingLevelOption.java | 16 ++++- .../stable/LoggingLevelOptionTest.java | 60 +++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 besu/src/test/java/org/hyperledger/besu/cli/options/stable/LoggingLevelOptionTest.java diff --git a/besu/src/main/java/org/hyperledger/besu/cli/options/stable/LoggingLevelOption.java b/besu/src/main/java/org/hyperledger/besu/cli/options/stable/LoggingLevelOption.java index 317e98705e8..4b7bb0051ab 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/options/stable/LoggingLevelOption.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/options/stable/LoggingLevelOption.java @@ -14,8 +14,12 @@ */ package org.hyperledger.besu.cli.options.stable; +import java.util.Set; + import org.apache.logging.log4j.Level; import picocli.CommandLine; +import picocli.CommandLine.Model.CommandSpec; +import picocli.CommandLine.Spec; public class LoggingLevelOption { @@ -23,18 +27,24 @@ public static LoggingLevelOption create() { return new LoggingLevelOption(); } + private static final Set ACCEPTED_VALUES = + Set.of("OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE", "ALL"); + @Spec CommandSpec spec; private Level logLevel; @CommandLine.Option( names = {"--logging", "-l"}, paramLabel = "", description = "Logging verbosity levels: OFF, ERROR, WARN, INFO, DEBUG, TRACE, ALL") - public void setLogLevel(final Level logLevel) { - if (Level.FATAL.equals(logLevel)) { + public void setLogLevel(final String logLevel) { + if ("FATAL".equalsIgnoreCase(logLevel)) { System.out.println("FATAL level is deprecated"); this.logLevel = Level.ERROR; + } else if (ACCEPTED_VALUES.contains(logLevel.toUpperCase())) { + this.logLevel = Level.getLevel(logLevel.toUpperCase()); } else { - this.logLevel = logLevel; + throw new CommandLine.ParameterException( + spec.commandLine(), "Unknown logging value: " + logLevel); } } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/options/stable/LoggingLevelOptionTest.java b/besu/src/test/java/org/hyperledger/besu/cli/options/stable/LoggingLevelOptionTest.java new file mode 100644 index 00000000000..78b2d4d55b8 --- /dev/null +++ b/besu/src/test/java/org/hyperledger/besu/cli/options/stable/LoggingLevelOptionTest.java @@ -0,0 +1,60 @@ +/* + * Copyright contributors to Hyperledger Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.cli.options.stable; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Answers.RETURNS_DEEP_STUBS; + +import java.util.Arrays; + +import org.apache.logging.log4j.Level; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import picocli.CommandLine.Model.CommandSpec; +import picocli.CommandLine.ParameterException; + +public class LoggingLevelOptionTest { + + private LoggingLevelOption levelOption; + + @Before + public void setUp() { + levelOption = new LoggingLevelOption(); + } + + @Test + public void fatalLevelEqualsToError() { + levelOption.setLogLevel("fatal"); + assertThat(levelOption.getLogLevel()).isEqualTo(Level.ERROR); + } + + @Test + public void setsExpectedLevels() { + Arrays.stream(Level.values()) + .filter(level -> !Level.FATAL.equals(level)) + .forEach( + level -> { + levelOption.setLogLevel(level.name()); + assertThat(levelOption.getLogLevel()).isEqualTo(level); + }); + } + + @Test(expected = ParameterException.class) + public void failsOnUnknownLevel() { + levelOption.spec = Mockito.mock(CommandSpec.class, RETURNS_DEEP_STUBS); + levelOption.setLogLevel("unknown"); + } +}