Reputation: 4487
I am running my Selenium Automation tests using Maven
. From time of execution till end I see so many logs.
I came to know with this code that only .info
warnings and .warn
goes to console and .debug
doesn't.
public static void main(String[] args) {
Logger log = LogManager.getLogger();
log.debug("its a debug message");
log.info("its a info message");
log.warn("its a warning message");
}
Output:
2015-12-24 13:58:21,166 ERROR Logger contains an invalid element or attribute "append"
[INFO ] 2015-12-24 13:58:21.245 [main] DebuggerTest - its a info message
[WARN ] 2015-12-24 13:58:21.247 [main] DebuggerTest - its a warning message
Now I want to pass on a variable in along with my mvn
command that will switch on/off any logs in console.
Something like: mvn test --debugging -false
So that logs can be seen in generated logs file but not in console.
More info: I want something like given here: How to initialize log4j properly?
here user "MATH" advised to use :
Logger.getRootLogger().setLevel(Level.WARN);
if don't want to see debug logs
I want to enable/disable this from mvn
command line.
More info 2:
this is how my log4j2.xml looks:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<RollingFile name="trace-log" fileName="${log-path}/rnf-info.log"
filePattern="${log-path}/rnf-trace-%d{yyyy-MM-dd}.log" append="false">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="debug-log" fileName="${log-path}/rnf-debug.log"
filePattern="${log-path}/rnf-debug-%d{yyyy-MM-dd}.log" append="false">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.rnf" level="debug" additivity="false" append="false">
<appender-ref ref="trace-log" level="info"/>
<appender-ref ref="debug-log" level="debug"/>
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
</Loggers>
</Configuration>
Upvotes: 3
Views: 4744
Reputation: 10547
What you can try to use are variables
log4j2 configuration
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LEVEL">WARN</Property> <!-- default value -->
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="${sys:LEVEL}">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
and to change the default value from WARN to DEBUG you can use
-DLEVEL=DEBUG
Edit:
In your configuration the level for console is fixed - static, set to INFO, but you want to have a dynamic behavior.
You need to add another property
<Properties>
<Property name="log-path">logs</Property>
<Property name="LEVEL">WARN</Property> <!-- default value -->
</Properties>
In which I set WARN as a default level, so by default, there will be less messages in console.
Then I'm referencing the property in Root logger configuration ${sys:LEVEL}
<Root level="${sys:LEVEL}">
but it can be specified from command line as standard JVM parameter -D...
.
So if you want more messages in console, you will run mvn test -DLEVEL=DEBUG
Upvotes: 0
Reputation: 2978
I think you could use -Dlog4j.configuration=<path>
to set the configuration of the logger to whatever you want directly on command line.
See documentation here: http://logging.apache.org/log4j/1.2/manual.html
Upvotes: 1