Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

PAN-2324 Support changing log level at runtime #1656

Merged
merged 5 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import tech.pegasys.pantheon.ethereum.eth.transactions.TransactionPool;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.filter.FilterManager;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.AdminAddPeer;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.AdminChangeLogLevel;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.AdminNodeInfo;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.AdminPeers;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.AdminRemovePeer;
Expand Down Expand Up @@ -314,7 +315,8 @@ blockchainQueries, new TransactionTracer(blockReplay), parameter),
new AdminRemovePeer(p2pNetwork, parameter),
new AdminNodeInfo(
clientVersion, networkId, genesisConfigOptions, p2pNetwork, blockchainQueries),
new AdminPeers(p2pNetwork));
new AdminPeers(p2pNetwork),
new AdminChangeLogLevel(parameter));
}
if (rpcApis.contains(RpcApis.EEA)) {
final PrivateTransactionHandler privateTransactionHandler =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum RpcMethod {
ADMIN_NODE_INFO("admin_nodeInfo"),
ADMIN_PEERS("admin_peers"),
ADMIN_REMOVE_PEER("admin_removePeer"),
ADMIN_CHANGE_LOG_LEVEL("admin_changeLogLevel"),
CLIQUE_DISCARD("clique_discard"),
CLIQUE_GET_SIGNERS("clique_getSigners"),
CLIQUE_GET_SIGNERS_AT_HASH("clique_getSignersAtHash"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 ConsenSys AG.
*
* 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.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.exception.InvalidJsonRpcParameters;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;

public class AdminChangeLogLevel implements JsonRpcMethod {

private static final Logger LOG = LogManager.getLogger();
private final JsonRpcParameter parameters;

public AdminChangeLogLevel(final JsonRpcParameter parameters) {
this.parameters = parameters;
}

@Override
public String getName() {
return RpcMethod.ADMIN_CHANGE_LOG_LEVEL.getMethodName();
}

@Override
public JsonRpcResponse response(final JsonRpcRequest request) {
try {
final Level logLevel = parameters.required(request.getParams(), 0, Level.class);
LOG.log(Level.OFF, String.format("Setting logging level to %s", logLevel.name()));
Configurator.setAllLevels("", logLevel);
return new JsonRpcSuccessResponse(request.getId());
} catch (InvalidJsonRpcParameters invalidJsonRpcParameters) {
return new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2018 ConsenSys AG.
*
* 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.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class AdminChangeLogLevelTest {

private final JsonRpcParameter parameters = new JsonRpcParameter();

private AdminChangeLogLevel adminChangeLogLevel;

@Before
public void before() {
adminChangeLogLevel = new AdminChangeLogLevel(parameters);
Configurator.setAllLevels("", Level.INFO);
}

@Test
public void shouldReturnExpectedMethodName() {
assertThat(adminChangeLogLevel.getName()).isEqualTo("admin_changeLogLevel");
}

@Test
public void shouldReturnCorrectResponse() {
final JsonRpcRequest request =
new JsonRpcRequest("2.0", "admin_changeLogLevel", new Object[] {Level.DEBUG});
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(request.getId());

final Level levelBeforeJsonRpcRequest = LogManager.getLogger().getLevel();
final JsonRpcSuccessResponse actualResponse =
(JsonRpcSuccessResponse) adminChangeLogLevel.response(request);
final Level levelAfterJsonRpcRequest = LogManager.getLogger().getLevel();

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
assertEquals(Level.INFO, levelBeforeJsonRpcRequest);
assertEquals(Level.DEBUG, levelAfterJsonRpcRequest);
}

@Test
public void requestHasValidStringLogLevelParameter() {
final JsonRpcRequest request =
new JsonRpcRequest("2.0", "admin_changeLogLevel", new String[] {"DEBUG"});
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(request.getId());

final Level levelBeforeJsonRpcRequest = LogManager.getLogger().getLevel();
final JsonRpcSuccessResponse actualResponse =
(JsonRpcSuccessResponse) adminChangeLogLevel.response(request);
final Level levelAfterJsonRpcRequest = LogManager.getLogger().getLevel();

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
assertEquals(Level.INFO, levelBeforeJsonRpcRequest);
assertEquals(Level.DEBUG, levelAfterJsonRpcRequest);
}

@Test
public void requestHasNullArrayParameter() {
final JsonRpcRequest request =
new JsonRpcRequest("2.0", "admin_changeLogLevel", new Object[] {null});
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);

final JsonRpcResponse actualResponse = adminChangeLogLevel.response(request);
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}

@Test
public void requestHasInvalidStringLogLevelParameter() {
final JsonRpcRequest request =
new JsonRpcRequest("2.0", "admin_changeLogLevel", new String[] {"INVALID"});
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);

final JsonRpcResponse actualResponse = adminChangeLogLevel.response(request);
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
}