mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-02-04 00:30:52 +08:00
support set log level via maven plugin
This commit is contained in:
parent
16ec3b9444
commit
abb6d04c16
@ -2,7 +2,6 @@ package com.alibaba.testable.agent;
|
|||||||
|
|
||||||
import com.alibaba.testable.agent.transformer.TestableClassTransformer;
|
import com.alibaba.testable.agent.transformer.TestableClassTransformer;
|
||||||
import com.alibaba.testable.core.util.LogUtil;
|
import com.alibaba.testable.core.util.LogUtil;
|
||||||
import com.alibaba.testable.core.model.MockDiagnose;
|
|
||||||
|
|
||||||
import java.lang.instrument.Instrumentation;
|
import java.lang.instrument.Instrumentation;
|
||||||
|
|
||||||
@ -15,6 +14,8 @@ public class PreMain {
|
|||||||
private static final String AND = "&";
|
private static final String AND = "&";
|
||||||
private static final String DEBUG = "debug";
|
private static final String DEBUG = "debug";
|
||||||
private static final String VERBOSE = "verbose";
|
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) {
|
public static void premain(String agentArgs, Instrumentation inst) {
|
||||||
parseArgs(agentArgs);
|
parseArgs(agentArgs);
|
||||||
@ -26,12 +27,28 @@ public class PreMain {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (String a : args.split(AND)) {
|
for (String a : args.split(AND)) {
|
||||||
if (a.equals(DEBUG)) {
|
int i = a.indexOf(EQUAL);
|
||||||
LogUtil.setDefaultLevel(LogUtil.LEVEL_DIAGNOSE);
|
if (i > 0) {
|
||||||
} else if (a.equals(VERBOSE)) {
|
String k = a.substring(0, i);
|
||||||
LogUtil.setDefaultLevel(LogUtil.LEVEL_VERBOSE);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,43 +5,62 @@ package com.alibaba.testable.core.util;
|
|||||||
*/
|
*/
|
||||||
public class LogUtil {
|
public class LogUtil {
|
||||||
|
|
||||||
public static final int LEVEL_ERROR = 0;
|
public enum LogLevel {
|
||||||
public static final int LEVEL_WARN = 1;
|
/**
|
||||||
public static final int LEVEL_DIAGNOSE = 2;
|
* Mute
|
||||||
public static final int LEVEL_VERBOSE = 3;
|
*/
|
||||||
|
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;
|
int level;
|
||||||
private static 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) {
|
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));
|
System.out.println(String.format("[VERBOSE] " + msg, args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void diagnose(String msg, Object... 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));
|
System.out.println(String.format("[DIAGNOSE] " + msg, args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void warn(String msg, Object... 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));
|
System.out.println(String.format("[WARN] " + msg, args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void enableDiagnose(boolean enable) {
|
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;
|
defaultLogLevel = level;
|
||||||
resetLogLevel();
|
resetLogLevel();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void resetLogLevel() {
|
public static void resetLogLevel() {
|
||||||
level = defaultLogLevel;
|
currentLogLevel = defaultLogLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,13 +24,21 @@ public class TestableMojo extends AbstractMojo
|
|||||||
/**
|
/**
|
||||||
* Maven project.
|
* Maven project.
|
||||||
*/
|
*/
|
||||||
@Parameter(property = "project", readonly = true)
|
@Parameter(property = "project", required = true, readonly = true)
|
||||||
private MavenProject project;
|
private MavenProject project;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map of plugin artifacts.
|
* Map of plugin artifacts.
|
||||||
*/
|
*/
|
||||||
@Parameter(property = "plugin.artifactMap", required = true, readonly = true)
|
@Parameter(property = "plugin.artifactMap", required = true, readonly = true)
|
||||||
private Map<String, Artifact> pluginArtifactMap;
|
private Map<String, Artifact> pluginArtifactMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JavaAgent log level (mute/debug/verbose)
|
||||||
|
*/
|
||||||
|
@Parameter
|
||||||
|
private String logLevel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Name of the Testable Agent artifact.
|
* Name of the Testable Agent artifact.
|
||||||
*/
|
*/
|
||||||
@ -57,9 +65,16 @@ public class TestableMojo extends AbstractMojo
|
|||||||
getLog().error("failed to fetch project properties");
|
getLog().error("failed to fetch project properties");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
String extraArgs = "";
|
||||||
|
if (!logLevel.isEmpty()) {
|
||||||
|
extraArgs += logLevel;
|
||||||
|
}
|
||||||
final String oldArgs = projectProperties.getProperty(testArgsPropertyKey);
|
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);
|
getLog().info(testArgsPropertyKey + " set to " + newArgs);
|
||||||
|
if (!extraArgs.isEmpty()) {
|
||||||
|
newArgs += ("=" + extraArgs);
|
||||||
|
}
|
||||||
projectProperties.setProperty(testArgsPropertyKey, newArgs);
|
projectProperties.setProperty(testArgsPropertyKey, newArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user