-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add options for change log level (#735)
- Loading branch information
Showing
6 changed files
with
145 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package jadx.cli; | ||
|
||
import org.slf4j.LoggerFactory; | ||
|
||
import com.beust.jcommander.IStringConverter; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
|
||
import jadx.api.JadxDecompiler; | ||
|
||
public class LogHelper { | ||
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(LogHelper.class); | ||
|
||
public enum LogLevelEnum { | ||
QUIET(Level.OFF), | ||
PROGRESS(Level.OFF), | ||
ERROR(Level.ERROR), | ||
WARN(Level.WARN), | ||
INFO(Level.INFO), | ||
DEBUG(Level.DEBUG); | ||
|
||
private final Level level; | ||
|
||
LogLevelEnum(Level level) { | ||
this.level = level; | ||
} | ||
|
||
public Level getLevel() { | ||
return level; | ||
} | ||
} | ||
|
||
public static void setLogLevelFromArgs(JadxCLIArgs args) { | ||
if (isCustomLogConfig()) { | ||
return; | ||
} | ||
LogLevelEnum logLevel = args.logLevel; | ||
if (args.quiet) { | ||
logLevel = LogLevelEnum.QUIET; | ||
} else if (args.verbose) { | ||
logLevel = LogLevelEnum.DEBUG; | ||
} | ||
|
||
applyLogLevel(logLevel); | ||
} | ||
|
||
public static void applyLogLevel(LogLevelEnum logLevel) { | ||
Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); | ||
rootLogger.setLevel(logLevel.getLevel()); | ||
|
||
if (logLevel != LogLevelEnum.QUIET) { | ||
// show progress for all levels except quiet | ||
setLevelForClass(JadxCLI.class, Level.INFO); | ||
setLevelForClass(JadxDecompiler.class, Level.INFO); | ||
} | ||
} | ||
|
||
private static void setLevelForClass(Class<?> cls, Level level) { | ||
((Logger) LoggerFactory.getLogger(cls)).setLevel(level); | ||
} | ||
|
||
/** | ||
* Try to detect if user provide custom logback config via -Dlogback.configurationFile= | ||
*/ | ||
private static boolean isCustomLogConfig() { | ||
try { | ||
String logbackConfig = System.getProperty("logback.configurationFile"); | ||
if (logbackConfig == null) { | ||
return false; | ||
} | ||
LOG.debug("Use custom log config: {}", logbackConfig); | ||
return true; | ||
} catch (Exception e) { | ||
LOG.error("Failed to detect custom log config", e); | ||
} | ||
return false; | ||
} | ||
|
||
public static class LogLevelConverter implements IStringConverter<LogLevelEnum> { | ||
|
||
@Override | ||
public LogLevelEnum convert(String value) { | ||
try { | ||
return LogLevelEnum.valueOf(value.toUpperCase()); | ||
} catch (Exception e) { | ||
throw new IllegalArgumentException( | ||
'\'' + value + "' is unknown log level, possible values are " | ||
+ JadxCLIArgs.enumValuesString(LogLevelEnum.values())); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | ||
<level>INFO</level> | ||
</filter> | ||
<encoder> | ||
<pattern>%-5level - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="DEBUG"> | ||
<root level="INFO"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
</configuration> |