Grails应用中Logback未生成文件日志问题求助
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 returningfalse(make sure you're starting the app in dev mode—Grails defaults to this, but check for startup flags like--environment prod). - Or
targetDirisnull(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
targetDirpath you saw in the debug print. If the directory doesn't exist, create it manually (or rely on thecreateParents = truesetting 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
chmodto 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.xmlor additionallogback.groovyfiles insrc/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

