I'm new to Grails and I'm trying to configure Log4j so it logs the exact file and line where the log call occured. No pattern works as the conversionPattern
! It seems Grails wraps the logger in a way that Log4j doesn't see the real source of the call.
I'm aware of this thread, but I'm not sure how to create a custom appender. I just can't believe nobody already developed something to fix this issue!
I'm open to any suggestions :
- Does using something else than Log4j work in Grails to get the actual file+line (Logback?)?
- Anyone with an existing "custom appender" he's willing to share?
Thanks in advance!
I actually did it by myself. I guess I should do a proper Grails plugin for it, but I'm still not comfortable enough with Grails to be sure the code will always work. I tested it by logging from a Controller and from a Service, using Grails 2.2.4, and it seems to work well.
It works by checking the stacktrace to find the actual file and line where the call occurred and then it adds this information in the MDC thread context. Values added to
MDC
can be used by the (other) appenders using the%X{fileAndLine}
token.Here's the code and the javadoc (read it!) :
Let me know if something is not clear or if you find a way to improve it!