Discussion:
How to obtain LogLevel set from command line?
Robert Winch
2011-05-03 05:14:25 UTC
Permalink
Is there a good way to obtain the LogLevel that is specified from the
command line? For example, if the -d switch is set I would want to get
LogLevel.DEBUG. I have tried using the script below but I alway get a null
result.

build.gradle

task viewLogLevel {
println "Log Level : ${logging.level}"
}

$ gradle -v
------------------------------------------------------------
Gradle 1.0-milestone-3
------------------------------------------------------------

Gradle build time: Monday, 25 April 2011 5:40:11 PM EST
Groovy: 1.7.10
Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Ivy: 2.2.0
JVM: 1.6.0_24 (Sun Microsystems Inc. 19.1-b02)
OS: Linux 2.6.38-8-generic i386

$ gradle viewLogLevel
...other logs...
Log Level : null

$ gradle -i viewLogLevel
...other logs...
Log Level : null

$ gradle -d viewLogLevel
...other logs...
00:11:28.777 [QUIET] [system.out] Log Level : null

Can someone point out what I am doing wrong?
Rob
Adam Murdoch
2011-05-03 07:58:16 UTC
Permalink
Is there a good way to obtain the LogLevel that is specified from the command line?
Could you give us some more details of what you want to use the log level for? There might be some other way to achieve what you want.
For example, if the -d switch is set I would want to get LogLevel.DEBUG. I have tried using the script below but I alway get a null result.
build.gradle
task viewLogLevel {
println "Log Level : ${logging.level}"
}
$ gradle -v
------------------------------------------------------------
Gradle 1.0-milestone-3
------------------------------------------------------------
Gradle build time: Monday, 25 April 2011 5:40:11 PM EST
Groovy: 1.7.10
Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Ivy: 2.2.0
JVM: 1.6.0_24 (Sun Microsystems Inc. 19.1-b02)
OS: Linux 2.6.38-8-generic i386
$ gradle viewLogLevel
...other logs...
Log Level : null
$ gradle -i viewLogLevel
...other logs...
Log Level : null
$ gradle -d viewLogLevel
...other logs...
00:11:28.777 [QUIET] [system.out] Log Level : null
Can someone point out what I am doing wrong?
Rob
--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com
Robert Winch
2011-05-03 19:46:32 UTC
Permalink
Is there a good way to obtain the LogLevel that is specified from the command line?
Could you give us some more details of what you want to use the log level
for? There might be some other way to achieve what you want.
Thank you for your prompt response. I would be happy to elaborate on what I
am trying to do.

I am currently using gradle and the jetty plugin to run integration tests
against the interaction of Spring Security's CAS Sample war and a third
party CAS Server war. The CAS server uses slf4j log4j implementation, with
an jcl-over-slf4j. It has a default logging level of INFO for its classes.
The sample CAS service war is using slf4j with jcl-over-slf4j and logback.
It has a logging level of DEBUG. My goal is to have logging be configured in
three different ways:

1) If the wars are running as part of an integration test, set the log
levels to ERROR. I would also be ok with no logging from the wars in this
situation.
2) If a problem occurs in the integration tests, I would like to be able to
easily change the log level of the wars to DEBUG. Preferably by providing
one of the gradle command line arguments (i.e. the -i argument).
3) If the wars are running outside of the integration tests, I would like
the log levels of the wars to be the defaults mentioned above and the gradle
build to be LIFECYCLE.

NOTE: I also do not want to change the default log levels, so that if the
wars are used outside of gradle they remain the default log levels.

I have tried a number of things to accomplish this:

1) I have tried using logging.captureStandardOutput LogLevel.INFO, but
logging from the wars continue to be displayed. I found a few JIRA's that
may be the cause [1] [2], but perhaps I have misunderstood what this feature
does or I am doing something incorrectly.

2) I notice that when running with the CAS server with the gradle jetty
plugin I see a message stating "SLF4J: Class path contains multiple SLF4J
bindings". This issue appears to have been reported in one of the previously
mentioned JIRAs [1]. I tried a few things to resolve this:

a) I tried to exclude the slf4j-log4j jar from the CAS server so that
gradle's logback gets picked up thinking that might get #1 to work. However,
I get a "ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder". I have
found there are some other issues with both removing the log4j dependency or
replacing it with the log4j-over-slf4j jar since it appears that the war
requires some classes in log4j that are not in log4j-over-slf4j. In short, I
do not thinking that removing the slf4j-log4j implementation on the cas
server is going to work.

b) I have tried Including a logback-test.xml to toggle the logging for the
web application since logback is the first slf4j implementation listed, but
this did not change the log levels.

3) This brings me to where I am at now. I noticed that if I modify the
log4j.xml that comes in the CAS Server webapp that I can control the
logging. Therefore I am attempting to see what log level has been specified
on the command line, so that I can perform filtering on a log4j.xml resource
I am overlaying with the CAS war.

I threw together a quick example of a gradle build where the war is
displaying logs that I wish to suppress below. If I run gradle casServer I
would like logging of the CAS Server to be set to ERROR. However, WARN/INFO
statements are seen. Note: This example does not have integration tests nor
does it include the cas sample service. However, I do not have the build on
my current computer. I believe if this problem could be solved that I could
take it from there. If desired, I can push a branch out tonight to
illustrate the whole problem in action.

apply plugin: 'jetty'

configurations {
casServer
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
casServer "org.jasig.cas:cas-server-webapp:***@war"
}

logging.captureStandardOutput LogLevel.INFO

task casServerOverlay(type: Sync) {
war = configurations.casServer.resolve().toArray()[0]
warName = war.name.replace('.war','-custom')
customWar = file("$buildDir/tmp/${warName}.war")
tmpDir = file("$buildDir/tmp/$warName")
overlay = file('src/main/webapp')

inputs.files files(war,overlay)
outputs.files files(customWar,tmpDir)

from zipTree(war)
// uncomment below for 2.a
// exclude 'WEB-INF/lib/slf4j-log4j*.jar'
from overlay
into tmpDir

doLast {
ant.zip(destfile: customWar, baseDir: tmpDir)
}
}

task casServer (type: org.gradle.api.plugins.jetty.JettyRunWar, dependsOn:
casServerOverlay) {
println '!!!!!!! THIS IS HIDDEN UNLESS -i or higher is set but Logging
of the war is always on !!!!!!!'
webApp = casServerOverlay.customWar
}

[1] http://issues.gradle.org/browse/GRADLE-669 and
http://issues.gradle.org/browse/GRADLE-897
[2] http://issues.gradle.org/browse/GRADLE-1081

Thanks again,
Rob
Robert Winch
2011-05-04 04:49:21 UTC
Permalink
I went ahead and pushed out a branch that should demonstrate the issue in
its entirety [1]. If you navigate to samples/cas and run the integrationTest
task, you will notice that the logging of the CAS Server is occurring
despite gradle/javaprojects.gradle invoking
logging.captureStandardOutput(LogLevel.INFO). I am currently suppressing the
logging of the cas sample by adding a logback-test.xml to the classpath of
jetty when the integration tests are ran. As described earlier, I would like
to be able to use -i to enable logging when doing the integration tests.

PS: Please pardon the build as it is the middle of being re-factored a bit.

[1]
http://git.springsource.org/~rwinch/spring-security/rwinchs-spring-security/commits/casitests

Thanks again for you help,
Rob
TheKaptain
2012-12-28 03:24:11 UTC
Permalink
You should just be able to get this by querying the Project.logger instance.
API is at
http://gradle.org/docs/current/javadoc/org/gradle/api/logging/Logger.html#isEnabled%28org.gradle.api.logging.LogLevel%29




--
View this message in context: http://gradle.1045684.n5.nabble.com/How-to-obtain-LogLevel-set-from-command-line-tp4366397p5710591.html
Sent from the gradle-user mailing list archive at Nabble.com.

Loading...