0

I'm using Grails 2.2.4 with log4j configured in a rather standard way (http://grails-dev.blogspot.com/2012/09/setting-up-log4j-configuration-in.html) and it works very well (details at the end of the question).

I would love to have complete information about anything that happens to objects with particular class in a separate files, so I'd like to be able to do something like configure log4j to log to custom file at runtime, but I can't figure out how to do this in grails. It would be great to do something like

package com.mydomain.service

import org.apache.commons.logging.LogFactory

class SampleService {

    private static final log = LogFactory.getLog(this)

    def index(Long id) {
        log.setFileName("object-${id}.log")
        log.info("The log message.")
    }
}

(I know method setFileName doesn't exist; I'm talking about the general approach) Is it even possible?

And, if it is possible, can you tell me anything about performance? Will logging around 500 (< 1000 lines long) files a day slow down the execution of the application?

Thanks in advance.


My log4j configuration:

Within the code I log messages like this:

package com.mydomain.service

import org.apache.commons.logging.LogFactory

class SampleService {

    private static final log = LogFactory.getLog(this)

    def index() {
        log.info("The log message.")
    }
}

with config file containing

log4j = {
    error 'org.codehaus.groovy.grails.web.servlet',        // controllers
        'org.codehaus.groovy.grails.web.pages',          // GSP
        'org.codehaus.groovy.grails.web.sitemesh',       // layouts
        'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
        'org.codehaus.groovy.grails.web.mapping',        // URL mapping
        'org.codehaus.groovy.grails.commons',            // core / classloading
        'org.codehaus.groovy.grails.plugins',            // plugins
        'org.codehaus.groovy.grails.orm.hibernate',      // hibernate integration
        'org.springframework',
        'org.hibernate',
        'net.sf.ehcache.hibernate'

    warn 'org.springframework',
        'org.hibernate',
        'groovyx.net.http'

    all 'myApp'
}
Community
  • 1
  • 1
pawels
  • 1,070
  • 1
  • 10
  • 16

1 Answers1

0

You can create an external config file which can be modified at runtime and make grails load that file. This way you can modify your logging in your app dynamically at runtime. Take a look at this link.

Sandeep Poonia
  • 2,158
  • 3
  • 16
  • 29
  • Thank you for the input, this link is very interesting; though I think that changing configuration every time I want to log something is a bit of overkill and adds unnecessary complexity to code wich will be hard to understand for others. Maybe I'll just give up the idea at all. Thanks again. – pawels Feb 01 '16 at 17:52