Skip to content

Commit 8b4a130

Browse files
committed
[feat] pass down exception directly to logger
1 parent 9ea80cd commit 8b4a130

File tree

4 files changed

+20
-62
lines changed

4 files changed

+20
-62
lines changed

src/main/java/org/jruby/rack/ext/Logger.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.jruby.rack.RackContext;
4040
import org.jruby.rack.RackLogger;
4141
import org.jruby.rack.logging.ServletContextLogger;
42-
import org.jruby.rack.util.ExceptionUtils;
4342
import org.jruby.runtime.Block;
4443
import org.jruby.runtime.ObjectAllocator;
4544
import org.jruby.runtime.ThreadContext;
@@ -382,10 +381,9 @@ private boolean add(final int severity, final ThreadContext context, IRubyObject
382381
final long datetime = System.currentTimeMillis();
383382
msg = format_message(context, severity, datetime, progname, msg);
384383
}
385-
else if ( msg instanceof RubyException ) { // print backtrace for error
386-
final RubyException error = (RubyException) msg;
387-
error.prepareIntegratedBacktrace(context, null);
388-
doLog( loggerLevel, ExceptionUtils.formatError(error) );
384+
else if ( msg instanceof RubyException ) {
385+
final RubyException ex = (RubyException) msg;
386+
doLog( loggerLevel, ex.toThrowable() );
389387
return true;
390388
}
391389
// @logdev.write(format_message(format_severity(severity), Time.now, progname, message))
@@ -462,12 +460,16 @@ public <T> T toJava(Class<T> target) {
462460
return super.toJava(target);
463461
}
464462

463+
private void doLog(RackLogger.Level level, Throwable ex) {
464+
logger.log(level, "", ex);
465+
}
466+
465467
private void doLog(RackLogger.Level level, CharSequence message) {
466-
logger.log( level, message );
468+
logger.log(level, message);
467469
}
468470

469471
private void doLog(RubyString message) {
470-
logger.log( message );
472+
logger.log(message);
471473
}
472474

473475
/**

src/main/java/org/jruby/rack/logging/OutputStreamLogger.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,7 @@ public static void printLevel(final RackLogger.Base logger, final Level level, f
113113
}
114114

115115
public static void printMessage(final PrintStream out, final CharSequence message) {
116-
if ( message.charAt(message.length() - 1) == '\n' ) {
117-
out.print(message);
118-
}
119-
else {
120-
out.println(message);
121-
}
116+
out.println(message);
122117
}
123118

124119
}

src/main/java/org/jruby/rack/util/ExceptionUtils.java

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@
2626
import java.io.IOException;
2727

2828
import org.jruby.Ruby;
29-
import org.jruby.RubyArray;
3029
import org.jruby.RubyClass;
31-
import org.jruby.RubyException;
32-
import org.jruby.RubyString;
3330
import org.jruby.exceptions.RaiseException;
34-
import org.jruby.runtime.ThreadContext;
35-
import org.jruby.runtime.builtin.IRubyObject;
3631

3732
/**
3833
*
@@ -54,53 +49,10 @@ public static RaiseException newIOError(final Ruby runtime, final IOException ca
5449
return raise;
5550
}
5651

57-
private static RaiseException newRaiseException(final Ruby runtime,
58-
final RubyClass errorClass, final Throwable cause) {
52+
private static RaiseException newRaiseException(final Ruby runtime, final RubyClass errorClass, final Throwable cause) {
5953
final String message = cause.getMessage();
6054
RaiseException raise = RaiseException.from(runtime, errorClass, message);
6155
raise.initCause(cause);
6256
return raise;
6357
}
64-
65-
public static CharSequence formatError(final RubyException error) {
66-
final StringBuilder out = new StringBuilder(128);
67-
appendError(error, out); return out;
68-
}
69-
70-
private static void appendInspect(final RubyException error, final StringBuilder out) {
71-
final RubyClass errorClass = error.getMetaClass().getRealClass();
72-
if ( error.getMessage() != null && ! error.getMessage().isNil() ) {
73-
out.append("#<").append( errorClass.getName() ).append(": ");
74-
out.append( error.getMessage().asString() ).append('>');
75-
}
76-
else {
77-
out.append( errorClass.getName() );
78-
}
79-
}
80-
81-
public static void appendError(final RubyException error, final StringBuilder out) {
82-
appendInspect(error, out);
83-
appendBacktrace(error, out.append('\n'));
84-
}
85-
86-
public static void appendBacktrace(final RubyException error, final StringBuilder out) {
87-
appendBacktrace(error, 0, out);
88-
}
89-
90-
public static void appendBacktrace(final RubyException error, final int skip,
91-
final StringBuilder out) {
92-
final ThreadContext context = error.getRuntime().getCurrentContext();
93-
final IRubyObject backtrace = error.callMethod(context, "backtrace");
94-
if ( ! backtrace.isNil() ) {
95-
final RubyArray<?> trace = backtrace.convertToArray();
96-
out.ensureCapacity(out.length() + 24 * trace.getLength());
97-
for ( int i = skip; i < trace.getLength(); i++ ) {
98-
IRubyObject stackTraceLine = trace.eltInternal(i);
99-
if ( stackTraceLine instanceof RubyString ) {
100-
out.append("\tfrom ").append(stackTraceLine).append('\n');
101-
}
102-
}
103-
}
104-
}
105-
10658
}

src/spec/ruby/jruby/rack/logger_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@
9797
expect( real_logger.formatting? ).to be false
9898
end
9999

100+
it 'logs exception with trace when passed as argument' do
101+
begin
102+
raise IndexError.new('TEST')
103+
rescue => e
104+
logger.debug(e)
105+
end
106+
expect( real_logger.logged_content ).to match /^DEBUG.*?IndexError.*?TEST.*?at.*?logger_spec.rb.*/m
107+
end
108+
100109
it 'handles constant resolution (for Rails compatibility)' do
101110
expect( logger.class::DEBUG ).to eql 0
102111
expect( logger.class::FATAL ).to eql 4

0 commit comments

Comments
 (0)