Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add errorprone to prevent Log4j direct usage #3759

Merged
merged 9 commits into from
May 3, 2022
Prev Previous commit
Next Next commit
Convert logging level option type to string
Signed-off-by: Diego López León <dieguitoll@gmail.com>
  • Loading branch information
diega committed Apr 25, 2022
commit 017d9c950538c0ebef537d42e1a8d2db8516bc44
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");
}
}