Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to find full package name when it is not obvious #81

Open
jglick opened this issue Dec 9, 2024 · 0 comments
Open

Way to find full package name when it is not obvious #81

jglick opened this issue Dec 9, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@jglick
Copy link
Member

jglick commented Dec 9, 2024

public String abbreviateClassName(String fqcn, int targetLength) {
if (fqcn == null) {
return "-";
}
int fqcnLength = fqcn.length();
if (fqcnLength < targetLength) {
return fqcn;
}
int[] indexes = new int[16];
int[] lengths = new int[17];
int count = 0;
for (int i = fqcn.indexOf('.'); i != -1 && count < indexes.length; i = fqcn.indexOf('.', i + 1)) {
indexes[count++] = i;
}
if (count == 0) {
return fqcn;
}
StringBuilder buf = new StringBuilder(targetLength);
int requiredSavings = fqcnLength - targetLength;
for (int i = 0; i < count; i++) {
int previous = i > 0 ? indexes[i - 1] : -1;
int available = indexes[i] - previous - 1;
int length = requiredSavings > 0 ? Math.min(available, 1) : available;
requiredSavings -= available - length;
lengths[i] = length + 1;
}
lengths[count] = fqcnLength - indexes[count - 1];
for (int i = 0; i <= count; i++) {
if (i == 0) {
buf.append(fqcn, 0, lengths[i] - 1);
} else {
buf.append(fqcn, indexes[i - 1], indexes[i - 1] + lengths[i]);
}
}
return buf.toString();
}
is useful to reduce the size of log files and especially the horizontal width of log lines in the case of well-known packages. But on occasion a log message is printed from a class with a generic-sounding name and it is actually difficult to figure out which package this is coming from, even with GitHub code search. Ideas:

  • Print the full package name the first time it is encountered in a given logger.
  • Print the .protectionDomain.codeSource.location of a JAR (at least the filename, not necessary directory path; e.g. some-lib-1.23.jar) the first time it is encountered as the emitter of a log message. Trickier since https://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#getSourceClassName() does not record the Class so it would need to be searched for in the Thread.contextClassLoader.
@jglick jglick added the enhancement New feature or request label Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant