diff --git a/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/BootLogger.java b/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/BootLogger.java index 107d77c9465a..8ce215b987c0 100644 --- a/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/BootLogger.java +++ b/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/BootLogger.java @@ -18,6 +18,8 @@ package com.navercorp.pinpoint.bootstrap; import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; import java.text.MessageFormat; /** @@ -54,7 +56,7 @@ public BootLogger(String loggerName) { throw new NullPointerException("loggerName must not be null"); } // this.loggerName = loggerName; - this.messagePattern = "{0,date,yyyy-MM-dd HH:mm:ss SSS} [{1}](" + loggerName + ") {2}"; + this.messagePattern = "{0,date,yyyy-MM-dd HH:mm:ss} [{1}](" + loggerName + ") {2}{3}"; this.out = out; this.err = err; } @@ -63,10 +65,12 @@ static BootLogger getLogger(String loggerName) { return new BootLogger(loggerName); } - private String format(String logLevel, String msg) { + private String format(String logLevel, String msg, String exceptionMessage) { + exceptionMessage = defaultString(exceptionMessage, ""); + MessageFormat messageFormat = new MessageFormat(messagePattern); final long date = System.currentTimeMillis(); - Object[] parameter = {date, logLevel, msg}; + Object[] parameter = {date, logLevel, msg, exceptionMessage}; return messageFormat.format(parameter); } @@ -75,7 +79,7 @@ public boolean isInfoEnabled() { } public void info(String msg) { - String formatMessage = format("INFO ", msg); + String formatMessage = format("INFO ", msg, ""); this.out.println(formatMessage); } @@ -85,12 +89,31 @@ public boolean isWarnEnabled() { } public void warn(String msg) { - String formatMessage = format("WARN ", msg); - this.err.println(formatMessage); + warn(msg, null); } public void warn(String msg, Throwable throwable) { - warn(msg); - throwable.printStackTrace(this.err); + String exceptionMessage = toString(throwable); + String formatMessage = format("WARN ", msg, exceptionMessage); + this.err.println(formatMessage); + } + + private String toString(Throwable throwable) { + if (throwable == null) { + return ""; + } + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + pw.println(); + throwable.printStackTrace(pw); + pw.close(); + return sw.toString(); + } + + private String defaultString(String exceptionMessage, String defaultValue) { + if (exceptionMessage == null) { + return defaultValue; + } + return exceptionMessage; } } diff --git a/commons/src/main/java/com/navercorp/pinpoint/common/util/logger/StdoutCommonLogger.java b/commons/src/main/java/com/navercorp/pinpoint/common/util/logger/StdoutCommonLogger.java index 65d9f7519a71..84e9262073b8 100644 --- a/commons/src/main/java/com/navercorp/pinpoint/common/util/logger/StdoutCommonLogger.java +++ b/commons/src/main/java/com/navercorp/pinpoint/common/util/logger/StdoutCommonLogger.java @@ -18,6 +18,8 @@ package com.navercorp.pinpoint.common.util.logger; import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; import java.text.MessageFormat; /** @@ -48,7 +50,7 @@ public StdoutCommonLogger(String loggerName) { } // this.loggerName = loggerName; - this.messagePattern = "{0,date,yyyy-MM-dd HH:mm:ss SSS} [{1}](" + loggerName + ") {2}"; + this.messagePattern = "{0,date,yyyy-MM-dd HH:mm:ss} [{1}](" + loggerName + ") {2}{3}"; this.out = out; this.err = err; } @@ -75,16 +77,17 @@ public void debug(String msg) { @Override public void info(String msg) { - String message = format("INFO ", msg); + String message = format("INFO ", msg, ""); this.out.println(message); } - private String format(String logLevel, String msg) { + private String format(String logLevel, String msg, String exceptionMessage) { + exceptionMessage = defaultString(exceptionMessage, ""); MessageFormat messageFormat = new MessageFormat(messagePattern); final long date = System.currentTimeMillis(); - Object[] parameter = {date, logLevel, msg}; + Object[] parameter = {date, logLevel, msg, exceptionMessage}; return messageFormat.format(parameter); } @@ -100,13 +103,32 @@ public boolean isWarnEnabled() { @Override public void warn(String msg) { - String message = format("WARN ", msg); - this.err.println(message); + warn(msg, null); } @Override - public void warn(String msg, Throwable th) { - warn(msg); - th.printStackTrace(this.err); + public void warn(String msg, Throwable throwable) { + String exceptionMessage = toString(throwable); + String message = format("WARN ", msg, exceptionMessage); + this.err.println(message); + } + + private String toString(Throwable throwable) { + if (throwable == null) { + return ""; + } + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + pw.println(); + throwable.printStackTrace(pw); + pw.close(); + return sw.toString(); + } + + private String defaultString(String exceptionMessage, String defaultValue) { + if (exceptionMessage == null) { + return defaultValue; + } + return exceptionMessage; } } diff --git a/commons/src/test/java/com/navercorp/pinpoint/common/util/logger/StdoutCommonLoggerTest.java b/commons/src/test/java/com/navercorp/pinpoint/common/util/logger/StdoutCommonLoggerTest.java index c8ad75e6422b..b2d1454e81b5 100644 --- a/commons/src/test/java/com/navercorp/pinpoint/common/util/logger/StdoutCommonLoggerTest.java +++ b/commons/src/test/java/com/navercorp/pinpoint/common/util/logger/StdoutCommonLoggerTest.java @@ -33,56 +33,26 @@ public class StdoutCommonLoggerTest { private final Logger logger = LoggerFactory.getLogger(getClass()); - private ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - private PrintStream out = new PrintStream(outputStream); - - private ByteArrayOutputStream errOutputStream = new ByteArrayOutputStream(); - private PrintStream err = new PrintStream(errOutputStream); - - private StdoutCommonLogger commonLogger; - - @Before - public void setUp() throws Exception { - this.outputStream = new ByteArrayOutputStream(); - this.out = new PrintStream(outputStream); - - this.errOutputStream = new ByteArrayOutputStream(); - this.err= new PrintStream(errOutputStream); - - this.commonLogger = new StdoutCommonLogger("StdoutCommonLoggerTest", out, err); - } - - private String getOut() { - return getLogMessage(this.outputStream); - } - - private String getError() { - return getLogMessage(this.errOutputStream); - } - - private String getLogMessage(ByteArrayOutputStream byteArrayOutputStream) { - String message = byteArrayOutputStream.toString(); - byteArrayOutputStream.reset(); - return message; - } @Test public void testLogging() { + LoggerHolder loggerHolder = new LoggerHolder("StdoutCommonLoggerTest"); + CommonLogger commonLogger = loggerHolder.getLogger(); commonLogger.debug("info test"); // assertMessage(getOut(), null); commonLogger.info("info test 1"); - assertMessage(getOut(), "info test 1"); + assertMessage(loggerHolder.getOut(), "info test 1"); commonLogger.info("info test 2"); - assertMessage(getOut(), "info test 2"); + assertMessage(loggerHolder.getOut(), "info test 2"); commonLogger.warn("warn test"); - assertMessage(getError(), "warn test"); + assertMessage(loggerHolder.getError(), "warn test"); commonLogger.warn("warn test error", new Exception("testException")); - assertMessage(getError(), "warn test"); + assertMessage(loggerHolder.getError(), "warn test"); } private void assertMessage(String out, String message) { @@ -91,4 +61,37 @@ private void assertMessage(String out, String message) { } + + private static class LoggerHolder { + private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + private final PrintStream out = new PrintStream(outputStream); + + private final ByteArrayOutputStream errOutputStream = new ByteArrayOutputStream(); + private final PrintStream err = new PrintStream(errOutputStream); + + private final StdoutCommonLogger logger; + + public LoggerHolder(String loggerName) { + logger = new StdoutCommonLogger(loggerName, out, err); + } + + public CommonLogger getLogger() { + return logger; + } + + private String getOut() { + return getLogMessage(this.outputStream); + } + + private String getError() { + return getLogMessage(this.errOutputStream); + } + + private String getLogMessage(ByteArrayOutputStream byteArrayOutputStream) { + String message = byteArrayOutputStream.toString(); + byteArrayOutputStream.reset(); + return message; + } + } + } \ No newline at end of file diff --git a/thirdparty/google-guava/src/main/java-logging/com/google/common/logging/Logger.java b/thirdparty/google-guava/src/main/java-logging/com/google/common/logging/Logger.java index 742b99bf49f4..5c02001e05a5 100644 --- a/thirdparty/google-guava/src/main/java-logging/com/google/common/logging/Logger.java +++ b/thirdparty/google-guava/src/main/java-logging/com/google/common/logging/Logger.java @@ -60,7 +60,7 @@ public Logger(String name, Level level) { Logger(String name, Level level, PrintStream out, PrintStream err) { this.name = name; this.level = level; - this.messagePattern = "{0,date,yyyy-MM-dd HH:mm:ss SSS} [{1}](" + name + ") {2}{3}"; + this.messagePattern = "{0,date,yyyy-MM-dd HH:mm:ss} [{1}](" + name + ") {2}{3}"; this.out = out; this.err = err; }