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