use of org.apache.geode.management.internal.cli.functions.ChangeLogLevelFunction in project geode by apache.
the class MiscellaneousCommands method changeLogLevel.
@CliCommand(value = CliStrings.CHANGE_LOGLEVEL, help = CliStrings.CHANGE_LOGLEVEL__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_LOGS }, interceptor = "org.apache.geode.management.internal.cli.commands.MiscellaneousCommands$ChangeLogLevelInterceptor")
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.WRITE)
public Result changeLogLevel(@CliOption(key = CliStrings.CHANGE_LOGLEVEL__MEMBER, help = CliStrings.CHANGE_LOGLEVEL__MEMBER__HELP) String[] memberIds, @CliOption(key = CliStrings.CHANGE_LOGLEVEL__GROUPS, unspecifiedDefaultValue = "", help = CliStrings.CHANGE_LOGLEVEL__GROUPS__HELP) String[] grps, @CliOption(key = CliStrings.CHANGE_LOGLEVEL__LOGLEVEL, optionContext = ConverterHint.LOG_LEVEL, mandatory = true, unspecifiedDefaultValue = "", help = CliStrings.CHANGE_LOGLEVEL__LOGLEVEL__HELP) String logLevel) {
try {
if ((memberIds == null || memberIds.length == 0) && (grps == null || grps.length == 0)) {
return ResultBuilder.createUserErrorResult(CliStrings.CHANGE_LOGLEVEL__MSG__SPECIFY_GRP_OR_MEMBER);
}
InternalCache cache = GemFireCacheImpl.getInstance();
LogWriter logger = cache.getLogger();
Set<DistributedMember> dsMembers = new HashSet<DistributedMember>();
Set<DistributedMember> ds = CliUtil.getAllMembers(cache);
if (grps != null && grps.length > 0) {
for (String grp : grps) {
dsMembers.addAll(cache.getDistributedSystem().getGroupMembers(grp));
}
}
if (memberIds != null && memberIds.length > 0) {
for (String member : memberIds) {
Iterator<DistributedMember> it = ds.iterator();
while (it.hasNext()) {
DistributedMember mem = it.next();
if (mem.getName() == null ? false : mem.getName().equals(member) || mem.getId().equals(member)) {
dsMembers.add(mem);
break;
}
}
}
}
if (dsMembers.size() == 0) {
return ResultBuilder.createGemFireErrorResult(CliStrings.CHANGE_LOGLEVEL__MSG_NO_MEMBERS);
}
Function logFunction = new ChangeLogLevelFunction();
FunctionService.registerFunction(logFunction);
Object[] functionArgs = new Object[1];
functionArgs[0] = logLevel;
CompositeResultData compositeResultData = ResultBuilder.createCompositeResultData();
SectionResultData section = compositeResultData.addSection("section");
TabularResultData resultTable = section.addTable("ChangeLogLevel");
resultTable = resultTable.setHeader("Summary");
Execution execution = FunctionService.onMembers(dsMembers).setArguments(functionArgs);
if (execution == null) {
return ResultBuilder.createUserErrorResult(CliStrings.CHANGE_LOGLEVEL__MSG__CANNOT_EXECUTE);
}
List<?> resultList = (List<?>) execution.execute(logFunction).getResult();
for (Object object : resultList) {
try {
if (object instanceof Throwable) {
logger.warning("Exception in ChangeLogLevelFunction " + ((Throwable) object).getMessage(), ((Throwable) object));
continue;
}
if (object != null) {
Map<String, String> resultMap = (Map<String, String>) object;
Entry<String, String> entry = resultMap.entrySet().iterator().next();
if (entry.getValue().contains("ChangeLogLevelFunction exception")) {
resultTable.accumulate(CliStrings.CHANGE_LOGLEVEL__COLUMN_MEMBER, entry.getKey());
resultTable.accumulate(CliStrings.CHANGE_LOGLEVEL__COLUMN_STATUS, "false");
} else {
resultTable.accumulate(CliStrings.CHANGE_LOGLEVEL__COLUMN_MEMBER, entry.getKey());
resultTable.accumulate(CliStrings.CHANGE_LOGLEVEL__COLUMN_STATUS, "true");
}
}
} catch (Exception ex) {
LogWrapper.getInstance().warning("change log level command exception " + ex);
continue;
}
}
Result result = ResultBuilder.buildResult(compositeResultData);
logger.info("change log-level command result=" + result);
return result;
} catch (Exception ex) {
GemFireCacheImpl.getInstance().getLogger().error("GFSH Changeloglevel exception: " + ex);
return ResultBuilder.createUserErrorResult(ex.getMessage());
}
}
Aggregations