3

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>
Community
  • 1
  • 1
paul
  • 4,333
  • 16
  • 71
  • 144
  • What is your logging configuration? Why you cannot set let say warn level for console and debug level for file? – Betlista Dec 24 '15 at 08:48
  • I think that is doable (I dont know how to but I will try), but how to let maven know that which to show in console and which not? – paul Dec 24 '15 at 08:49
  • It's not about maven at all... You have to configure logging framework and basically it will append messages to both file and console, but you can specify different log level, so as a result you will see more messages in file and less in console... – Betlista Dec 24 '15 at 08:52
  • You can start reading here - https://logging.apache.org/log4j/1.2/manual.html – Betlista Dec 24 '15 at 08:54
  • I mean to say I want to pass on a variable with my maven command which will disable any logs on console and if I am not passing that variable with maven command then I will see the logs in console. – paul Dec 24 '15 at 08:57

2 Answers2

1

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

Prim
  • 2,880
  • 2
  • 15
  • 29
0

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

Betlista
  • 10,327
  • 13
  • 69
  • 110
  • so command would be `mvn test -DLEVEL=DEBUG` and I won't see logs in console? – paul Dec 24 '15 at 10:21
  • Not really, my logic was twisted, but feel free to modify it to meet your needs. The example above show only messages with level `WARN` and above by default and if you want to see `DEBUG` messages, you have to use `-DLEVEL=DEBUG` – Betlista Dec 24 '15 at 11:26
  • Property tag is written some other way in my `log4j2.xml`, see I have pasted in above. I dont understand how to modify or use it in a way to get logs in console or stop logs in console. – paul Dec 28 '15 at 04:45
  • Sorry, me being new to this. So which line/tag in my `.xml` says that console level is `fixed-static`. – paul Dec 30 '15 at 10:35
  • This one ` – Betlista Dec 30 '15 at 10:55