support set log level via maven plugin

This commit is contained in:
金戟 2020-12-14 21:25:51 +08:00
parent 16ec3b9444
commit abb6d04c16
3 changed files with 70 additions and 19 deletions

View File

@ -2,7 +2,6 @@ package com.alibaba.testable.agent;
import com.alibaba.testable.agent.transformer.TestableClassTransformer;
import com.alibaba.testable.core.util.LogUtil;
import com.alibaba.testable.core.model.MockDiagnose;
import java.lang.instrument.Instrumentation;
@ -15,6 +14,8 @@ public class PreMain {
private static final String AND = "&";
private static final String DEBUG = "debug";
private static final String VERBOSE = "verbose";
private static final String LOG_LEVEL = "logLevel";
private static final String EQUAL = "=";
public static void premain(String agentArgs, Instrumentation inst) {
parseArgs(agentArgs);
@ -26,12 +27,28 @@ public class PreMain {
return;
}
for (String a : args.split(AND)) {
if (a.equals(DEBUG)) {
LogUtil.setDefaultLevel(LogUtil.LEVEL_DIAGNOSE);
} else if (a.equals(VERBOSE)) {
LogUtil.setDefaultLevel(LogUtil.LEVEL_VERBOSE);
int i = a.indexOf(EQUAL);
if (i > 0) {
String k = a.substring(0, i);
String v = a.substring(i + 1);
if (k.equals(LOG_LEVEL)) {
setLogLevel(v);
}
} else {
setLogLevel(a);
}
}
}
private static boolean setLogLevel(String level) {
if (level.equals(DEBUG)) {
LogUtil.setDefaultLevel(LogUtil.LogLevel.LEVEL_DIAGNOSE);
return true;
} else if (level.equals(VERBOSE)) {
LogUtil.setDefaultLevel(LogUtil.LogLevel.LEVEL_VERBOSE);
return true;
}
return false;
}
}

View File

@ -5,43 +5,62 @@ package com.alibaba.testable.core.util;
*/
public class LogUtil {
public static final int LEVEL_ERROR = 0;
public static final int LEVEL_WARN = 1;
public static final int LEVEL_DIAGNOSE = 2;
public static final int LEVEL_VERBOSE = 3;
public enum LogLevel {
/**
* Mute
*/
LEVEL_MUTE(0),
/**
* Warn only
*/
LEVEL_WARN(1),
/**
* Show diagnose messages
*/
LEVEL_DIAGNOSE(2),
/**
* Show detail progress logs
*/
LEVEL_VERBOSE(3);
private static int defaultLogLevel = LEVEL_WARN;
private static int level;
int level;
LogLevel(int l) {
level = l;
}
}
private static LogLevel defaultLogLevel = LogLevel.LEVEL_WARN;
private static LogLevel currentLogLevel;
public static void verbose(String msg, Object... args) {
if (level >= LEVEL_VERBOSE) {
if (currentLogLevel.level >= LogLevel.LEVEL_VERBOSE.level) {
System.out.println(String.format("[VERBOSE] " + msg, args));
}
}
public static void diagnose(String msg, Object... args) {
if (level >= LEVEL_DIAGNOSE) {
if (currentLogLevel.level >= LogLevel.LEVEL_DIAGNOSE.level) {
System.out.println(String.format("[DIAGNOSE] " + msg, args));
}
}
public static void warn(String msg, Object... args) {
if (level >= LEVEL_WARN) {
if (currentLogLevel.level >= LogLevel.LEVEL_WARN.level) {
System.out.println(String.format("[WARN] " + msg, args));
}
}
public static void enableDiagnose(boolean enable) {
level = enable ? LEVEL_DIAGNOSE : LEVEL_ERROR;
currentLogLevel = enable ? LogLevel.LEVEL_DIAGNOSE : LogLevel.LEVEL_MUTE;
}
public static void setDefaultLevel(int level) {
public static void setDefaultLevel(LogLevel level) {
defaultLogLevel = level;
resetLogLevel();
}
public static void resetLogLevel() {
level = defaultLogLevel;
currentLogLevel = defaultLogLevel;
}
}

View File

@ -24,13 +24,21 @@ public class TestableMojo extends AbstractMojo
/**
* Maven project.
*/
@Parameter(property = "project", readonly = true)
@Parameter(property = "project", required = true, readonly = true)
private MavenProject project;
/**
* Map of plugin artifacts.
*/
@Parameter(property = "plugin.artifactMap", required = true, readonly = true)
private Map<String, Artifact> pluginArtifactMap;
/**
* JavaAgent log level (mute/debug/verbose)
*/
@Parameter
private String logLevel;
/**
* Name of the Testable Agent artifact.
*/
@ -57,9 +65,16 @@ public class TestableMojo extends AbstractMojo
getLog().error("failed to fetch project properties");
return;
}
String extraArgs = "";
if (!logLevel.isEmpty()) {
extraArgs += logLevel;
}
final String oldArgs = projectProperties.getProperty(testArgsPropertyKey);
final String newArgs = (oldArgs == null) ? getAgentJarArgs().trim() : (oldArgs + getAgentJarArgs());
String newArgs = (oldArgs == null) ? getAgentJarArgs().trim() : (oldArgs + getAgentJarArgs());
getLog().info(testArgsPropertyKey + " set to " + newArgs);
if (!extraArgs.isEmpty()) {
newArgs += ("=" + extraArgs);
}
projectProperties.setProperty(testArgsPropertyKey, newArgs);
}