Skip to content

Commit

Permalink
Logging > Unify logging
Browse files Browse the repository at this point in the history
  • Loading branch information
gitseti committed Jul 21, 2022
1 parent 7800df1 commit de7fe40
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 34 deletions.
20 changes: 11 additions & 9 deletions src/main/java/com/hivemq/cli/commands/cli/PublishCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

package com.hivemq.cli.commands.cli;

import com.google.common.base.Throwables;
import com.hivemq.cli.MqttCLIMain;
import com.hivemq.cli.commands.options.ConnectOptions;
import com.hivemq.cli.commands.options.DebugOptions;
import com.hivemq.cli.commands.options.PublishOptions;
import com.hivemq.cli.mqtt.MqttClientExecutor;
import com.hivemq.cli.utils.LoggerUtils;
import com.hivemq.client.mqtt.MqttClient;
import com.hivemq.client.mqtt.exceptions.ConnectionFailedException;
import org.jetbrains.annotations.NotNull;
import org.tinylog.Logger;
import picocli.CommandLine;
Expand Down Expand Up @@ -82,16 +80,20 @@ public Integer call() {
connectOptions.setDefaultOptions();
connectOptions.logUnusedOptions();
publishOptions.logUnusedOptions(connectOptions.getVersion());
publishOptions.arrangeQosToMatchTopics();

final MqttClient client;
try {
publishOptions.arrangeQosToMatchTopics();
final MqttClient client = mqttClientExecutor.connect(connectOptions, null);
mqttClientExecutor.publish(client, publishOptions);
} catch (final ConnectionFailedException cex) {
Logger.error(cex, "Unable to connect: {}", cex.getCause().getMessage());
client = mqttClientExecutor.connect(connectOptions, null);
} catch (final Exception exception) {
LoggerUtils.logCommandError("Unable to connect", exception, debugOptions);
return 1;
} catch (final Exception ex) {
Logger.error(ex, "Unable to publish: {}", Throwables.getRootCause(ex).getMessage());
}

try {
mqttClientExecutor.publish(client, publishOptions);
} catch (final Exception exception) {
LoggerUtils.logCommandError("Unable to publish", exception, debugOptions);
return 1;
}

Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/hivemq/cli/commands/cli/SubscribeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.hivemq.cli.commands.cli;

import com.google.common.base.Throwables;
import com.hivemq.cli.MqttCLIMain;
import com.hivemq.cli.commands.options.ConnectOptions;
import com.hivemq.cli.commands.options.DebugOptions;
Expand Down Expand Up @@ -93,27 +92,30 @@ public Integer call() {
connectOptions.logUnusedOptions();
subscribeOptions.setDefaultOptions();
subscribeOptions.logUnusedOptions(connectOptions.getVersion());
subscribeOptions.arrangeQosToMatchTopics();

if (subscribeOptions.isOutputFileInvalid(subscribeOptions.getOutputFile())) {
return 1;
}

try {
subscribeOptions.arrangeQosToMatchTopics();
subscribeClient = mqttClientExecutor.connect(connectOptions, subscribeOptions);
mqttClientExecutor.subscribe(subscribeClient, subscribeOptions);
} catch (final ConnectionFailedException cex) {
Logger.error(cex, "Unable to connect: {}",cex.getCause().getMessage());
} catch (final Exception exception) {
LoggerUtils.logCommandError("Unable to connect", exception, debugOptions);
return 1;
} catch (final Exception ex) {
Logger.error(ex, "Unable to subscribe: {}", Throwables.getRootCause(ex).getMessage());
}

try {
mqttClientExecutor.subscribe(subscribeClient, subscribeOptions);
} catch (final ConnectionFailedException exception) {
LoggerUtils.logCommandError("Unable to subscribe", exception, debugOptions);
return 1;
}

try {
stay();
} catch (final InterruptedException ex) {
Logger.error(ex, Throwables.getRootCause(ex).getMessage());
} catch (final InterruptedException exception) {
LoggerUtils.logCommandError("Unable to stay", exception, debugOptions);
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.hivemq.cli.commands.shell;

import org.jetbrains.annotations.NotNull;
import picocli.CommandLine;

import javax.inject.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.hivemq.cli.commands.options.DisconnectOptions;
import com.hivemq.cli.mqtt.ClientKey;
import com.hivemq.cli.mqtt.MqttClientExecutor;
import com.hivemq.cli.utils.LoggerUtils;
import org.jetbrains.annotations.NotNull;
import org.tinylog.Logger;
import picocli.CommandLine;
Expand Down Expand Up @@ -68,7 +69,7 @@ public Integer call() {
mqttClientExecutor.disconnect(contextClient, disconnectOptions);
}
} catch (final Exception ex) {
Logger.error(ex, "Unable to disconnect: {}" ,Throwables.getRootCause(ex).getMessage());
LoggerUtils.logShellError("Unable to disconnect", ex);
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Throwables;
import com.hivemq.cli.commands.options.PublishOptions;
import com.hivemq.cli.mqtt.MqttClientExecutor;
import com.hivemq.cli.utils.LoggerUtils;
import org.jetbrains.annotations.NotNull;
import org.tinylog.Logger;
import picocli.CommandLine;
Expand Down Expand Up @@ -54,12 +55,12 @@ public Integer call() {

if (contextClient != null) {
publishOptions.logUnusedOptions(contextClient.getConfig().getMqttVersion());
publishOptions.arrangeQosToMatchTopics();

try {
publishOptions.arrangeQosToMatchTopics();
mqttClientExecutor.publish(contextClient, publishOptions);
} catch (final Exception ex) {
Logger.error(ex, "Unable to publish: {}", Throwables.getRootCause(ex).getMessage());
LoggerUtils.logShellError("Unable to publish", ex);
return 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.hivemq.cli.commands.options.SubscribeOptions;
import com.hivemq.cli.commands.options.UnsubscribeOptions;
import com.hivemq.cli.mqtt.MqttClientExecutor;
import com.hivemq.cli.utils.LoggerUtils;
import org.jetbrains.annotations.NotNull;
import org.tinylog.Logger;
import picocli.CommandLine;
Expand Down Expand Up @@ -72,6 +73,7 @@ public Integer call() {

subscribeOptions.setDefaultOptions();
subscribeOptions.logUnusedOptions(contextClient.getConfig().getMqttVersion());
subscribeOptions.arrangeQosToMatchTopics();

if (!stay) {
subscribeOptions.setPrintToSTDOUT(false);
Expand All @@ -82,17 +84,17 @@ public Integer call() {
}

try {
subscribeOptions.arrangeQosToMatchTopics();
mqttClientExecutor.subscribe(Objects.requireNonNull(contextClient), subscribeOptions);
} catch (final Exception ex) {
Logger.error(ex, "Unable to subscribe: {}", Throwables.getRootCause(ex).getMessage());
LoggerUtils.logShellError("Unable to subscribe", ex);
return 1;
}

if (stay) {
try {
stay();
} catch (final InterruptedException ex) {
Logger.error(ex, "Interrupted while staying after subscribe: {}",Throwables.getRootCause(ex).getMessage());
LoggerUtils.logShellError("Unable to stay", ex);
return 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Throwables;
import com.hivemq.cli.mqtt.ClientKey;
import com.hivemq.cli.mqtt.MqttClientExecutor;
import com.hivemq.cli.utils.LoggerUtils;
import com.hivemq.client.mqtt.MqttClient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -72,7 +73,7 @@ public Integer call() {
try {
extractKeyFromContextName(contextName);
} catch (final IllegalArgumentException ex) {
Logger.error(ex, "Unable to switch context: {}", Throwables.getRootCause(ex).getMessage());
LoggerUtils.logShellError("Unable to switch context", ex);
return 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Throwables;
import com.hivemq.cli.commands.options.UnsubscribeOptions;
import com.hivemq.cli.mqtt.MqttClientExecutor;
import com.hivemq.cli.utils.LoggerUtils;
import org.jetbrains.annotations.NotNull;
import org.tinylog.Logger;
import picocli.CommandLine;
Expand Down Expand Up @@ -64,7 +65,7 @@ public Integer call() {
try {
mqttClientExecutor.unsubscribe(contextClient, unsubscribeOptions);
} catch (final Exception ex) {
Logger.error(ex, "Unable to unsubscribe: {}", Throwables.getRootCause(ex).getMessage());
LoggerUtils.logShellError("Unable to unsubscribe", ex);
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Throwables;
import com.hivemq.cli.commands.options.ConnectOptions;
import com.hivemq.cli.mqtt.MqttClientExecutor;
import com.hivemq.cli.utils.LoggerUtils;
import com.hivemq.client.mqtt.MqttClient;
import com.hivemq.client.mqtt.exceptions.ConnectionFailedException;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -58,15 +59,13 @@ public Integer call() {
Logger.trace("Command {} ", this);

connectOptions.setDefaultOptions();
connectOptions.logUnusedOptions();

final MqttClient client;
try {
client = mqttClientExecutor.connect(connectOptions);
} catch (final ConnectionFailedException cex) {
Logger.error(cex, "Unable to connect: {}", cex.getCause().getMessage());
return 1;
} catch (final Exception ex) {
Logger.error(ex, "Unable to connect: {}", Throwables.getRootCause(ex).getMessage());
} catch (final Exception exception) {
LoggerUtils.logShellError("Unable to connect", exception);
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.hivemq.cli.commands.options.DisconnectOptions;
import com.hivemq.cli.mqtt.ClientKey;
import com.hivemq.cli.mqtt.MqttClientExecutor;
import com.hivemq.cli.utils.LoggerUtils;
import org.jetbrains.annotations.NotNull;
import org.tinylog.Logger;
import picocli.CommandLine;
Expand Down Expand Up @@ -74,7 +75,7 @@ public Integer call() {
mqttClientExecutor.disconnect(clientKey, disconnectOptions);
}
} catch (final Exception ex) {
Logger.error(ex, "Unable to disconnect: {}", Throwables.getRootCause(ex).getMessage());
LoggerUtils.logShellError("Unable to disconnect", ex);
return 1;
}

Expand Down
34 changes: 33 additions & 1 deletion src/main/java/com/hivemq/cli/utils/LoggerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package com.hivemq.cli.utils;

import com.google.common.base.Throwables;
import com.hivemq.cli.DefaultCLIProperties;
import com.hivemq.cli.MqttCLIMain;
import com.hivemq.cli.commands.options.DebugOptions;
import com.hivemq.client.mqtt.MqttClientConfig;
import com.hivemq.client.mqtt.datatypes.MqttClientIdentifier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tinylog.Level;
import org.tinylog.Logger;
import org.tinylog.configuration.Configuration;

import java.io.File;
Expand Down Expand Up @@ -67,7 +70,11 @@ public static void setupConsoleLogging(final boolean logToLogfile, final @NotNul
// TinyLog configuration
final Map<String, String> configurationMap = new HashMap<String, String>() {{
put("writer1", "console");
put("writer1.format", "{message-only}");
if (logLevel.equals("debug") || logLevel.equals("trace")) {
put("writer1.format", "{message}");
} else {
put("writer1.format", "{message-only}");
}
put("writer1.level", logLevel);
}};

Expand Down Expand Up @@ -101,4 +108,29 @@ public static String getClientPrefix(final @NotNull MqttClientConfig config) {
}
return "Client '" + id + "@" + config.getServerHost() + "'";
}

public static void logCommandError(
final @NotNull String message,
final @NotNull Exception exception,
final @NotNull DebugOptions debugOptions) {
if (debugOptions.isDebug() || debugOptions.isVerbose()) {
Logger.error(exception, message);
} else {
final String exceptionMessage = Throwables.getRootCause(exception).getMessage();
if (exceptionMessage != null) {
Logger.error("{}. {}", message, exceptionMessage);
} else {
Logger.error("{}. Use '-d' option to get more detailed information.", message);
}
}
}

public static void logShellError(final @NotNull String message, final @NotNull Exception exception) {
final String exceptionMessage = Throwables.getRootCause(exception).getMessage();
if (exceptionMessage != null) {
Logger.error(exception, "{}. {}", message, exceptionMessage);
} else {
Logger.error(exception, "{}. Use 'mqtt sh -l' to see more detailed information in the logfile.", message);
}
}
}

0 comments on commit de7fe40

Please sign in to comment.