2

I'm able to set logging in a single groovy class. But I want to send all the logs from different groovy classes to a single file. Is it possible ? Please find below my log code in my groovy class.

import groovy.sql.Sql
import org.apache.log4j.*
import groovy.util.logging.*

@Log4j
public class TibcoAppConfGenertor {

    public TibcoAppConfGenertor() {

        log.level = Level.DEBUG
        log.addAppender(new FileAppender(new TTCCLayout(), 'Tibco.log'));
        log.addAppender(new FileAppender(new TTCCLayout(), 'Tibco_Exception.log'));
        //log.info 'Simple sample to show log field is injected.'

    }




    public void generateServiceXML() {
        log.info 'generateServiceXML'

        getApplicationGroups();

    }
DevOps Junky
  • 287
  • 1
  • 5
  • 22
  • 2
    Possible duplicate of [How to initialize log4j properly?](http://stackoverflow.com/questions/1140358/how-to-initialize-log4j-properly) – Michael Easter Sep 10 '16 at 15:10

1 Answers1

1

As Groovy runs above Java you can use one of the Java Libraries. Personally I would prefer using slf4j than using log4j. The difference from log4j is explained here. If you are using Grails MVC framework than it would be a good practice to make a Service which handles all your logs.

Example for such a Service can be something like that:

public class LoggerService {

private static Logger defaultLogger=LoggerFactory.getLogger(LoggerService.class);

// Add more methods here 

}

Every class uses the logger should state the following line:

private static final Logger log = LoggerFactory.getLogger('className')

The 'className' of course should be replaced with the relevant class you uses.

Community
  • 1
  • 1
Rotem
  • 1,381
  • 1
  • 11
  • 23