How to use log4j in gradle task

I am new to gradle. I tried using the log4j annotation. Log statements work great with the simpe Groovy file. But when I try to use them with a gradle task. It is not printed.

Groovy file:

@Log4j class HelloWorldLog { static void main(args) { log.info "Hello world" } } 

Gradle Task:

  @Log4j class DeployTask extends DefaultTask { @TaskAction def deployTaskAction(){ log.info "DeployTask!!!" } } 

and I use the same log4j.properties file for both.
Logs are printed in the console, not in the log file. How to redirect logs to a log file?

thanks

+4
source share
2 answers

Save the log4j.properties file in src/main/resources and create your project.

Add this line:

 war.from('src/main/resources'){ include('log4j.properties')} 

to the main gradle.build. Then create it again and run.

+2
source

Put log4j.properties with the following contents in the classpath (maybe src / main / resources):

 log4j.rootLogger=INFO, file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\\myLog.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n 
0
source

All Articles