Skip to content

Commit 9dad4e6

Browse files
authored
Merge pull request #177 from vnandwana/sse
Enhanced logging in KPM package and added logger name to SSE endpoint logs.
2 parents 4757728 + 5d21942 commit 9dad4e6

File tree

6 files changed

+88
-30
lines changed

6 files changed

+88
-30
lines changed

osgi-bundles/bundles/kpm/src/main/java/org/killbill/billing/osgi/bundles/kpm/KPMClient.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@
3333
import org.killbill.billing.plugin.util.http.InvalidRequest;
3434
import org.killbill.billing.plugin.util.http.ResponseFormat;
3535
import org.killbill.commons.utils.Strings;
36+
import org.slf4j.Logger;
37+
import org.slf4j.LoggerFactory;
3638

3739
public class KPMClient extends HttpClient {
3840

41+
private final Logger logger = LoggerFactory.getLogger(KPMClient.class);
42+
3943
public KPMClient(final boolean strictSSL,
4044
final int connectTimeoutMs,
4145
final int requestTimeoutMs) throws GeneralSecurityException {
@@ -44,8 +48,10 @@ public KPMClient(final boolean strictSSL,
4448

4549
@Override
4650
protected Builder getBuilderWithHeaderAndQuery(final String verb, final String url, final Map<String, String> headers, final Map<String, String> queryParams) throws URISyntaxException {
51+
logger.debug("Building request: verb={}, url={}, headers={}, queryParams={}", verb, url, headers, queryParams);
4752
final Builder builder = super.getBuilderWithHeaderAndQuery(verb, url, headers, queryParams);
4853
builder.setHeader("User-Agent", "KillBill/kpm-plugin/1.0");
54+
4955
return builder;
5056
}
5157

@@ -60,8 +66,13 @@ public void download(final String uri, final Path target) throws InvalidRequest,
6066
}
6167

6268
public void download(final String uri, final Map<String, String> headers, final Path target) throws InvalidRequest, IOException, URISyntaxException, InterruptedException {
69+
logger.info("Starting download: uri={}, target={}", uri, target);
6370
try (final InputStream is = doCall(GET, uri, null, Collections.emptyMap(), headers, InputStream.class, ResponseFormat.RAW)) {
6471
Files.copy(is, target, StandardCopyOption.REPLACE_EXISTING);
72+
logger.info("Download completed: uri={}, target={}", uri, target);
73+
} catch (final Exception e) {
74+
logger.error("Download failed: uri={}, target={}, error={}", uri, target, e.getMessage(), e);
75+
throw e;
6576
}
6677
}
6778

@@ -96,6 +107,7 @@ public Path downloadToTempOS(final String url, final Map<String, String> headers
96107
} catch (final Exception e) {
97108
throw new RuntimeException(String.format("Error when GET request to '%s', because: %s", url, e.getMessage()), e);
98109
}
110+
99111
return target;
100112
}
101113

@@ -115,7 +127,7 @@ private Path createTarget(final String... names) {
115127
}
116128
}
117129
} catch (final IOException ex) {
118-
throw new RuntimeException("Cannot create temp file for downloaded file because: " + ex.getMessage());
130+
throw new RuntimeException("Cannot create temp file for downloaded file because: " + ex.getMessage(), ex);
119131
}
120132
}
121133
}

osgi-bundles/bundles/kpm/src/main/java/org/killbill/billing/osgi/bundles/kpm/impl/DefaultPluginInstaller.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public DefaultPluginInstaller(final PluginFileService pluginFileService) {
4747
*/
4848
@Override
4949
public Path install(final Path downloadedFile, final String pluginKey, final String pluginVersion) throws KPMPluginException {
50+
logger.info("Starting installation of plugin: {} version: {}", pluginKey, pluginVersion);
51+
5052
try {
5153
// Make directory
5254
final Path pluginDirectory = pluginFileService.createPluginDirectory(pluginKey, pluginVersion);
@@ -56,17 +58,25 @@ public Path install(final Path downloadedFile, final String pluginKey, final Str
5658
pluginDirectory.resolve(PluginNamingResolver.of(pluginKey, pluginVersion).getPluginJarFileName()),
5759
StandardCopyOption.REPLACE_EXISTING);
5860

61+
logger.info("Copied plugin file to: {}", result);
62+
5963
// Make symlink
6064
pluginFileService.createSymlink(pluginDirectory);
6165

66+
logger.info("Created symlink for plugin: {} version: {}", pluginKey, pluginVersion);
67+
6268
return result;
6369
} catch (final IOException e) {
70+
logger.error("Failed to install plugin: {} version: {} due to error: {}", pluginKey, pluginVersion, e.getMessage(), e);
71+
6472
throw new KPMPluginException(String.format("Unable to install plugin with key: %s", pluginKey), e);
6573
}
6674
}
6775

6876
@Override
6977
public Path uninstall(final String pluginKey, final String pluginVersion) throws KPMPluginException {
78+
logger.info("Starting uninstallation of plugin: {} version: {}", pluginKey, pluginVersion);
79+
7080
try {
7181
final PluginNamingResolver namingResolver = PluginNamingResolver.of(pluginKey, pluginVersion);
7282
// Example value: <bundlesPath>/plugins/java/some-plugins/1.2.3/
@@ -75,10 +85,11 @@ public Path uninstall(final String pluginKey, final String pluginVersion) throws
7585
final Path pluginFile = pluginDirectory.resolve(namingResolver.getPluginJarFileName());
7686
// Example value: <bundlesPath>/plugins/java/some-plugins/
7787
final Path parentOfPluginDir = pluginDirectory.getParent();
78-
logger.debug("#uninstall() key{}-{}. Is Exists? dir:{}, file:{}", pluginKey, pluginVersion, Files.exists(pluginDirectory), Files.exists(pluginFile));
88+
89+
logger.debug("Uninstalling plugin: {} version: {}. Exists? dir: {}, file: {}", pluginKey, pluginVersion, Files.exists(pluginDirectory), Files.exists(pluginFile));
7990

8091
FilesUtils.deleteRecursively(pluginDirectory);
81-
logger.debug("#uninstall() Is still exists after deleteRecursively()? dir: {}, file: {}", Files.exists(pluginDirectory), Files.exists(pluginFile));
92+
logger.info("Deleted plugin directory: {}", pluginDirectory);
8293

8394
final String symlinkName = DefaultPluginFileService.DEFAULT_SYMLINK_NAME;
8495
try (final Stream<Path> stream = Files.list(parentOfPluginDir)) {
@@ -93,19 +104,23 @@ public Path uninstall(final String pluginKey, final String pluginVersion) throws
93104
.filter(path -> !path.getFileName().toString().equals(symlinkName))
94105
.max(Comparator.naturalOrder())
95106
.orElse(null);
96-
logger.debug("nextLatestPluginForKey: {}", nextLatestPluginForKey);
107+
97108
if (nextLatestPluginForKey != null) {
98109
pluginFileService.createSymlink(nextLatestPluginForKey);
110+
logger.info("Updated symlink to point to: {}", nextLatestPluginForKey);
99111
return nextLatestPluginForKey;
100112
} else {
101113
// Removed plugin is the last plugin by key. Delete the pluginKey directory
102114
FilesUtils.deleteIfExists(parentOfPluginDir);
115+
logger.info("Deleted empty plugin directory: {}", parentOfPluginDir);
103116
}
104117
}
105118
} catch (final IOException e) {
106-
final String msg = String.format("Cannot uninstall: %s version: %s, because %s", pluginKey, pluginVersion, e.getMessage());
107-
throw new KPMPluginException(msg, e);
119+
logger.error("Failed to uninstall plugin: {} version: {} due to error: {}", pluginKey, pluginVersion, e.getMessage(), e);
120+
121+
throw new KPMPluginException(String.format("Cannot uninstall: %s version: %s", pluginKey, pluginVersion), e);
108122
}
123+
109124
return null;
110125
}
111126
}

osgi-bundles/bundles/kpm/src/main/java/org/killbill/billing/osgi/bundles/kpm/impl/DefaultPluginManager.java

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void notifyFileSystemChange(final PluginStateChange newState,
100100
final String pluginKey,
101101
final String pluginVersion) {
102102
try {
103-
logger.info("Notifying Kill Bill: state='{}', pluginKey='{}', pluginVersion={}", newState, pluginKey, pluginVersion);
103+
logger.info("Notifying Kill Bill of plugin state change: state='{}', pluginKey='{}', pluginVersion={}", newState, pluginKey, pluginVersion);
104104

105105
killbillApi.getSecurityApi().login(adminUsername, adminPassword);
106106

@@ -110,6 +110,8 @@ private void notifyFileSystemChange(final PluginStateChange newState,
110110
pluginNamingResolver.getPluginName(),
111111
pluginVersion,
112112
null /* Unused */);
113+
114+
logger.info("Successfully notified Kill Bill of plugin state change: pluginKey={}, pluginVersion={}", pluginKey, pluginVersion);
113115
} finally {
114116
killbillApi.getSecurityApi().logout();
115117
}
@@ -118,6 +120,7 @@ private void notifyFileSystemChange(final PluginStateChange newState,
118120
@Override
119121
public GetAvailablePluginsModel getAvailablePlugins(@Nonnull final String kbVersion,
120122
final boolean forceDownload) throws KPMPluginException {
123+
logger.info("Fetching available plugins for Kill Bill version: {}, forceDownload: {}", kbVersion, forceDownload);
121124
final GetAvailablePluginsModel result = new GetAvailablePluginsModel();
122125

123126
final VersionsProvider versionsProvider = availablePluginsComponentsFactory.createVersionsProvider(kbVersion, forceDownload);
@@ -133,14 +136,16 @@ public GetAvailablePluginsModel getAvailablePlugins(@Nonnull final String kbVers
133136
.getPlugins();
134137
plugins.forEach(entry -> result.addPlugins(entry.getPluginKey(), entry.getPluginVersion()));
135138

139+
logger.info("Successfully fetched {} available plugins for Kill Bill version: {}", plugins.size(), kbVersion);
140+
136141
return result;
137142
}
138143

139144
@Override
140145
public void install(@Nonnull final String uri,
141146
@Nonnull final String pluginKey,
142147
@Nonnull final String pluginVersion) throws KPMPluginException {
143-
logger.debug("Installing plugin via URL for key: {}, pluginVersion: {}, uri: {}", pluginKey, pluginVersion, uri);
148+
logger.info("Starting installation of plugin: pluginKey={}, pluginVersion={}, uri={}", pluginKey, pluginVersion, uri);
144149

145150
Path downloadedFile = null;
146151
final PluginNamingResolver namingResolver = PluginNamingResolver.of(pluginKey, pluginVersion, uri);
@@ -161,6 +166,7 @@ public void install(@Nonnull final String uri,
161166

162167
notifyFileSystemChange(PluginStateChange.NEW_VERSION, pluginKey, fixedVersion);
163168

169+
logger.info("Plugin installed successfully: pluginKey={}, pluginVersion={}", pluginKey, pluginVersion);
164170
} catch (final InvalidRequest e) {
165171
String responseInfo = "";
166172
if (e.getResponse() != null) {
@@ -207,8 +213,8 @@ public void install(@Nonnull final String pluginKey,
207213
String artifactId,
208214
String pluginVersion,
209215
final boolean forceDownload) throws KPMPluginException {
210-
logger.info("Install plugin via coordinate. key:{}, kbVersion:{}, version:{}, groupId:{}, artifactId:{}",
211-
pluginKey, kbVersion, pluginVersion, groupId, artifactId);
216+
logger.info("Starting coordinate-based plugin installation: " +
217+
"pluginKey={}, kbVersion={}, version={}, groupId={}, artifactId={}", pluginKey, kbVersion, pluginVersion, groupId, artifactId);
212218

213219
DownloadResult downloadResult = null;
214220
Path installedPath = null;
@@ -230,9 +236,12 @@ public void install(@Nonnull final String pluginKey,
230236
pluginIdentifiersDAO.add(pluginKey, groupId, artifactId, pluginVersion);
231237

232238
notifyFileSystemChange(PluginStateChange.NEW_VERSION, pluginKey, pluginVersion);
233-
logger.info("Plugin key: {} installed successfully via coordinate.", pluginKey);
239+
240+
logger.info("Successfully installed plugin via coordinate: pluginKey={}, version={}", pluginKey, pluginVersion);
234241
} catch (final Exception e) {
235-
logger.error("Error when install pluginKey: '{}' with coordinate. Exception: {}", pluginKey, e.getMessage());
242+
logger.error("Error installing plugin via coordinate: pluginKey={}, version={}, error={}",
243+
pluginKey, pluginVersion, e.getMessage(), e);
244+
236245
// If exception happened, installed file should be deleted.
237246
FilesUtils.deleteIfExists(installedPath);
238247
throw new KPMPluginException(e);
@@ -245,21 +254,30 @@ public void install(@Nonnull final String pluginKey,
245254

246255
@Override
247256
public void uninstall(final String pluginKey, final String version) throws KPMPluginException {
248-
// Uninstall from bundlesPath
249-
final Path nextPluginByKey = pluginInstaller.uninstall(pluginKey, version);
250-
// Update plugin_identifiers.json
251-
final String nextPluginVersion = (nextPluginByKey == null || nextPluginByKey.getFileName() == null) ?
252-
null :
253-
PluginNamingResolver.getVersionFromString(nextPluginByKey.getFileName().toString());
254-
if (Strings.isNullOrEmpty(nextPluginVersion)) {
255-
// Last plugin by pluginKey. Just remove it.
256-
pluginIdentifiersDAO.remove(pluginKey);
257-
} else {
258-
// Replace the version value
259-
pluginIdentifiersDAO.add(pluginKey, nextPluginVersion);
260-
}
257+
logger.info("Starting uninstallation of plugin: pluginKey={}, version={}", pluginKey, version);
261258

262-
// What if notifyFileSystemChange() implementation fails? Like, wrong username/password?
263-
notifyFileSystemChange(PluginStateChange.DISABLED, pluginKey, version);
259+
try {
260+
// Uninstall from bundlesPath
261+
final Path nextPluginByKey = pluginInstaller.uninstall(pluginKey, version);
262+
// Update plugin_identifiers.json
263+
final String nextPluginVersion = (nextPluginByKey == null || nextPluginByKey.getFileName() == null) ?
264+
null :
265+
PluginNamingResolver.getVersionFromString(nextPluginByKey.getFileName().toString());
266+
if (Strings.isNullOrEmpty(nextPluginVersion)) {
267+
// Last plugin by pluginKey. Just remove it.
268+
pluginIdentifiersDAO.remove(pluginKey);
269+
} else {
270+
// Replace the version value
271+
pluginIdentifiersDAO.add(pluginKey, nextPluginVersion);
272+
}
273+
274+
// What if notifyFileSystemChange() implementation fails? Like, wrong username/password?
275+
notifyFileSystemChange(PluginStateChange.DISABLED, pluginKey, version);
276+
277+
logger.info("Successfully uninstalled plugin: pluginKey={}, version={}", pluginKey, version);
278+
} catch (final Exception e) {
279+
logger.error("Error uninstalling plugin: pluginKey={}, version={}, error={}", pluginKey, version, e.getMessage(), e);
280+
throw new KPMPluginException("Uninstallation failed for plugin " + pluginKey, e);
281+
}
264282
}
265283
}

osgi-bundles/bundles/logger/src/main/java/org/killbill/billing/osgi/bundles/logger/KillbillLogWriter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ public KillbillLogWriter(final LogEntriesManager logEntriesManager, final Killbi
5252
public void log(final ServiceReference serviceReference, final int level, final String message, final Throwable exception) {
5353
final Bundle bundle = serviceReference == null ? null : serviceReference.getBundle();
5454

55+
final String loggerName = message.split("; ")[0];
56+
5557
// Forward the log to HTTP consumers
56-
logEntriesManager.recordEvent(new LogEntryJson(bundle, level, message, exception));
58+
logEntriesManager.recordEvent(new LogEntryJson(bundle, level, loggerName, message, exception));
5759

5860
if (serviceReference != null && "true".equals(serviceReference.getProperty("KILL_BILL_ROOT_LOGGING"))) {
5961
// LogEntry comes from Logback already (see OSGIAppender), ignore

osgi-bundles/bundles/logger/src/main/java/org/killbill/billing/osgi/bundles/logger/LogEntryJson.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public class LogEntryJson {
3434
private final UUID id;
3535
private final String level;
3636
private final String name;
37+
private final String logger;
3738
private final String message;
3839
private final Long time;
3940

40-
public LogEntryJson(final Bundle bundle, final int intLevel, final String message, final Throwable exception) {
41+
public LogEntryJson(final Bundle bundle, final int intLevel, final String loggerName, final String message, final Throwable exception) {
4142
id = UUID.randomUUID();
4243

4344
if (intLevel == LogService.LOG_ERROR) {
@@ -64,10 +65,15 @@ public LogEntryJson(final Bundle bundle, final int intLevel, final String messag
6465
name = bundle.getSymbolicName();
6566
}
6667

68+
this.logger = loggerName;
6769
this.message = message;
6870
this.time = System.currentTimeMillis();
6971
}
7072

73+
public String getLogger() {
74+
return logger;
75+
}
76+
7177
public UUID getId() {
7278
return id;
7379
}
@@ -98,6 +104,7 @@ public String toString() {
98104
sb.append("\"id\":\"").append(id).append("\"");
99105
sb.append(", \"level\":\"").append(level).append("\"");
100106
sb.append(", \"name\":\"").append(name).append("\"");
107+
sb.append(", \"logger\":\"").append(logger).append("\"");
101108
sb.append(", \"message\":\"").append(message).append("\"");
102109
sb.append(", \"time\":\"").append(time).append("\"");
103110
sb.append('}');

osgi/src/main/java/org/killbill/billing/osgi/OSGIAppender.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ protected void append(final ILoggingEvent eventObject) {
8181
}
8282
}
8383

84-
logService.log(SR, level, eventObject.getFormattedMessage(), t);
84+
final String loggerName = eventObject.getLoggerName();
85+
86+
final String msg = loggerName + "; " + eventObject.getFormattedMessage();
87+
88+
logService.log(SR, level, msg, t);
8589
}
8690

8791
private static final class RootBundleLogbackServiceReference implements ServiceReference {

0 commit comments

Comments
 (0)