Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Evolveum/midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaHonchar committed Jul 18, 2019
2 parents 3afcb0b + dc4bebe commit cc87a81
Show file tree
Hide file tree
Showing 26 changed files with 479 additions and 194 deletions.
Expand Up @@ -25,11 +25,13 @@
import com.evolveum.midpoint.common.configuration.api.MidpointConfiguration;
import com.evolveum.midpoint.common.configuration.api.ProfilingMode;
import com.evolveum.midpoint.schema.constants.MidPointConstants;
import com.evolveum.midpoint.schema.util.LoggingSchemaUtil;
import com.evolveum.midpoint.util.aspect.ProfilingDataManager;
import com.evolveum.midpoint.util.logging.*;
import com.evolveum.midpoint.util.statistics.OperationExecutionLogger;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.bridge.SLF4JBridgeHandler;

import ch.qos.logback.classic.Level;
Expand All @@ -42,8 +44,6 @@
import com.evolveum.midpoint.schema.internals.InternalsConfig;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

public class LoggingConfigurationManager {

Expand All @@ -52,11 +52,11 @@ public class LoggingConfigurationManager {
private final static Trace LOGGER = TraceManager.getTrace(LoggingConfigurationManager.class);

private static final String REQUEST_FILTER_LOGGER_CLASS_NAME = "com.evolveum.midpoint.web.util.MidPointProfilingServletFilter";
private static final String PROFILING_ASPECT_LOGGER = "com.evolveum.midpoint.util.aspect.ProfilingDataManager";
private static final String PROFILING_ASPECT_LOGGER = ProfilingDataManager.class.getName();
private static final String IDM_PROFILE_APPENDER = "IDM_LOG";
private static final String ALT_APPENDER_NAME = "ALT_LOG";
private static final String TRACING_APPENDER_NAME = "TRACING_LOG";
private static final String TRACING_APPENDER_CLASS_NAME = "com.evolveum.midpoint.util.logging.TracingAppender"; // todo replace by class reference?
private static final String TRACING_APPENDER_CLASS_NAME = TracingAppender.class.getName();
private static final LoggingLevelType DEFAULT_PROFILING_LEVEL = LoggingLevelType.INFO;

private static String currentlyUsedVersion = null;
Expand Down Expand Up @@ -133,7 +133,7 @@ public static void configure(LoggingConfigurationType config, String version,
internalLog = baos.toString("UTF8");
} catch (UnsupportedEncodingException e) {
// should never happen
LOGGER.error("Woops?", e);
LOGGER.error("Whoops?", e);
}

if (!StringUtils.isEmpty(internalLog)) {
Expand Down Expand Up @@ -166,10 +166,12 @@ private static String prepareConfiguration(LoggingConfigurationType config, Midp
boolean debug = Boolean.TRUE.equals(config.isDebug());
sb.append("<configuration scan=\"false\" debug=\"").append(debug).append("\">\n");

sb.append("\t<turboFilter class=\"").append(LevelOverrideTurboFilter.class.getName()).append("\"/>\n");

//find and configure ALL logger and bring it to top of turbo stack
for (SubSystemLoggerConfigurationType ss : config.getSubSystemLogger()) {
if ("ALL".contentEquals(ss.getComponent().name())) {
defineTurbo(sb, ss);
defineSubsystemTurboFilter(sb, ss);
}
}

Expand All @@ -189,7 +191,7 @@ private static String prepareConfiguration(LoggingConfigurationType config, Midp
if ("ALL".contentEquals(ss.getComponent().name())) {
continue;
}
defineTurbo(sb, ss);
defineSubsystemTurboFilter(sb, ss);
}

boolean rootAppenderDefined = StringUtils.isNotEmpty(config.getRootLoggerAppender());
Expand Down Expand Up @@ -257,9 +259,9 @@ private static String prepareConfiguration(LoggingConfigurationType config, Midp

if (profilingMode == ProfilingMode.DYNAMIC) {
if (profilingLoggingLevelSet != null) {
OperationExecutionLogger.setGlobalOperationInvocationLevelOverride(toLevel(profilingLoggingLevelSet));
OperationExecutionLogger.setGlobalOperationInvocationLevelOverride(LoggingSchemaUtil.toLevel(profilingLoggingLevelSet));
} else {
OperationExecutionLogger.setGlobalOperationInvocationLevelOverride(toLevel(DEFAULT_PROFILING_LEVEL));
OperationExecutionLogger.setGlobalOperationInvocationLevelOverride(LoggingSchemaUtil.toLevel(DEFAULT_PROFILING_LEVEL));
sb.append("\t<logger name=\"");
sb.append(MidPointConstants.PROFILING_LOGGER_NAME);
sb.append("\" level=\"TRACE\"/>\n");
Expand Down Expand Up @@ -288,19 +290,6 @@ private static String prepareConfiguration(LoggingConfigurationType config, Midp
return sb.toString();
}

private static Level toLevel(@NotNull LoggingLevelType level) {
switch (level) {
case ALL: return Level.ALL;
case TRACE: return Level.TRACE;
case DEBUG: return Level.DEBUG;
case INFO: return Level.INFO;
case WARN: return Level.WARN;
case ERROR: return Level.ERROR;
case OFF: return Level.OFF;
default: throw new IllegalArgumentException("level: " + level);
}
}

private static void prepareAppenderConfiguration(StringBuilder sb, AppenderConfigurationType appender,
LoggingConfigurationType config, boolean isRoot, boolean createAltForThisAppender,
MidpointConfiguration midpointConfiguration) throws SchemaException {
Expand Down Expand Up @@ -437,9 +426,9 @@ private static void prepareCommonAppenderHeader(StringBuilder sb,
sb.append("\t<appender name=\"").append(appender.getName()).append("\" class=\"").append(appenderClass).append("\">\n");

//Apply profiling appender filter if necessary
if(IDM_PROFILE_APPENDER.equals(appender.getName())){
for(ClassLoggerConfigurationType cs: config.getClassLogger()){
if(REQUEST_FILTER_LOGGER_CLASS_NAME.equals(cs.getPackage()) || PROFILING_ASPECT_LOGGER.endsWith(cs.getPackage())){
if (IDM_PROFILE_APPENDER.equals(appender.getName())) {
for (ClassLoggerConfigurationType cs: config.getClassLogger()) {
if (REQUEST_FILTER_LOGGER_CLASS_NAME.equals(cs.getPackage()) || PROFILING_ASPECT_LOGGER.endsWith(cs.getPackage())) {
LOGGER.debug("Defining ProfilingLogbackFilter to {} appender.", appender.getName());
sb.append(defineProfilingLogbackFilter());
}
Expand Down Expand Up @@ -484,8 +473,10 @@ private static void generateAuditingLogConfig(AuditingConfigurationType auditing
}
}

private static void defineTurbo(StringBuilder sb, SubSystemLoggerConfigurationType ss) {
sb.append("\t<turboFilter class=\"com.evolveum.midpoint.util.logging.MDCLevelTurboFilter\">\n");
// Not very useful these days, as we don't set system loggers in the sysconfig object any more.
// But let's keep this just in case it's needed in the future.
private static void defineSubsystemTurboFilter(StringBuilder sb, SubSystemLoggerConfigurationType ss) {
sb.append("\t<turboFilter class=\"").append(MDCLevelTurboFilter.class.getName()).append("\">\n");
sb.append("\t\t<MDCKey>subsystem</MDCKey>\n");
sb.append("\t\t<MDCValue>");
sb.append(ss.getComponent().name());
Expand All @@ -497,8 +488,8 @@ private static void defineTurbo(StringBuilder sb, SubSystemLoggerConfigurationTy
sb.append("\t</turboFilter>\n");
}

private static String defineProfilingLogbackFilter(){
return ("\t<filter class=\"com.evolveum.midpoint.util.logging.ProfilingLogbackFilter\" />\n");
private static String defineProfilingLogbackFilter() {
return ("\t<filter class=\"" + ProfilingLogbackFilter.class.getName() + "\" />\n");
}

public static void dummy() {
Expand Down
Expand Up @@ -141,6 +141,9 @@ public static <T> T clone(T orig) {
throw new IllegalArgumentException("Cannot clone "+orig+" ("+origClass+")");
}

/**
* @return List that can be freely used.
*/
@Contract("!null -> !null; null -> null")
public static <T> List<T> cloneCollectionMembers(Collection<T> collection) {
if (collection == null) {
Expand All @@ -153,14 +156,6 @@ public static <T> List<T> cloneCollectionMembers(Collection<T> collection) {
return clonedCollection;
}

public static <T> List<T> cloneListMembers(List<T> list) {
List<T> clonedCollection = new ArrayList<>(list.size());
for (T element : list) {
clonedCollection.add(clone(element));
}
return clonedCollection;
}

private static PolyString clonePolyString(PolyString orig){
if (orig == null){
return null;
Expand Down
Expand Up @@ -118,6 +118,10 @@ private static boolean isMatchingType(Class<?> expectedClass, Class<?> actualCla
return false;
}

public static XMLGregorianCalendar createXMLGregorianCalendar() {
return createXMLGregorianCalendar(System.currentTimeMillis());
}

public static XMLGregorianCalendar createXMLGregorianCalendar(Long timeInMillis) {
if (timeInMillis == null) {
return null;
Expand Down Expand Up @@ -158,6 +162,7 @@ public static ZonedDateTime toZonedDateTime(XMLGregorianCalendar xcal) {
}

// in some environments, XMLGregorianCalendar.clone does not work
@Contract("null -> null; !null -> !null")
public static XMLGregorianCalendar createXMLGregorianCalendar(XMLGregorianCalendar cal) {
if (cal == null) {
return null;
Expand Down
4 changes: 4 additions & 0 deletions infra/schema/pom.xml
Expand Up @@ -125,6 +125,10 @@
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

<!-- Testing dependecies -->
<dependency>
Expand Down
Expand Up @@ -21,19 +21,21 @@
import com.evolveum.midpoint.prism.schema.PrismSchema;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.logging.LoggingLevelOverrideConfiguration;
import com.evolveum.midpoint.util.logging.LoggingLevelOverrideConfiguration.Entry;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TraceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TracingLevelType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TracingProfileType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.xml.namespace.QName;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

import static com.evolveum.midpoint.schema.util.LoggingSchemaUtil.toLevel;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;

/**
Expand All @@ -44,10 +46,13 @@ public class CompiledTracingProfile implements Serializable {
private static final Trace LOGGER = TraceManager.getTrace(CompiledTracingProfile.class);

@NotNull private final TracingProfileType definition;
@Nullable private final LoggingLevelOverrideConfiguration loggingLevelOverrideConfiguration;

private final Map<Class<? extends TraceType>, TracingLevelType> levelMap = new HashMap<>();

private CompiledTracingProfile(@NotNull TracingProfileType definition) {
this.definition = definition;
this.loggingLevelOverrideConfiguration = compileLevelOverrideConfiguration(definition.getLoggingOverride());
}

public static CompiledTracingProfile create(TracingProfileType resolvedProfile, PrismContext prismContext) {
Expand Down Expand Up @@ -128,4 +133,21 @@ public boolean isLevel(@NotNull Class<? extends TraceType> traceClass, @NotNull
public boolean isCollectingLogEntries() {
return Boolean.TRUE.equals(definition.isCollectLogEntries());
}

public LoggingLevelOverrideConfiguration getLoggingLevelOverrideConfiguration() {
return loggingLevelOverrideConfiguration;
}

private LoggingLevelOverrideConfiguration compileLevelOverrideConfiguration(LoggingOverrideType override) {
if (override == null) {
return null;
}
LoggingLevelOverrideConfiguration rv = new LoggingLevelOverrideConfiguration();
for (ClassLoggerLevelOverrideType levelOverride : override.getLevelOverride()) {
rv.addEntry(new Entry(new HashSet<>(levelOverride.getLogger()), toLevel(levelOverride.getLevel())));
}
return rv;
}


}
Expand Up @@ -157,6 +157,7 @@ public class OperationResult implements Serializable, DebugDumpable, ShortDumpab

private CompiledTracingProfile tracingProfile; // NOT SERIALIZED
private boolean collectingLogEntries; // NOT SERIALIZED
private boolean startedLoggingOverride; // NOT SERIALIZED

private final List<TraceType> traces = new ArrayList<>();

Expand Down Expand Up @@ -234,6 +235,7 @@ public OperationResultBuilder subresult(String operation) {
OperationResult subresult = new OperationResult(operation);
subresult.building = true;
subresult.futureParent = this;
subresult.tracingProfile = tracingProfile;
return subresult;
}

Expand Down Expand Up @@ -265,7 +267,12 @@ private static void recordStart(OperationResult result, String operation, Object
result.start = System.currentTimeMillis();
result.collectingLogEntries = result.tracingProfile != null && result.tracingProfile.isCollectingLogEntries();
if (result.collectingLogEntries) {
TracingAppender.startCollectingForCurrentThread(result::appendLoggedEvents);
LoggingLevelOverrideConfiguration loggingOverrideConfiguration = result.tracingProfile.getLoggingLevelOverrideConfiguration();
if (loggingOverrideConfiguration != null && !LevelOverrideTurboFilter.isActive()) {
LevelOverrideTurboFilter.overrideLogging(loggingOverrideConfiguration);
result.startedLoggingOverride = true;
}
TracingAppender.openSink(result::appendLoggedEvents);
}
}

Expand Down Expand Up @@ -295,15 +302,15 @@ public OperationResult createMinorSubresult(String operation) {
return createSubresult(operation, true, true, null); // temporarily profiled todo make this configurable
}

public static OperationResult createProfiled(String operation) {
return createProfiled(operation, new Object[0]);
}

public static OperationResult createProfiled(String operation, Object[] arguments) {
OperationResult result = new OperationResult(operation);
recordStart(result, operation, arguments);
return result;
}
// public static OperationResult createProfiled(String operation) {
// return createProfiled(operation, new Object[0]);
// }
//
// public static OperationResult createProfiled(String operation, Object[] arguments) {
// OperationResult result = new OperationResult(operation);
// recordStart(result, operation, arguments);
// return result;
// }

private OperationResult createSubresult(String operation, boolean minor, boolean profiled, Object[] arguments) {
OperationResult subresult = new OperationResult(operation);
Expand All @@ -328,10 +335,14 @@ public void recordEnd() {
invocationRecord.processReturnValue(returnValue);
invocationRecord.afterCall();
microseconds = invocationRecord.getElapsedTimeMicros();
TracingAppender.stopCollectingForCurrentThread();
TracingAppender.closeCurrentSink();
invocationRecord = null;
}
end = System.currentTimeMillis();
if (startedLoggingOverride) {
LevelOverrideTurboFilter.cancelLoggingOverride();
startedLoggingOverride = false;
}
}

/**
Expand Down Expand Up @@ -870,13 +881,6 @@ public void addTrace(TraceType trace) {
traces.add(trace);
}

public void startTracing(@NotNull CompiledTracingProfile profile) {
this.tracingProfile = profile;
if (invocationId == null) {
recordStart(this, operation, createArguments());
}
}

@Override
public OperationResultBuilder tracingProfile(CompiledTracingProfile profile) {
this.tracingProfile = profile;
Expand Down Expand Up @@ -928,6 +932,10 @@ public TracingLevelType getTracingLevel(Class<? extends TraceType> traceClass) {
}
}

public void clearTracingProfile() {
tracingProfile = null;
}

public static class PreviewResult {
public final OperationResultStatus status;
public final String message;
Expand Down
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2010-2019 Evolveum
*
* 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 com.evolveum.midpoint.schema.util;

import ch.qos.logback.classic.Level;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LoggingLevelType;
import org.jetbrains.annotations.NotNull;

/**
*
*/
public class LoggingSchemaUtil {

public static Level toLevel(@NotNull LoggingLevelType level) {
switch (level) {
case ALL: return Level.ALL;
case TRACE: return Level.TRACE;
case DEBUG: return Level.DEBUG;
case INFO: return Level.INFO;
case WARN: return Level.WARN;
case ERROR: return Level.ERROR;
case OFF: return Level.OFF;
default: throw new IllegalArgumentException("level: " + level);
}
}
}

0 comments on commit cc87a81

Please sign in to comment.