Skip to content

Commit

Permalink
Closes RPTools#850 - Add log.trace to log macro group
Browse files Browse the repository at this point in the history
Add log.trace(message) to macro function log group.

Signed-off-by: JamzTheMan <[email protected]>
  • Loading branch information
JamzTheMan committed Oct 27, 2019
1 parent 8046a6a commit bd910ba
Showing 1 changed file with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

/** Log functions to dynamically set log levels, log configurations, and log messages from macros */
public class LogFunctions extends AbstractFunction {

private static final Logger log = LogManager.getLogger(LogFunctions.class);
private static final Logger logger = LogManager.getLogger("macro-logger");

Expand All @@ -48,7 +49,8 @@ private LogFunctions() {
"log.error",
"log.warn",
"log.info",
"log.debug");
"log.debug",
"log.trace");
}

public static LogFunctions getInstance() {
Expand All @@ -59,20 +61,22 @@ public static LogFunctions getInstance() {
public Object childEvaluate(Parser parser, String functionName, List<Object> parameters)
throws ParserException {

if (!MapTool.getParser().isMacroPathTrusted())
if (!MapTool.getParser().isMacroPathTrusted()) {
throw new ParserException(I18N.getText("macro.function.general.noPerm", functionName));
}

if (functionName.equalsIgnoreCase("log.getLoggers"))
if (functionName.equalsIgnoreCase("log.getLoggers")) {
return getLoggers(functionName, parameters);
else if (functionName.equalsIgnoreCase("log.setLevel"))
} else if (functionName.equalsIgnoreCase("log.setLevel")) {
return setLogLevel(functionName, parameters);
else if (functionName.equalsIgnoreCase("log.setPattern"))
} else if (functionName.equalsIgnoreCase("log.setPattern")) {
return setPattern(functionName, parameters);
else if (functionName.toLowerCase().startsWith("log."))
} else if (functionName.toLowerCase().startsWith("log.")) {
return logMessage(functionName, parameters);
else
} else {
throw new ParserException(
I18N.getText("macro.function.general.unknownFunction", functionName));
}
}

/**
Expand All @@ -89,12 +93,13 @@ private Object getLoggers(String functionName, List<Object> parameters) throws P
LoggerContext logContext = (LoggerContext) LogManager.getContext(false);
List<LoggerResponse> loggerLevels = new ArrayList<>();

for (Logger logger : logContext.getLoggers())
for (Logger logger : logContext.getLoggers()) {
try {
loggerLevels.add(new LoggerResponse(logger.getName(), logger.getLevel().name()));
} catch (IOException ioe) {
log.error("Unable to get loggers from LogManager!", ioe);
}
}

Gson gson = new Gson();
String jsonResponse = gson.toJson(loggerLevels);
Expand All @@ -117,7 +122,9 @@ private Object setLogLevel(String functionName, List<Object> parameters) throws

// Convert the parameter string to a logger Level and return false if no match is found
Level newLevel = Level.getLevel(parameters.get(1).toString().toUpperCase());
if (newLevel == null) return BigDecimal.ZERO;
if (newLevel == null) {
return BigDecimal.ZERO;
}

Configurator.setLevel(loggerName, newLevel);

Expand All @@ -129,14 +136,22 @@ private Object logMessage(String functionName, List<Object> parameters) throws P

String logMessage = parameters.get(0).toString();

if (functionName.equalsIgnoreCase("log.fatal")) logger.fatal(logMessage);
else if (functionName.equalsIgnoreCase("log.error")) logger.error(logMessage);
else if (functionName.equalsIgnoreCase("log.warn")) logger.warn(logMessage);
else if (functionName.equalsIgnoreCase("log.info")) logger.info(logMessage);
else if (functionName.equalsIgnoreCase("log.debug")) logger.debug(logMessage);
else
if (functionName.equalsIgnoreCase("log.fatal")) {
logger.fatal(logMessage);
} else if (functionName.equalsIgnoreCase("log.error")) {
logger.error(logMessage);
} else if (functionName.equalsIgnoreCase("log.warn")) {
logger.warn(logMessage);
} else if (functionName.equalsIgnoreCase("log.info")) {
logger.info(logMessage);
} else if (functionName.equalsIgnoreCase("log.debug")) {
logger.debug(logMessage);
} else if (functionName.equalsIgnoreCase("log.trace")) {
logger.trace(logMessage);
} else {
throw new ParserException(
I18N.getText("macro.function.general.unknownFunction", functionName));
}

return "";
}
Expand Down Expand Up @@ -201,28 +216,32 @@ private void checkParameters(String functionName, List<Object> parameters, int m
throws ParserException {

if (min == max) {
if (parameters.size() != max)
if (parameters.size() != max) {
throw new ParserException(
I18N.getText(
"macro.function.general.wrongNumParam", functionName, max, parameters.size()));
}

} else {
if (parameters.size() < min)
if (parameters.size() < min) {
throw new ParserException(
I18N.getText(
"macro.function.general.notEnoughParam", functionName, min, parameters.size()));
}

if (parameters.size() > max)
if (parameters.size() > max) {
throw new ParserException(
I18N.getText(
"macro.function.general.tooManyParam", functionName, max, parameters.size()));
}
}
}

/*
* A POJO to hold an Logger Config to marshal as a nice JSON object
*/
private final class LoggerResponse {

private final String name;
private final String level;

Expand Down

0 comments on commit bd910ba

Please sign in to comment.