-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* jruby-logger-ng: (21 commits) make SLF4J dependency optional simply use isDebug() instead of getDebug().isTrue() do not remove backtrace's last new line as others don't make sure printed caller/warning backtraces are not printed as (hard to read) array.to_s no need to double check for null ... printBacktraceJRuby does the very same remove DEBUG field - LOG.setDebugEnable will do better in enabling detailed logs use debug level to log + setDebugEnable on JIT_LOADING_DEBUG this way logging will work as expected with delegated logging such as SLF4J as well refactor DEBUG field into setDebugEnable + log at the debug level no need to use Logger in InvokerGenerator ... only invoked from cmd-line unused logger field cleanup - adjust some of the logging calls (most importantly to happen in one stmnt) refactor to using the new preferred way of getting a logger - by passing a class down support getLogger with a Class parameter - where package name is not skipped SLF4J JRuby logger implementation (which delegates all logs to a SLF4J impl) introduce a java.util.logging JRuby logger implementation (which delegates all logs) re-use OutputStreamLogger test-cases test with StandardErrorLogger cleanup LoggerFactory (for later extension) include current thread-name as well as level in logger's formatted message e.g. : cleanup asserts around exception with stack-trace being logged make sure redirected System.err is picked up - not uncommon to happen on Ruby land ...
- 9.4.12.0
- 9.4.11.0
- 9.4.10.0
- 9.4.9.0
- 9.4.8.0
- 9.4.7.0
- 9.4.6.0
- 9.4.5.0
- 9.4.4.0
- 9.4.3.0
- 9.4.2.0
- 9.4.1.0
- 9.4.0.0
- 9.3.15.0
- 9.3.14.0
- 9.3.13.0
- 9.3.12.0
- 9.3.11.0
- 9.3.10.0
- 9.3.9.0
- 9.3.8.0
- 9.3.7.0
- 9.3.6.0
- 9.3.5.0
- 9.3.4.0
- 9.3.3.0
- 9.3.2.0
- 9.3.1.0
- 9.3.0.0
- 9.2.21.0
- 9.2.20.1
- 9.2.20.0
- 9.2.19.0
- 9.2.18.0
- 9.2.17.0
- 9.2.16.0
- 9.2.15.0
- 9.2.14.0
- 9.2.13.0
- 9.2.12.0
- 9.2.11.1
- 9.2.11.0
- 9.2.10.0
- 9.2.9.0
- 9.2.8.0
- 9.2.7.0
- 9.2.6.0
- 9.2.5.0
- 9.2.4.1
- 9.2.4.0
- 9.2.3.0
- 9.2.2.0
- 9.2.1.0
- 9.2.0.0
- 9.1.17.0
- 9.1.16.0
- 9.1.15.0
- 9.1.14.0
- 9.1.13.0
- 9.1.12.0
- 9.1.11.0
- 9.1.10.0
- 9.1.9.0
- 9.1.8.0
- 9.1.7.0
- 9.1.6.0
- 9.1.5.0
- 9.1.4.0
- 9.1.3.0
- 9.1.2.0
- 9.1.1.0
- 9.1.0.0
Showing
55 changed files
with
1,391 additions
and
474 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/***** BEGIN LICENSE BLOCK ***** | ||
* Version: EPL 1.0/GPL 2.0/LGPL 2.1 | ||
* | ||
* The contents of this file are subject to the Eclipse Public | ||
* License Version 1.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of | ||
* the License at http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Software distributed under the License is distributed on an "AS | ||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* rights and limitations under the License. | ||
* | ||
* Copyright (C) 2015-2015 The JRuby Community (and contribs) | ||
* | ||
* Alternatively, the contents of this file may be used under the terms of | ||
* either of the GNU General Public License Version 2 or later (the "GPL"), | ||
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | ||
* in which case the provisions of the GPL or the LGPL are applicable instead | ||
* of those above. If you wish to allow use of your version of this file only | ||
* under the terms of either the GPL or the LGPL, and not to allow others to | ||
* use your version of this file under the terms of the EPL, indicate your | ||
* decision by deleting the provisions above and replace them with the notice | ||
* and other provisions required by the GPL or the LGPL. If you do not delete | ||
* the provisions above, a recipient may use your version of this file under | ||
* the terms of any one of the EPL, the GPL or the LGPL. | ||
***** END LICENSE BLOCK *****/ | ||
package org.jruby.util.log; | ||
|
||
import java.util.logging.Level; | ||
|
||
/** | ||
* Logger which delegates to {@link java.util.logging.Logger}. | ||
* | ||
* @author kares | ||
*/ | ||
public class JULLogger implements Logger { | ||
|
||
private final java.util.logging.Logger logger; // our delegate | ||
|
||
public JULLogger(final String loggerName) { | ||
this( java.util.logging.Logger.getLogger(loggerName) ); | ||
} | ||
|
||
public JULLogger(final Class<?> loggerClass) { | ||
this( java.util.logging.Logger.getLogger(loggerClass.getName()) ); | ||
} | ||
|
||
protected JULLogger(final java.util.logging.Logger logger) { | ||
this.logger = logger; | ||
} | ||
|
||
public String getName() { | ||
return logger.getName(); | ||
} | ||
|
||
public void warn(String message, Object... args) { | ||
log(Level.WARNING, adjustPattern(message), args); | ||
} | ||
|
||
public void warn(Throwable throwable) { | ||
log(Level.WARNING, null, throwable); | ||
} | ||
|
||
public void warn(String message, Throwable throwable) { | ||
log(Level.WARNING, message, throwable); | ||
} | ||
|
||
public void error(String message, Object... args) { | ||
log(Level.SEVERE, adjustPattern(message), args); | ||
} | ||
|
||
public void error(Throwable throwable) { | ||
log(Level.SEVERE, null, throwable); | ||
} | ||
|
||
public void error(String message, Throwable throwable) { | ||
log(Level.SEVERE, message, throwable); | ||
} | ||
|
||
public void info(String message, Object... args) { | ||
if (logger.isLoggable(Level.INFO)) { | ||
log(Level.INFO, adjustPattern(message), args); | ||
} | ||
} | ||
|
||
public void info(Throwable throwable) { | ||
log(Level.INFO, null, throwable); | ||
} | ||
|
||
public void info(String message, Throwable throwable) { | ||
log(Level.INFO, message, throwable); | ||
} | ||
|
||
public void debug(String message, Object... args) { | ||
if (logger.isLoggable(Level.FINE)) { | ||
log(Level.FINE, adjustPattern(message), args); | ||
} | ||
} | ||
|
||
public void debug(Throwable throwable) { | ||
log(Level.FINE, null, throwable); | ||
} | ||
|
||
public void debug(String message, Throwable throwable) { | ||
log(Level.FINE, message, throwable); | ||
} | ||
|
||
|
||
protected void log(Level level, String message, Object... args) { | ||
final String souceClass = null; | ||
final String souceMethod = null; | ||
logger.logp(level, souceClass, souceMethod, message, args); | ||
} | ||
|
||
protected void log(Level level, String message, Throwable ex) { | ||
final String souceClass = null; | ||
final String souceMethod = null; | ||
logger.logp(level, souceClass, souceMethod, message, ex); | ||
} | ||
|
||
public boolean isDebugEnabled() { | ||
return logger.isLoggable(Level.FINE); | ||
} | ||
|
||
public void setDebugEnable(boolean debug) { | ||
logger.setLevel(debug ? Level.FINE : Level.INFO); | ||
} | ||
|
||
// adjust {} to JUL conventions e.g. "hello {} {}" -> "hello {0} {1}" | ||
static String adjustPattern(final String messagePattern) { | ||
if (messagePattern == null) return null; | ||
|
||
StringBuilder julPattern = null; | ||
final int len = messagePattern.length(); int last = 0; int counter = 0; | ||
for ( int i = 0; i < len; i++ ) { | ||
if ( messagePattern.charAt(i) == '{' && | ||
( i == 0 || (i > 0 && messagePattern.charAt(i - 1) != '\\' ) ) && | ||
( i < len - 1 && messagePattern.charAt(i + 1) == '}' ) ) { | ||
if (julPattern == null) { | ||
julPattern = new StringBuilder(len + 8); | ||
} | ||
julPattern. | ||
append(messagePattern, last, i). | ||
append('{').append(counter++).append('}'); | ||
last = i + 2; | ||
} | ||
} | ||
|
||
if (julPattern != null) { | ||
if (last < len) { | ||
julPattern.append(messagePattern, last, len); | ||
} | ||
return julPattern.toString(); | ||
} | ||
|
||
return messagePattern; // no {} place holders | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
core/src/main/java/org/jruby/util/log/OutputStreamLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/***** BEGIN LICENSE BLOCK ***** | ||
* Version: EPL 1.0/GPL 2.0/LGPL 2.1 | ||
* | ||
* The contents of this file are subject to the Eclipse Public | ||
* License Version 1.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of | ||
* the License at http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Software distributed under the License is distributed on an "AS | ||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* rights and limitations under the License. | ||
* | ||
* Copyright (C) 2015-2015 The JRuby Community (and contribs) | ||
* | ||
* Alternatively, the contents of this file may be used under the terms of | ||
* either of the GNU General Public License Version 2 or later (the "GPL"), | ||
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | ||
* in which case the provisions of the GPL or the LGPL are applicable instead | ||
* of those above. If you wish to allow use of your version of this file only | ||
* under the terms of either the GPL or the LGPL, and not to allow others to | ||
* use your version of this file under the terms of the EPL, indicate your | ||
* decision by deleting the provisions above and replace them with the notice | ||
* and other provisions required by the GPL or the LGPL. If you do not delete | ||
* the provisions above, a recipient may use your version of this file under | ||
* the terms of any one of the EPL, the GPL or the LGPL. | ||
***** END LICENSE BLOCK *****/ | ||
package org.jruby.util.log; | ||
|
||
import java.io.PrintStream; | ||
|
||
import org.joda.time.DateTime; | ||
|
||
/** | ||
* Logger logging to an output (print) stream. | ||
* @author kares | ||
*/ | ||
public class OutputStreamLogger implements Logger { | ||
|
||
private final String loggerName; | ||
private boolean debug = false; | ||
protected PrintStream stream; // volatile? | ||
|
||
public OutputStreamLogger(String loggerName) { | ||
this.loggerName = loggerName; | ||
} | ||
|
||
public OutputStreamLogger(String loggerName, PrintStream stream) { | ||
this.loggerName = loggerName; | ||
this.stream = stream; | ||
} | ||
|
||
public String getName() { | ||
return loggerName; | ||
} | ||
|
||
public PrintStream getStream() { | ||
return stream; | ||
} | ||
|
||
public void setStream(PrintStream stream) { | ||
this.stream = stream; | ||
} | ||
|
||
public void warn(String message, Object... args) { | ||
write(message, "WARN", args); | ||
} | ||
|
||
public void warn(Throwable throwable) { | ||
write("", "WARN", throwable); | ||
} | ||
|
||
public void warn(String message, Throwable throwable) { | ||
write(message, "WARN", throwable); | ||
} | ||
|
||
public void error(String message, Object... args) { | ||
write(message, "ERROR", args); | ||
} | ||
|
||
public void error(Throwable throwable) { | ||
write("", "ERROR", throwable); | ||
} | ||
|
||
public void error(String message, Throwable throwable) { | ||
write(message, "ERROR", throwable); | ||
} | ||
|
||
public void info(String message, Object... args) { | ||
write(message, "INFO", args); | ||
} | ||
|
||
public void info(Throwable throwable) { | ||
write("", "INFO", throwable); | ||
} | ||
|
||
public void info(String message, Throwable throwable) { | ||
write(message, "INFO", throwable); | ||
} | ||
|
||
public void debug(String message, Object... args) { | ||
if (debug) write(message, "DEBUG", args); | ||
} | ||
|
||
public void debug(Throwable throwable) { | ||
if (debug) write("", "DEBUG", throwable); | ||
} | ||
|
||
public void debug(String message, Throwable throwable) { | ||
if (debug) write(message, "DEBUG", throwable); | ||
} | ||
|
||
public boolean isDebugEnabled() { | ||
return debug; | ||
} | ||
|
||
public void setDebugEnable(boolean debug) { | ||
this.debug = debug; | ||
} | ||
|
||
protected void write(String message, String level, Object[] args) { | ||
CharSequence suble = substitute(message, args); | ||
stream.println(formatMessage(suble, level)); | ||
} | ||
|
||
protected void write(String message, String level, Throwable throwable) { | ||
synchronized (stream) { | ||
stream.println(formatMessage(message, level)); | ||
//if ( message == null || message.length() > 0 ) stream.print(' '); | ||
writeStackTrace(stream, throwable); | ||
} | ||
} | ||
|
||
protected static void writeStackTrace(PrintStream stream, Throwable throwable) { | ||
if (throwable == null) { | ||
throw new IllegalArgumentException("null throwable"); | ||
} | ||
|
||
throwable.printStackTrace(stream); | ||
} | ||
|
||
protected String formatMessage(CharSequence message, String level) { | ||
// 2015-11-04T11:29:41.759+01:00 [main] INFO SampleLogger : hello world | ||
return new StringBuilder(64) | ||
.append(new DateTime(System.currentTimeMillis())) | ||
.append(' ') | ||
.append('[').append(Thread.currentThread().getName()).append(']') | ||
.append(' ') | ||
.append(level) | ||
.append(' ') | ||
.append(loggerName) | ||
.append(" : ") | ||
.append(message) | ||
.toString(); | ||
} | ||
|
||
/** | ||
* Message pattern {} substitution. | ||
* @param messagePattern | ||
* @param args | ||
*/ | ||
static CharSequence substitute(final String messagePattern, Object... args) { | ||
if (messagePattern == null) { | ||
final StringBuilder msg = new StringBuilder(); | ||
for (int i = 0; i < args.length; i++) msg.append( args[i] ); | ||
return msg; | ||
} | ||
|
||
StringBuilder msg = null; | ||
final int len = messagePattern.length(); int s = 0; int a = 0; | ||
for ( int i = 0; i < len; i++ ) { | ||
if ( messagePattern.charAt(i) == '{' && | ||
( i == 0 || (i > 0 && messagePattern.charAt(i - 1) != '\\' ) ) && | ||
( i < len - 1 && messagePattern.charAt(i + 1) == '}' ) ) { | ||
|
||
if (msg == null) { | ||
msg = new StringBuilder(len + 48); | ||
} | ||
|
||
msg.append(messagePattern, s, i); s = i + 2; | ||
|
||
if ( a < args.length ) { | ||
msg.append( args[a++] ); | ||
} | ||
else { // invalid e.g. ("hello {} {}", "world") | ||
msg.append( "{!abs-arg!}" ); // absent argument | ||
} | ||
} | ||
} | ||
|
||
if ( msg != null ) { | ||
if ( s < len ) msg.append(messagePattern, s, len); | ||
return msg; | ||
} | ||
return messagePattern; // no substitution place-holders | ||
} | ||
|
||
} |
38 changes: 0 additions & 38 deletions
38
core/src/main/java/org/jruby/util/log/ParameterizedWriter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/***** BEGIN LICENSE BLOCK ***** | ||
* Version: EPL 1.0/GPL 2.0/LGPL 2.1 | ||
* | ||
* The contents of this file are subject to the Eclipse Public | ||
* License Version 1.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of | ||
* the License at http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Software distributed under the License is distributed on an "AS | ||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* rights and limitations under the License. | ||
* | ||
* Copyright (C) 2015-2015 The JRuby Community (and contribs) | ||
* | ||
* Alternatively, the contents of this file may be used under the terms of | ||
* either of the GNU General Public License Version 2 or later (the "GPL"), | ||
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | ||
* in which case the provisions of the GPL or the LGPL are applicable instead | ||
* of those above. If you wish to allow use of your version of this file only | ||
* under the terms of either the GPL or the LGPL, and not to allow others to | ||
* use your version of this file under the terms of the EPL, indicate your | ||
* decision by deleting the provisions above and replace them with the notice | ||
* and other provisions required by the GPL or the LGPL. If you do not delete | ||
* the provisions above, a recipient may use your version of this file under | ||
* the terms of any one of the EPL, the GPL or the LGPL. | ||
***** END LICENSE BLOCK *****/ | ||
package org.jruby.util.log; | ||
|
||
/** | ||
* Using SLF4J API as a logging backend. | ||
* @author kares | ||
*/ | ||
public class SLF4JLogger implements Logger { | ||
|
||
private final org.slf4j.Logger logger; | ||
|
||
public SLF4JLogger(final String loggerName) { | ||
this( org.slf4j.LoggerFactory.getLogger(loggerName) ); | ||
} | ||
|
||
public SLF4JLogger(final Class<?> loggerClass) { | ||
this( org.slf4j.LoggerFactory.getLogger(loggerClass) ); | ||
} | ||
|
||
protected SLF4JLogger(final org.slf4j.Logger logger) { | ||
this.logger = logger; | ||
} | ||
|
||
public String getName() { | ||
return logger.getName(); | ||
} | ||
|
||
public void warn(String message, Object... args) { | ||
logger.warn(message, args); | ||
} | ||
|
||
public void warn(Throwable throwable) { | ||
logger.warn("", throwable); | ||
} | ||
|
||
public void warn(String message, Throwable throwable) { | ||
logger.warn(message, throwable); | ||
} | ||
|
||
public void error(String message, Object... args) { | ||
logger.error(message, args); | ||
} | ||
|
||
public void error(Throwable throwable) { | ||
logger.error("", throwable); | ||
} | ||
|
||
public void error(String message, Throwable throwable) { | ||
logger.error(message, throwable); | ||
} | ||
|
||
public void info(String message, Object... args) { | ||
logger.info(message, args); | ||
} | ||
|
||
public void info(Throwable throwable) { | ||
logger.info("", throwable); | ||
} | ||
|
||
public void info(String message, Throwable throwable) { | ||
logger.info(message, throwable); | ||
} | ||
|
||
public void debug(String message, Object... args) { | ||
logger.debug(message, args); | ||
} | ||
|
||
public void debug(Throwable throwable) { | ||
logger.debug("", throwable); | ||
} | ||
|
||
public void debug(String message, Throwable throwable) { | ||
logger.debug(message, throwable); | ||
} | ||
|
||
public boolean isDebugEnabled() { | ||
return logger.isDebugEnabled(); | ||
} | ||
|
||
public void setDebugEnable(boolean debug) { | ||
// no-op ignore | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
core/src/test/java/org/jruby/util/log/LoggerFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/* | ||
* Copyright (c) 2015 JRuby. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* JRuby - initial API and implementation and/or initial documentation | ||
*/ | ||
package org.jruby.util.log; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author kares | ||
*/ | ||
public class LoggerFactoryTest { | ||
|
||
@Test | ||
public void hasCorrectBackupLoggerClassName() { | ||
assertEquals( StandardErrorLogger.class.getName(), LoggerFactory.BACKUP_LOGGER_CLASS ); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("deprecation") | ||
public void usesStandardErrorLoggerByDefault() { | ||
if ( LoggerFactory.LOGGER_CLASS != null ) return; // skip test | ||
|
||
Logger logger = LoggerFactory.getLogger(LoggerFactoryTest.class); | ||
assertTrue( logger instanceof StandardErrorLogger ); | ||
assertFalse( logger.isDebugEnabled() ); | ||
logger.debug("not-logged", new IllegalStateException("if you're reading this - smt is BAD")); | ||
// try if it works without throwing : | ||
logger.info("hello {}", "world"); | ||
|
||
logger = LoggerFactory.getLogger("LoggerFactoryTest"); | ||
assertTrue( logger instanceof StandardErrorLogger ); | ||
} | ||
|
||
@Test | ||
public void usingJULLogger() throws Exception { | ||
final ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger("JULLogger"); | ||
java.util.logging.SimpleFormatter fmt = new java.util.logging.SimpleFormatter() { | ||
|
||
@Override | ||
public synchronized String format(java.util.logging.LogRecord record) { | ||
final String format = "%2$s %4$s: %5$s%6$s%n"; | ||
String source; | ||
if (record.getSourceClassName() != null) { | ||
source = record.getSourceClassName(); | ||
if (record.getSourceMethodName() != null) { | ||
source += " " + record.getSourceMethodName(); | ||
} | ||
} else { | ||
source = record.getLoggerName(); | ||
} | ||
String message = formatMessage(record); | ||
String throwable = ""; | ||
if (record.getThrown() != null) { | ||
StringWriter sw = new StringWriter(); | ||
PrintWriter pw = new PrintWriter(sw); | ||
pw.println(); | ||
record.getThrown().printStackTrace(pw); | ||
pw.close(); | ||
throwable = sw.toString(); | ||
} | ||
return String.format(format, | ||
"", // no timestamp | ||
source, | ||
record.getLoggerName(), | ||
record.getLevel().getLocalizedName(), | ||
message, | ||
throwable); | ||
} | ||
|
||
}; | ||
java.util.logging.StreamHandler handler = new java.util.logging.StreamHandler(out, fmt); | ||
handler.setLevel(java.util.logging.Level.ALL); | ||
julLogger.addHandler(handler); | ||
julLogger.setUseParentHandlers(false); | ||
|
||
julLogger.setLevel(java.util.logging.Level.INFO); | ||
|
||
changeLoggerImpl(JULLogger.class); | ||
|
||
Logger logger = LoggerFactory.getLogger("JULLogger"); | ||
assertFalse( logger.isDebugEnabled() ); | ||
|
||
logger.debug("ignored debug stuff"); | ||
logger.debug("more ignored debug stuff {}", 10); | ||
handler.flush(); | ||
assertEquals("", out.toString()); | ||
|
||
String log = ""; | ||
|
||
logger.info("logged at info level"); | ||
handler.flush(); | ||
assertEquals(log += "JULLogger INFO: logged at info level\n", out.toString()); | ||
|
||
logger.warn("logged at {} {}", "warn", new StringBuilder("level")); | ||
handler.flush(); | ||
assertEquals(log += "JULLogger WARNING: logged at warn level\n", out.toString()); | ||
|
||
julLogger.setLevel(java.util.logging.Level.WARNING); | ||
|
||
logger.info("more at info level {}", 'z'); | ||
handler.flush(); assertEquals(log, out.toString()); | ||
logger.info("even more at info level", new RuntimeException("ex")); | ||
handler.flush(); assertEquals(log, out.toString()); | ||
|
||
logger.error("bad news", new RuntimeException("exception happened")); | ||
handler.flush(); | ||
assertStartsWith(log += "JULLogger SEVERE: bad news\njava.lang.RuntimeException: exception happened", out.toString()); | ||
} | ||
|
||
@Test | ||
public void usingSLF4JLogger() throws Exception { | ||
final PrintStream defaultErr = System.err; | ||
try { | ||
System.setProperty("org.slf4j.simpleLogger.logFile", "System.err"); | ||
System.setProperty("org.slf4j.simpleLogger.dateTimeFormat", "yyyy"); | ||
System.setProperty("org.slf4j.simpleLogger.showThreadName", "false"); | ||
|
||
final ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
final PrintStream printOut = new PrintStream(out); | ||
System.setErr(printOut); | ||
|
||
//org.slf4j.Logger slf4jLogger = org.sqlf4j.LoggerFactory.getLogger("SLF4JLogger"); | ||
|
||
changeLoggerImpl(SLF4JLogger.class); | ||
|
||
Logger logger = LoggerFactory.getLogger(SLF4JLogger.class); | ||
assertFalse( logger.isDebugEnabled() ); | ||
|
||
logger.debug("ignored debug stuff"); | ||
logger.debug("more ignored debug stuff {}", 10); | ||
printOut.flush(); | ||
assertEquals("", out.toString()); | ||
|
||
String log = ""; | ||
|
||
logger.info("logged at info level"); | ||
printOut.flush(); | ||
assertEquals(log += "INFO org.jruby.util.log.SLF4JLogger - logged at info level\n", out.toString()); | ||
|
||
logger.warn("logged at {} {}", "warn", new StringBuilder("level")); | ||
printOut.flush(); | ||
assertEquals(log += "WARN org.jruby.util.log.SLF4JLogger - logged at warn level\n", out.toString()); | ||
|
||
logger.debug("more debug", new RuntimeException("ex")); | ||
printOut.flush(); assertEquals(log, out.toString()); | ||
|
||
logger.error("bad news", new RuntimeException("exception happened")); | ||
printOut.flush(); | ||
assertStartsWith(log += "ERROR org.jruby.util.log.SLF4JLogger - bad news\njava.lang.RuntimeException: exception happened", out.toString()); | ||
|
||
} | ||
finally { | ||
System.setErr(defaultErr); | ||
} | ||
} | ||
|
||
final static Constructor DEFAULT_LOGGER = LoggerFactory.LOGGER; | ||
final static Constructor DEFAULT_LOGGER_OLD = LoggerFactory.LOGGER_OLD; | ||
|
||
@After | ||
public void restoreLoggerImpl() throws Exception { | ||
if ( LoggerFactory.LOGGER != DEFAULT_LOGGER ) { | ||
setLoggerImpl(DEFAULT_LOGGER, DEFAULT_LOGGER_OLD); | ||
} | ||
} | ||
|
||
static void changeLoggerImpl(final Class<? extends Logger> type) throws NoSuchFieldException, IllegalAccessException { | ||
final String klass = type.getName(); | ||
Constructor LOGGER = LoggerFactory.resolveLoggerConstructor(klass, Class.class, true); | ||
Constructor LOGGER_OLD; | ||
if ( LOGGER == null ) { | ||
LOGGER_OLD = LoggerFactory.resolveLoggerConstructor(klass, String.class, false); | ||
} | ||
else { | ||
LOGGER_OLD = LoggerFactory.resolveLoggerConstructor(klass, String.class, true); | ||
} | ||
setLoggerImpl( LOGGER, LOGGER_OLD ); | ||
} | ||
|
||
static void setLoggerImpl(final Constructor logger, final Constructor loggerOld) | ||
throws NoSuchFieldException, IllegalAccessException { | ||
final Field LOGGER = LoggerFactory.class.getDeclaredField("LOGGER"); | ||
LOGGER.setAccessible(true); | ||
changeModifiers(LOGGER, LOGGER.getModifiers() & (~Modifier.FINAL)); | ||
|
||
LOGGER.set(null, logger); | ||
|
||
final Field LOGGER_OLD = LoggerFactory.class.getDeclaredField("LOGGER_OLD"); | ||
LOGGER_OLD.setAccessible(true); | ||
changeModifiers(LOGGER_OLD, LOGGER_OLD.getModifiers() & (~Modifier.FINAL)); | ||
|
||
LOGGER_OLD.set(null, loggerOld); | ||
} | ||
|
||
private static void changeModifiers(final Field field, final int mod) | ||
throws NoSuchFieldException, IllegalAccessException { | ||
Field modifiers = Field.class.getDeclaredField("modifiers"); | ||
modifiers.setAccessible(true); | ||
modifiers.setInt(field, mod); | ||
} | ||
|
||
static void assertStartsWith(String expected, String actual) { | ||
final String begPart = actual.substring(0, expected.length()); | ||
assertEquals("expected: \"" + actual + "\" to start with: \"" + expected + "\"", expected, begPart); | ||
} | ||
|
||
} |
154 changes: 154 additions & 0 deletions
154
core/src/test/java/org/jruby/util/log/OutputStreamLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright (c) 2015 JRuby. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* JRuby - initial API and implementation and/or initial documentation | ||
*/ | ||
package org.jruby.util.log; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.After; | ||
|
||
/** | ||
* @author kares | ||
*/ | ||
public class OutputStreamLoggerTest extends junit.framework.TestCase { | ||
|
||
static final String LINE_SEPARATOR = System.getProperty("line.separator"); | ||
|
||
protected final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
protected final PrintStream stream = new PrintStream(baos); | ||
|
||
protected final Logger logger = newLogger(); | ||
|
||
protected Logger newLogger() { | ||
return new OutputStreamLogger(OutputStreamLoggerTest.class.getSimpleName(), stream); | ||
} | ||
|
||
@After | ||
public void resetStream() throws IOException { | ||
baos.reset(); | ||
} | ||
|
||
@Test | ||
public void testPlainMessage() { | ||
logger.info("test"); | ||
assertStreamEndsWith("test"); | ||
} | ||
|
||
@Test | ||
public void testMessageWithPlaceholder() { | ||
logger.warn("hello {}", "world"); | ||
assertStreamEndsWith("hello world"); | ||
} | ||
|
||
@Test | ||
public void testMessageWithPlaceholders() { | ||
logger.warn("{} {}", 1, new StringBuilder("23")); | ||
assertStreamEndsWith("1 23\n", false); | ||
} | ||
|
||
@Test | ||
public void testWithDebuggingDisabled() { | ||
final boolean debug = logger.isDebugEnabled(); | ||
try { | ||
logger.setDebugEnable(false); | ||
logger.debug("debug-test-1"); | ||
logger.debug("debug-test-{}", 2); | ||
logger.debug("debug-test-x", new RuntimeException()); | ||
assertStreamEmpty(); | ||
} | ||
finally { | ||
logger.setDebugEnable(debug); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWithDebuggingEnabled() { | ||
final boolean debug = logger.isDebugEnabled(); | ||
try { | ||
logger.setDebugEnable(true); | ||
String arg = null; | ||
logger.debug("debug-test-{}", arg); | ||
assertStreamNotEmpty(); | ||
assertStreamEndsWith("debug-test-null"); | ||
} | ||
finally { | ||
logger.setDebugEnable(debug); | ||
} | ||
} | ||
|
||
@Test | ||
public void testIncludesLevel() { | ||
final boolean debug = logger.isDebugEnabled(); | ||
try { | ||
logger.setDebugEnable(true); | ||
logger.debug("{} message", "a debug"); | ||
assertStreamNotEmpty(); | ||
assertStreamEndsWith("DEBUG OutputStreamLoggerTest : a debug message"); | ||
|
||
logger.info("hello!"); | ||
assertStreamNotEmpty(); | ||
assertStreamEndsWith("INFO OutputStreamLoggerTest : hello!"); | ||
|
||
logger.warn("red-{}", "alert"); | ||
assertStreamNotEmpty(); | ||
assertStreamEndsWith("WARN OutputStreamLoggerTest : red-alert"); | ||
} | ||
finally { | ||
logger.setDebugEnable(debug); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWithException() { | ||
logger.error("debug-test-x", new RuntimeException("42")); | ||
assertStreamNotEmpty(); | ||
|
||
assertStreamContains("debug-test-x\njava.lang.RuntimeException: 42"); | ||
// with stack trace : | ||
assertStreamContains("at org.jruby.util.log.OutputStreamLoggerTest.testWithException"); | ||
} | ||
|
||
protected void assertStreamEmpty() { | ||
stream.flush(); | ||
Assert.assertEquals("", baos.toString()); | ||
} | ||
|
||
protected void assertStreamNotEmpty() { | ||
stream.flush(); | ||
Assert.assertNotEquals("", baos.toString()); | ||
} | ||
|
||
protected void assertStreamEndsWith(String expected) { | ||
assertStreamEndsWith(expected, true); | ||
} | ||
|
||
protected void assertStreamEndsWith(String expected, final boolean newLine) { | ||
stream.flush(); | ||
if ( newLine ) expected = expected + LINE_SEPARATOR; | ||
|
||
final String actual = baos.toString(); | ||
final String endPart = actual.substring(actual.length() - expected.length()); | ||
|
||
Assert.assertEquals("expected: \"" + actual + "\" to end with: \"" + expected + "\"", expected, endPart); | ||
} | ||
|
||
protected void assertStreamContains(String expected) { | ||
stream.flush(); | ||
|
||
final String actual = baos.toString(); | ||
|
||
Assert.assertTrue("expected: \"" + actual + "\" to contain: \"" + expected + "\"", actual.contains(expected)); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
core/src/test/java/org/jruby/util/log/ParameterSubstitutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.jruby.util.log; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import static org.jruby.util.log.OutputStreamLogger.substitute; | ||
|
||
public class ParameterSubstitutionTest extends TestCase { | ||
|
||
public void testWithNoPlaceholder() { | ||
CharSequence actual = substitute("test"); | ||
assertEquals("test", actual.toString()); | ||
} | ||
|
||
public void testWithEmptyString() { | ||
CharSequence actual = substitute(""); | ||
assertEquals("", actual.toString()); | ||
} | ||
|
||
public void testWithNonEmptyString() { | ||
CharSequence actual = substitute(" "); | ||
assertEquals(" ", actual.toString()); | ||
} | ||
|
||
public void testWithEmptyStringAndArg() { | ||
CharSequence actual = substitute("", "arg"); | ||
assertEquals("", actual.toString()); | ||
} | ||
|
||
public void testWithJustOnePlaceholder() { | ||
CharSequence actual = substitute("{}", "a"); | ||
assertEquals("a", actual.toString()); | ||
} | ||
|
||
public void testWithOnePlaceholder() { | ||
CharSequence actual = substitute("a {}", "test"); | ||
assertEquals("a test", actual.toString()); | ||
} | ||
|
||
public void testWithTwoPlaceholders() { | ||
CharSequence actual = substitute("{} and {}", "test1", "test2"); | ||
assertEquals("test1 and test2", actual.toString()); | ||
} | ||
|
||
public void testWithPlaceholdersCallsToString() { | ||
CharSequence actual = substitute("{} and {}", new StringBuilder("test"), new java.util.ArrayList()); | ||
assertEquals("test and []", actual.toString()); | ||
} | ||
|
||
public void testWithTwoPlaceholdersAfterAnother() { | ||
CharSequence actual = substitute("{}{}", "1", Integer.valueOf(2)); | ||
assertEquals("12", actual.toString()); | ||
} | ||
|
||
public void testWithPlaceholdersAndNullArg() { | ||
CharSequence actual = substitute("{} and {}", 0, null); | ||
assertEquals("0 and null", actual.toString()); | ||
|
||
actual = substitute("the {} {}!{some}", null, null); | ||
assertEquals("the null null!{some}", actual.toString()); | ||
} | ||
|
||
public void testPlaceholderEscaping() { | ||
CharSequence actual = substitute("\\{} & {}", 0, null); | ||
assertEquals("\\{} & 0", actual.toString()); | ||
} | ||
|
||
public void testMorePlaceholdersThanArgs() { | ||
CharSequence actual = substitute("hello {}, {} and {}", 'A', 'B'); | ||
assertEquals("hello A, B and {!abs-arg!}", actual.toString()); | ||
|
||
String arg = null; | ||
actual = substitute("{} {} {xxx}", arg); | ||
assertEquals("null {!abs-arg!} {xxx}", actual.toString()); | ||
} | ||
|
||
} |
53 changes: 0 additions & 53 deletions
53
core/src/test/java/org/jruby/util/log/ParameterizedWriterTest.java
This file was deleted.
Oops, something went wrong.
103 changes: 78 additions & 25 deletions
103
core/src/test/java/org/jruby/util/log/StandardErrorLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,94 @@ | ||
package org.jruby.util.log; | ||
|
||
import junit.framework.TestCase; | ||
import org.junit.Assert; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
|
||
public class StandardErrorLoggerTest extends TestCase { | ||
import org.junit.Test; | ||
|
||
public class StandardErrorLoggerTest extends OutputStreamLoggerTest { | ||
|
||
private ByteArrayOutputStream baos; | ||
private StandardErrorLogger logger; | ||
private PrintStream stream; | ||
private static final PrintStream defaultErr = System.err; | ||
|
||
public void setUp() { | ||
baos = new ByteArrayOutputStream(); | ||
stream = new PrintStream(baos); | ||
logger = new StandardErrorLogger("test", stream); | ||
protected Logger newLogger() { | ||
return new StandardErrorLogger(OutputStreamLoggerTest.class.getSimpleName(), stream); | ||
} | ||
|
||
public void tearDown() throws IOException { | ||
baos.reset(); | ||
baos.close(); | ||
@Test | ||
public void testWithDebuggingDisabledUsingSystemErr() { | ||
try { | ||
System.setErr(stream); | ||
|
||
StandardErrorLogger logger = new StandardErrorLogger("StandardErrorLoggerTest"); | ||
|
||
logger.setDebugEnable(false); | ||
logger.debug("test"); | ||
stream.flush(); | ||
assertStreamEmpty(); | ||
} | ||
finally { | ||
System.setErr(defaultErr); | ||
} | ||
} | ||
|
||
public void testWithDebuggingDisabled() { | ||
logger.setDebugEnable(false); | ||
logger.debug("test"); | ||
stream.flush(); | ||
Assert.assertEquals("", baos.toString()); | ||
@Test | ||
public void testWithDebuggingEnabledUsingSystemErr() { | ||
try { | ||
System.setErr(stream); | ||
|
||
StandardErrorLogger logger = new StandardErrorLogger("StandardErrorLoggerTest"); | ||
|
||
logger.setDebugEnable(true); | ||
String arg = null; | ||
logger.debug("debug-test-{}", arg); | ||
assertStreamNotEmpty(); | ||
assertStreamEndsWith("debug-test-null"); | ||
} | ||
finally { | ||
System.setErr(defaultErr); | ||
} | ||
} | ||
|
||
@Test | ||
public void testIncludesLevelUsingSystemErr() { | ||
try { | ||
System.setErr(stream); | ||
|
||
StandardErrorLogger logger = new StandardErrorLogger("StandardErrorLoggerTest"); | ||
|
||
logger.setDebugEnable(true); | ||
logger.debug("{} message", "a debug"); | ||
assertStreamNotEmpty(); | ||
assertStreamEndsWith("DEBUG StandardErrorLoggerTest : a debug message"); | ||
|
||
logger.info("hello!"); | ||
assertStreamNotEmpty(); | ||
assertStreamEndsWith("INFO StandardErrorLoggerTest : hello!"); | ||
|
||
logger.warn("red-{}", "alert"); | ||
assertStreamNotEmpty(); | ||
assertStreamEndsWith("WARN StandardErrorLoggerTest : red-alert"); | ||
} | ||
finally { | ||
System.setErr(defaultErr); | ||
} | ||
} | ||
|
||
@Override @Test | ||
public void testWithException() { | ||
logger.setDebugEnable(true); | ||
logger.debug(new IllegalStateException()); | ||
stream.flush(); | ||
Assert.assertTrue(baos.toString().contains(IllegalStateException.class.getName())); | ||
try { | ||
System.setErr(stream); | ||
|
||
StandardErrorLogger logger = new StandardErrorLogger("StandardErrorLoggerTest"); | ||
|
||
logger.error("debug-test-x", new RuntimeException("42")); | ||
assertStreamNotEmpty(); | ||
|
||
assertStreamContains("debug-test-x\njava.lang.RuntimeException: 42"); | ||
// with stack trace : | ||
assertStreamContains("at org.jruby.util.log.StandardErrorLoggerTest.testWithException"); | ||
} | ||
finally { | ||
System.setErr(defaultErr); | ||
} | ||
} | ||
|
||
} |