Print Stack Trace
ID |
kotlin.print_stack_trace |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Kotlin |
Tags |
best-practice, logging |
Description
Reports calls to Throwable.printStackTrace() (the argument-less form). The
trace is written to System.err, bypassing the application’s structured
logging pipeline.
Rationale
In a deployed service System.err ends up in container stdout/stderr where
the trace cannot be filtered by log level, lacks the usual metadata (MDC,
correlation id), and may be silently truncated or split by the runtime.
// Bad
try { doWork() } catch (e: Exception) {
e.printStackTrace()
}
// Good
private val log = LoggerFactory.getLogger(MyService::class.java)
try { doWork() } catch (e: Exception) {
log.error("doWork failed", e)
}
Exceptions
The two-arg printStackTrace(PrintStream) / printStackTrace(PrintWriter)
overloads are intentionally not flagged: when the caller passes an explicit
sink they have already chosen a routing target.
Remediation
Route the exception through a logging facade — slf4j-api, kotlin-logging, or
Android Log — so the trace is captured, formatted, and shipped to the same
sink as the rest of the application output.