Objective: to have a seperate logger exclusively for writing some special messages to a seperate file.
Usually log4j writes to logs based on package & class names, as covered by the Log4J Configuration Documentation (In fact, some things have changed since Grails 1.1)
But we sometimes need a logger for logging special messages, though we may log other messages to the usual log within the same class.
So in Config.groovy, we define a new rolling file appender named ‘splMsgFile’
log4j = { appenders { console name:'stdout' rollingFile name:'msgFile', file: logDirectory + './appln.log', threshold: org.apache.log4j.Level.INFO, maxFileSize:"1MB", maxBackupIndex: 10, 'append':true, layout:pattern(conversionPattern: "%d{dd MMM yyyy HH:mm:ss,SSS} [%t] %-5p [%c] - %m%n") rollingFile name:'splMsgFile', file: './splMsg.log', threshold: org.apache.log4j.Level.INFO, maxFileSize:"1MB", maxBackupIndex: 10, 'append':true, layout:pattern(conversionPattern: "%d{dd MMM yyyy HH:mm:ss,SSS} [%t] %-5p [%c] - %m%n") }
Then we add an info level log below, after the appenders section, with the name splMsgLog
debug msgFile:"splMsgLog" // name of the log category is splMsgLog...
In any part of the project, independent of package/class, apart from the usual logs, we can write to this additional log as follows:
def msgLog = Logger.getLogger("splMsgLog") msgLog.info("this is a special message1")
So while there may be other log messages that come in the usual log file, the splMsgLog contains only the ones logged with this one!