Skip to content

Commit

Permalink
MID-8842 ninja - improved ninja logging
Browse files Browse the repository at this point in the history
(cherry picked from commit 53eff55)
  • Loading branch information
1azyman committed Aug 3, 2023
1 parent acea535 commit 81947bc
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 101 deletions.
49 changes: 29 additions & 20 deletions tools/ninja/src/main/java/com/evolveum/midpoint/ninja/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import com.beust.jcommander.ParameterException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.fusesource.jansi.AnsiConsole;
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.ninja.action.Action;
import com.evolveum.midpoint.ninja.action.ActionResult;
import com.evolveum.midpoint.ninja.action.BaseOptions;
import com.evolveum.midpoint.ninja.impl.Command;
import com.evolveum.midpoint.ninja.impl.LogLevel;
import com.evolveum.midpoint.ninja.impl.NinjaContext;
import com.evolveum.midpoint.ninja.util.ConsoleFormat;
import com.evolveum.midpoint.ninja.util.InputParameterException;
Expand Down Expand Up @@ -62,7 +64,7 @@ public void setErr(@NotNull PrintStream err) {
this.err = err;
}

// todo main doesn't return error code non zero if error occurrs, fix this
// todo main doesn't return error code non zero if error occurs, fix this
protected <T> @NotNull MainResult<?> run(String[] args) {
AnsiConsole.systemInstall();

Expand All @@ -71,7 +73,8 @@ public void setErr(@NotNull PrintStream err) {
try {
jc.parse(args);
} catch (ParameterException ex) {
err.println(ex.getMessage());
err.println(ConsoleFormat.formatLogMessage(LogLevel.ERROR, ex.getMessage()));

return MainResult.EMPTY_ERROR;
}

Expand All @@ -82,9 +85,11 @@ public void setErr(@NotNull PrintStream err) {
ConsoleFormat.setBatchMode(base.isBatchMode());

if (base.isVerbose() && base.isSilent()) {
err.println("Cant' use " + BaseOptions.P_VERBOSE + " and " + BaseOptions.P_SILENT
+ " together (verbose and silent)");
err.println(ConsoleFormat.formatLogMessage(
LogLevel.ERROR, "Cant' use " + BaseOptions.P_VERBOSE + " and " + BaseOptions.P_SILENT + " together (verbose and silent)"));

printHelp(jc, parsedCommand);

return MainResult.EMPTY_ERROR;
}

Expand All @@ -103,7 +108,7 @@ public void setErr(@NotNull PrintStream err) {
Action<T, ?> action = Command.createAction(parsedCommand);

if (action == null) {
err.println("Action for command '" + parsedCommand + "' not found");
err.println(ConsoleFormat.formatLogMessage(LogLevel.ERROR, "Action for command '" + parsedCommand + "' not found"));
return MainResult.EMPTY_ERROR;
}

Expand Down Expand Up @@ -131,7 +136,7 @@ public void setErr(@NotNull PrintStream err) {
action.destroy();
}
} catch (InputParameterException ex) {
err.println("ERROR: " + ex.getMessage());
err.println(ConsoleFormat.formatLogMessage(LogLevel.ERROR, ex.getMessage()));

return MainResult.EMPTY_ERROR;
} catch (Exception ex) {
Expand All @@ -151,43 +156,47 @@ private void cleanupResources(BaseOptions opts, NinjaContext context) {
context.close();
}
} catch (Exception ex) {
if (opts.isVerbose()) {
String stack = NinjaUtils.printStackToString(ex);

err.println("Unexpected exception occurred (" + ex.getClass()
+ ") during destroying context. Exception stack trace:\n" + stack);
}
printException("Unexpected exception occurred (" + ex.getClass() + ") during destroying context", ex, opts.isVerbose());
}
}

private void handleException(BaseOptions opts, Exception ex) {
if (!opts.isSilent()) {
err.println("Unexpected exception occurred (" + ex.getClass() + "), reason: " + ex.getMessage());
err.println(ConsoleFormat.formatLogMessage(
LogLevel.ERROR, "Unexpected exception occurred (" + ex.getClass() + "), reason: " + ex.getMessage()));
}

if (opts.isVerbose()) {
String stack = NinjaUtils.printStackToString(ex);

err.println("Exception stack trace:\n" + stack);
err.println(ConsoleFormat.formatLogMessage(LogLevel.DEBUG, "Exception stack trace:\n" + stack));
}
}

private void printVersion(boolean verbose) {
URL url = Main.class.getResource("/version");
if (url == null) {
err.println("Couldn't obtain version");
err.println(ConsoleFormat.formatLogMessage(LogLevel.ERROR, "Couldn't obtain version. Version file not available."));
return;
}

try (InputStream is = url.openStream()) {
String version = IOUtils.toString(is, StandardCharsets.UTF_8).trim();
out.println(version);
} catch (Exception ex) {
err.println("Couldn't obtains version");
if (verbose) {
String stack = NinjaUtils.printStackToString(ex);
err.println("Exception stack trace:\n" + stack);
}
printException("Couldn't obtain version", ex, verbose);
}
}

private void printException(String message, Exception ex, boolean verbose) {
String msg = StringUtils.isNotEmpty(message) ? message + ", reason: " : "";
msg += ex.getMessage();

err.println(ConsoleFormat.formatLogMessage(LogLevel.ERROR, msg));
if (verbose) {
String stack = NinjaUtils.printStackToString(ex);

err.println(ConsoleFormat.formatLogMessage(LogLevel.DEBUG, "Exception stack trace:\n" + stack));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ private boolean validateChangeNumber(List<LabeledString> list, String label, int
boolean equals = Objects.equals(number, Integer.toString(expected));

if (!equals) {
log.error(ConsoleFormat.formatError(
"Database schema change number (" + number + ") doesn't match supported one (" + expected + ") for label "
+ label + "."));
log.error("Database schema change number ({}) doesn't match supported one ({}) for label {}.", number, expected, label);
} else {
log.info("Database schema change number matches supported one (" + expected + ") for label " + label + ".");
log.info("Database schema change number matches supported one ({}) for label {}.", expected, label);
}

return equals;
Expand Down Expand Up @@ -107,12 +105,11 @@ private boolean checkNodesVersion(RepositoryService repository) throws SchemaExc
});

if (versions.isEmpty()) {
log.info(ConsoleFormat.formatWarn("There are zero nodes in cluster to validate current midPoint version."));
log.warn("There are zero nodes in cluster to validate current midPoint version.");

return true;
} else if (versions.size() > 1) {
log.error(ConsoleFormat.formatError(
"There are nodes with different versions of midPoint. Please remove incorrect nodes from cluster."));
log.error("There are nodes with different versions of midPoint. Please remove incorrect nodes from cluster.");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
package com.evolveum.midpoint.ninja.action.upgrade.action;

import java.io.*;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.ninja.action.AbstractRepositorySearchAction;
import com.evolveum.midpoint.ninja.action.upgrade.SkipUpgradeItem;
import com.evolveum.midpoint.ninja.action.upgrade.UpgradeObjectHandler;
import com.evolveum.midpoint.ninja.action.upgrade.UpgradeObjectsConsumerWorker;
import com.evolveum.midpoint.ninja.action.verify.VerificationReporter;
import com.evolveum.midpoint.ninja.impl.LogTarget;
import com.evolveum.midpoint.ninja.impl.NinjaApplicationContextLevel;
import com.evolveum.midpoint.ninja.util.ConsoleFormat;
import com.evolveum.midpoint.ninja.util.NinjaUtils;
import com.evolveum.midpoint.ninja.util.OperationStatus;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;

public class UpgradeObjectsAction extends AbstractRepositorySearchAction<UpgradeObjectsOptions, Void> {

private Map<UUID, Set<SkipUpgradeItem>> skipUpgradeItems;
Expand Down Expand Up @@ -52,7 +51,7 @@ public Void execute() throws Exception {

if (!options.getFiles().isEmpty()) {
if (!options.isSkipUpgradeWarning()) {
log.info(ConsoleFormat.formatWarn("WARNING: File update will remove XML comments and change formatting. Do you wish to proceed? (Y/n)"));
log.warn("File update will remove XML comments and change formatting. Do you wish to proceed? (Y/n)");
String result = NinjaUtils.readInput(input -> StringUtils.isEmpty(input) || input.equalsIgnoreCase("y"));

if (result.trim().equalsIgnoreCase("n")) {
Expand Down
40 changes: 12 additions & 28 deletions tools/ninja/src/main/java/com/evolveum/midpoint/ninja/impl/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.ninja.util.ConsoleFormat;
Expand All @@ -24,23 +23,6 @@ public class Log {

private static final Pattern PATTERN = Pattern.compile("\\{}");

private enum Level {
ERROR, INFO, DEBUG;

public ConsoleFormat.Level toConsoleFormatLevel() {
switch (this) {
case ERROR:
return ConsoleFormat.Level.ERROR;
case INFO:
return ConsoleFormat.Level.INFO;
case DEBUG:
return ConsoleFormat.Level.DEFAULT;
default:
throw new IllegalStateException("Unknown log level: " + this);
}
}
}

private final LogVerbosity level;

private final PrintStream stream;
Expand All @@ -59,27 +41,31 @@ public void error(String message, Exception ex, Object... args) {
message = message + ". Reason: " + ex.getMessage();
}

log(Level.ERROR, message, args);
log(LogLevel.ERROR, message, args);

if (ex != null && level == LogVerbosity.VERBOSE) {
log(Level.DEBUG, "Exception details:\n{}", ex);
log(LogLevel.DEBUG, "Exception details:\n{}", ex);
}
}

public void warn(String message, Object... args) {
log(LogLevel.WARNING, message, args);
}

public void debug(String message, Object... args) {
log(Level.DEBUG, message, args);
log(LogLevel.DEBUG, message, args);
}

public void info(String message, Object... args) {
log(Level.INFO, message, args);
log(LogLevel.INFO, message, args);
}

public void log(Level level, String message, Object... args) {
public void log(LogLevel level, String message, Object... args) {
switch (this.level) {
case SILENT:
return;
case DEFAULT:
if (level == Level.DEBUG) {
if (level == LogLevel.DEBUG) {
return;
}
case VERBOSE:
Expand All @@ -93,16 +79,14 @@ public void log(Level level, String message, Object... args) {
int i = 0;
while (matcher.find()) {
Object arg = args[i++];
if (arg instanceof Exception && level == Level.DEBUG) {
if (arg instanceof Exception && level == LogLevel.DEBUG) {
arg = NinjaUtils.printStackToString((Exception) arg);
}

matcher.appendReplacement(sb, Matcher.quoteReplacement(arg.toString()));
}
matcher.appendTail(sb);

ConsoleFormat.formatMessageWithParameter(level + ": ", sb.toString(), level.toConsoleFormatLevel());

stream.println(level + ": " + sb);
stream.println(ConsoleFormat.formatLogMessage(level, sb.toString()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2010-2023 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.ninja.impl;

import org.fusesource.jansi.Ansi;

public enum LogLevel {

ERROR("ERROR", Ansi.Color.RED),

WARNING("WARNING", Ansi.Color.YELLOW),

INFO("INFO", Ansi.Color.BLUE),

DEBUG("DEBUG", Ansi.Color.DEFAULT);

private final String label;

private final Ansi.Color color;

LogLevel(String label, Ansi.Color color) {
this.label = label;
this.color = color;
}

public String label() {
return label;
}

public Ansi.Color color() {
return color;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.fusesource.jansi.Ansi;

import com.evolveum.midpoint.ninja.action.Action;
import com.evolveum.midpoint.ninja.impl.LogLevel;

/**
* TODO think this through - how to format different messages
Expand All @@ -21,7 +22,7 @@ public enum Level {

ERROR(Ansi.Color.RED);

final Ansi.Color color;
public final Ansi.Color color;

Level(Ansi.Color color) {
this.color = color;
Expand All @@ -40,35 +41,19 @@ public static String formatActionStartMessage(Action action) {
return Ansi.ansi().a("Starting ").fgGreen().a(operation).reset().toString();
}

public static String formatSuccessMessageWithParameter(String message, Object parameter) {
return formatMessageWithParameter(message, parameter, Level.SUCCESS);
}

public static String formatWarnMessageWithParameter(String message, Object parameter) {
return formatMessageWithParameter(message, parameter, Level.WARN);
}

public static String formatErrorMessageWithParameter(String message, Object parameter) {
return formatMessageWithParameter(message, parameter, Level.ERROR);
}

public static String formatInfoMessageWithParameter(String message, Object parameter) {
return formatMessageWithParameter(message, parameter, Level.INFO);
}

public static String formatMessageWithParameter(String message, Object parameter, Level level) {
return Ansi.ansi().a(message).fg(level.color).a(parameter).reset().toString();
}

public static String formatWarn(String message) {
return format(message, Level.WARN);
}

public static String formatError(String message) {
return format(message, Level.ERROR);
return Ansi.ansi().a(message).fgBright(level.color).a(parameter).reset().toString();
}

public static String format(String message, Level level) {
return Ansi.ansi().fg(level.color).a(message).reset().toString();
public static String formatLogMessage(LogLevel level, String msg) {
return Ansi.ansi()
.reset()
.a("[").fgBright(level.color()).a(level.label()).reset().a("] ")
.a(msg)
.toString();
}
}

0 comments on commit 81947bc

Please sign in to comment.