You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Grails应用中Logback未生成文件日志问题求助

Troubleshooting Missing stacktrace.log in Grails with Logback

Let's walk through the common issues and fixes for your Logback configuration not generating the stacktrace.log file, even after restarting your app.

1. Verify Your Configuration Block is Actually Executing

First, let's confirm if the development mode code block is running at all. Add debug prints to your logback.groovy to check key values:

def targetDir = BuildSettings.TARGET_DIR 
println "Logback Debug: Target directory path = ${targetDir}"
println "Logback Debug: Running in development mode? ${Environment.isDevelopmentMode()}"

if (Environment.isDevelopmentMode() && targetDir != null) { 
    println "Logback Debug: Setting up FULL_STACKTRACE appender"
    appender("FULL_STACKTRACE", FileAppender) { 
        file = "${targetDir}/stacktrace.log" 
        append = true 
        createParents = true // Add this to auto-create missing parent directories
        encoder(PatternLayoutEncoder) { 
            pattern = "%level %logger - %msg%n" 
        } 
    } 
    logger("StackTrace", DEBUG, ['FULL_STACKTRACE'], false) 
} 
root(ERROR, ['STDOUT']) 

Restart your app and check the console output. If you don't see the "Setting up FULL_STACKTRACE appender" line:

  • Either Environment.isDevelopmentMode() is returning false (make sure you're starting the app in dev mode—Grails defaults to this, but check for startup flags like --environment prod).
  • Or targetDir is null (see the next fix for Grails version-specific path handling).

2. Fix Target Directory Resolution (If Needed)

In newer Grails versions (4.x+), BuildSettings.TARGET_DIR might not resolve correctly. Replace the targetDir line with a more reliable path:

// For Grails 4+
def targetDir = new File(BuildSettings.BASE_DIR, "build/target").absolutePath

Or use the Grails configuration to get the target dir:

import grails.util.Holders

def targetDir = Holders.grailsApplication.config.getProperty("grails.build.target.dir", String, "./build/target")

3. Ensure You're Using the Correct Logger Name

Your configuration creates a logger named StackTrace, but you need to explicitly use this logger in your code to send logs to the file. For example:

// In a service or controller
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class MyService {
    private static final Logger stackTraceLog = LoggerFactory.getLogger("StackTrace")

    def doSomething() {
        // This will write to stacktrace.log
        stackTraceLog.debug("Debug message for stacktrace file")
        
        // Test exception logging
        try {
            throw new RuntimeException("Test error")
        } catch (Exception e) {
            stackTraceLog.error("Error occurred", e)
        }
    }
}

If you're using Grails' injected log variable, it uses the class name as the logger name by default—this won't route to your StackTrace logger unless you reconfigure it.

4. Check Directory Permissions & Existence

  • Navigate to the targetDir path you saw in the debug print. If the directory doesn't exist, create it manually (or rely on the createParents = true setting we added earlier to auto-create it).
  • Ensure your application process has write permissions to this directory. On Linux/macOS, you might need to run chmod to adjust permissions if needed.

5. Rule Out Conflicting Configuration Files

Check if there are other Logback configuration files in your project that might be overriding your logback.groovy:

  • Look for logback.xml or additional logback.groovy files in src/main/resources, src/test/resources, or the root of your project.
  • Grails picks up the first valid Logback config file it finds, so duplicate files can cause unexpected behavior.

内容的提问来源于stack exchange,提问作者Manish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:00:52