Skip to content

Commit

Permalink
Convert logging level option type to string
Browse files Browse the repository at this point in the history
Signed-off-by: Diego López León <dieguitoll@gmail.com>
  • Loading branch information
diega committed Feb 14, 2022
1 parent 3742243 commit c1ef9a3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,37 @@
*/
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 {

public static LoggingLevelOption create() {
return new LoggingLevelOption();
}

private static final Set<String> ACCEPTED_VALUES =
Set.of("OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE", "ALL");
@Spec CommandSpec spec;
private Level logLevel;

@CommandLine.Option(
names = {"--logging", "-l"},
paramLabel = "<LOG VERBOSITY LEVEL>",
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit c1ef9a3

Please sign in to comment.