From abb6d04c168823f1697239f34a4dbbe337137a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Mon, 14 Dec 2020 21:25:51 +0800 Subject: [PATCH] support set log level via maven plugin --- .../com/alibaba/testable/agent/PreMain.java | 27 +++++++++--- .../alibaba/testable/core/util/LogUtil.java | 43 +++++++++++++------ .../com/alibaba/testable/TestableMojo.java | 19 +++++++- 3 files changed, 70 insertions(+), 19 deletions(-) diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java index fb79769..cf2ea5e 100755 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java @@ -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; + } + } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java index 26cd630..97431cc 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java @@ -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; } } diff --git a/testable-maven-plugin/src/main/java/com/alibaba/testable/TestableMojo.java b/testable-maven-plugin/src/main/java/com/alibaba/testable/TestableMojo.java index 4beca7b..0df71ff 100644 --- a/testable-maven-plugin/src/main/java/com/alibaba/testable/TestableMojo.java +++ b/testable-maven-plugin/src/main/java/com/alibaba/testable/TestableMojo.java @@ -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 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); }