Skip to content

Instantly share code, notes, and snippets.

@jpschewe
Last active September 10, 2016 22:12
Show Gist options
  • Save jpschewe/ed09230bad39bcbb50952cf49fff0928 to your computer and use it in GitHub Desktop.
Save jpschewe/ed09230bad39bcbb50952cf49fff0928 to your computer and use it in GitHub Desktop.
Java logging example
/build/
.classpath
.settings
.project
package net.eggplant.mtu;
import java.util.logging.Logger;
public class ClassA {
private static final Logger LOGGER = logging.LogUtils.getLogger();
public void logInfo() {
LOGGER.info("info Coming from ClassA");
}
public void logFine() {
LOGGER.fine("fine Coming from ClassA");
}
}
package net.eggplant;
import java.util.logging.Logger;
public class ClassB {
private static final Logger LOGGER = logging.LogUtils.getLogger();
public void logInfo() {
LOGGER.info("info Coming from ClassA");
}
public void logFine() {
LOGGER.fine("fine Coming from ClassA");
}
}
package logging;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;
public class JavaLogging {
private static void configureLogging() throws SecurityException, IOException {
try (final InputStream stream = JavaLogging.class.getResourceAsStream("logging.properties")) {
LogManager.getLogManager().readConfiguration(stream);
}
}
public static void main(final String[] args) throws SecurityException, IOException {
configureLogging();
net.eggplant.mtu.ClassA classA = new net.eggplant.mtu.ClassA();
net.eggplant.ClassB classB = new net.eggplant.ClassB();
classA.logInfo();
classB.logInfo();
classA.logFine();
classB.logFine();
}
}
handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s [%3$s] %4$s: %5$s%n
# default level for all loggers
.level = INFO
net.eggplant.ClassB.level = ALL
net.eggplant.level = ALL
package logging;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class LogUtils {
/**
* Automatically gets logger for class calling this method. This method also
* ensures that a {@link BufferedAppender} is added to the default appenders
* until log4j is configured.
*/
public static Logger getLogger() {
final StackTraceElement[] elements = new RuntimeException().getStackTrace();
boolean useNextElement = false;
for (final StackTraceElement element : elements) {
if (!useNextElement) {
if (element.getClassName().equals(LogUtils.class.getName())
&& element.getMethodName().equals("getLogger")) {
useNextElement = true;
}
} else {
return Logger.getLogger(element.getClassName());
}
}
// cannot find logger, this is odd, return Root Logger
Logger.getGlobal().warning("Cannot find logger for calling class with stack trace, returning root logger:"
+ Arrays.asList(elements));
return Logger.getGlobal();
}
}
rm -r build
mkdir build
javac *.java -d build
cp logging.properties build/logging/logging.properties
java -classpath build logging.JavaLogging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment