Log4j not initialized while running maven execute

543 Views Asked by At

I'm trying to run a groovy script with Log4j. The script runs fine, but log4j is not initialized. I've placed log4j.properties in src/main/resources and also included resources in build section in pom.xml. Below is a part of pom.xml

        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <version>1.5</version>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.gmaven.runtime</groupId>
                    <artifactId>gmaven-runtime-2.0</artifactId>
                    <version>1.5</version>
                </dependency>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy</artifactId>
                    <version>2.1.9</version>
                </dependency>
                <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>1.2.17</version>
                </dependency>
            </dependencies>
            <configuration>
                <providerSelection>2.0</providerSelection>
                <source>
                    ${pom.basedir}/src/main/groovy/Hello.groovy
                </source>
            </configuration>


            <executions>
                <execution>
                    <goals>
                        <goal>testCompile</goal>
                        <goal>compile</goal>
                        <goal>generateStubs</goal>
                        <goal>execute</goal>
                    </goals>
                </execution>

            </executions>
        </plugin>

I used following command to execute the script:

mvn org.codehaus.gmaven:gmaven-plugin:1.5:execute

I get following warning at the end of script execution:

log4j:WARN No appenders could be found for logger (...).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
1

There are 1 best solutions below

0
user944849 On

I did something similar, had to explicitly initialize log4j at the top of my script.

import org.apache.log4j.PropertyConfigurator

def config = new ConfigSlurper().parse( new File( 
                  "${basedir.canonicalPath}/src/main/scripts/log4j.groovy" ).toURL())
PropertyConfigurator.configure(config.toProperties())

and then normal logging (in Java calls for me) worked fine.

Here's my test log4j.groovy file.

log4j.appender.stdout = "org.apache.log4j.ConsoleAppender"
log4j.appender."stdout.layout" = "org.apache.log4j.PatternLayout"
log4j.appender."stdout.layout.ConversionPattern" = "%-5p [%c{1}]: %m%n"
log4j.rootLogger="error,stdout"
log4j.logger.com.myCompany = "info,stdout"
log4j.additivity.com.myCompany = false