一键导入
monitoring-logging
IMPORTANT: Load when adding monitoring, metrics, or logging. Covers Micrometer/Prometheus, SLF4J usage, CallLogging, and observability patterns.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
IMPORTANT: Load when adding monitoring, metrics, or logging. Covers Micrometer/Prometheus, SLF4J usage, CallLogging, and observability patterns.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Load when adding scheduled tasks, periodic jobs, or background coroutines. Covers the coroutine-based task pattern, error handling, and lifecycle management.
Load when configuring build pipeline, CI workflows, SonarCloud, JaCoCo, or the Gradle version catalog. Covers fatJar, ktlint, and dependency management.
Load when adding new configuration properties, modifying HOCON files, or understanding how config is loaded. Covers app.conf, application.conf, and the loading pipeline.
IMPORTANT: Load when adding error handling in routes, services, or repositories. Covers AcknowledgeBO vs exceptions, standardized responses, and proper logging.
Load when implementing multipart file upload, download, or file storage. Covers multipart parsing, validation, disk persistence, and URL generation.
IMPORTANT: Load when onboarding, planning a new feature, or understanding how the project is wired end-to-end. Covers the full stack: startup → DI → data → routing → config.
| name | monitoring-logging |
| description | IMPORTANT: Load when adding monitoring, metrics, or logging. Covers Micrometer/Prometheus, SLF4J usage, CallLogging, and observability patterns. |
error-handlingci-cd-sonarThe project uses SLF4J with SLF4J Simple backend (configured in build.gradle.kts):
implementation(libs.slf4j.api) // org.slf4j:slf4j-api
implementation(libs.slf4j.simple) // org.slf4j:slf4j-simple
import org.slf4j.LoggerFactory
private val logger = LoggerFactory.getLogger(MyClass::class.java)
Or inside a companion object (for services):
class EmailService(...) {
companion object {
private val logger = LoggerFactory.getLogger(EmailService::class.java)
}
}
Ktor also provides log on Application:
import io.ktor.server.application.log
fun Application.someFunction() {
log.info("Message")
log.error("Error", exception)
}
| Level | When to use |
|---|---|
info | Normal operations (task started, user registered) |
warn | Unexpected but non-critical situations |
error | Failures, exceptions, critical issues |
debug | Development-only details (can be noisy) |
println()All println() calls have been replaced with SLF4J. If you see one, replace it:
// BAD:
println(e.message)
println(e.stackTraceToString())
// GOOD:
logger.error("Failed to send email", e)
logger.error("Failed to save recover request", e)
Configured in plugins/Monitoring.kt to log every HTTP request at INFO level:
install(CallLogging) {
level = Level.INFO
filter { call -> call.request.path().startsWith("/") }
}
val appMicrometerRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
install(MicrometerMetrics) {
registry = appMicrometerRegistry
meterBinders = listOf(
ClassLoaderMetrics(),
JvmMemoryMetrics(),
JvmGcMetrics(),
ProcessorMetrics(),
JvmThreadMetrics(),
FileDescriptorMetrics(),
UptimeMetrics()
)
}
Available at GET /metrics — returns Prometheus-formatted text:
routing {
get("/metrics") {
call.respondText {
appMicrometerRegistry.scrape()
}
}
}
| Metric | Source | Description |
|---|---|---|
| JVM memory | JvmMemoryMetrics | Heap, non-heap, buffers |
| GC | JvmGcMetrics | Garbage collection pauses |
| Threads | JvmThreadMetrics | Thread count, states |
| CPU | ProcessorMetrics | CPU usage |
| Classes | ClassLoaderMetrics | Loaded/unloaded classes |
| Uptime | UptimeMetrics | Application uptime |
| File descriptors | FileDescriptorMetrics | Open FD count |
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
import io.micrometer.core.instrument.Counter
// Inject or access the registry:
val registry: PrometheusMeterRegistry = appMicrometerRegistry
val counter = Counter.builder("api.sounds.uploads")
.description("Number of sound uploads")
.register(registry)
// In your code:
counter.increment()
application.conf)Ktor application logging is configured via application.conf:
ktor {
// ...
}
SLF4J Simple logging can be configured via src/main/resources/simplelogger.properties or system properties:
org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.logFile=System.out
logger.error("Failed to X for user {}", userId, e)logger.info("User {} logged in", username) (not string concatenation)println() for logging — always use SLF4J{} placeholderslogger.error("msg", exception)plugins/Monitoring.kt — Micrometer Prometheus + CallLogging setupbuild.gradle.kts — SLF4J dependenciesgradle/libs.versions.toml — logback and SLF4J version references