DevOps Junky DevOps Junky - 5 months ago 22
Groovy Question

how to set logging in groovy project (logs from all classes) to a single file

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.*

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'));
// 'Simple sample to show log field is injected.'


public void generateServiceXML() { 'generateServiceXML'




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.