Problem With log Property From Static Context
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
```groovy
class DemoService {
static void someMethod() {
log.error 'This is an error message'
}
}
```
That code won't compile...
```
/Users/jeffbrown/logdemo/grails-app/services/logdemo/DemoService.groovy: 6: Apparent variable 'log' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'log' but left out brackets in a place not allowed by the grammar.
@ line 6, column 9.
log.error 'This is an error message'
^
```
This does compile...
```groovy
class DemoService {
static void someMethod() {
DemoService.log.error 'This is an error message'
}
}
```
Another way to make this work is to mark the class with `@Slf4j`.
It is a little unusual to have static methods in a Grails artifact like this, but it probably should be allowed to work.
Contributor guide
Research direction
Start with the Grails service artifact behavior shown in the DemoService example, focusing on how the static someMethod resolves the implicit log property. Compare the failing unqualified access with DemoService.log and the @Slf4j alternative; done means the original static call compiles and logs without requiring either workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100