1

hey I was wondering if it possible to have the same output in Console as in the file output.

Here is my XML config.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" name="log4j2 Logs">
    <Properties>
        <Property name="basePath">./logs</Property>
    </Properties>   

  <Appenders>     
    <RollingFile name="file" fileName="${basePath}/ActivateMaintenancePage.logs" 
                             filePattern="${basePath}/ActivateMaintenancePage-%d{yyyy-MM-dd}">
      <PatternLayout header="LOGGING START%n%n" footer="%n%nLOGGING END"
                     pattern="%3sn %30d{DEFAULT}    [%M] %-7level %c{30} - %m%n" />

      <Policies>      
        <OnStartupTriggeringPolicy minSize="0"/>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="500 MB"/>
      </Policies>
    </RollingFile>


    <Console name="console" target="SYSTEM_OUT">
      <PatternLayout header="LOGGING START%n%n" footer="%n%nLOGGING END"
                     pattern="%3sn %30d{DEFAULT}    [%M] %-7level %c{30} - %m%n" />
    </Console>     
  </Appenders>    


  <Loggers>             
    <Root level="trace">        
        <AppenderRef ref="console" level="error"/>  
        <AppenderRef ref="file" level="trace"/>         
    </Root>
  </Loggers> 
</Configuration>

Output in RollingFile

  1        2017-07-25 11:16:36,762    [initializeChrome] INFO    class testNG.SimSettings - Web Chrome driver is now initilized.
  2        2017-07-25 11:16:36,762    [lambda$0] INFO    class testNG.SimSettings - Opening... http://msrvaq11vm.technomedia.ca/sigal_60/2017sp1/sp_polymont/_sim/PROD Sp2 2016 Sim...
  3        2017-07-25 11:16:47,926    [initilizeAllElements] INFO    class testNG.SimSettings - All @FindBy elements have been initilized.
  4        2017-07-25 11:16:48,006    [change1stLevelFrame] INFO    class testNG.SimSettings - Changing target to 1st level frame.
  5        2017-07-25 11:16:49,719    [enterCredentials] INFO    class testNG.SimSettings - UserName & Password: Success!

and empty in Console. But now if I change

<AppenderRef ref="console" level="error"/>  

to "trace"

It will be 2,4,6....in console and in my file it will be 1,3,5,7... which is easily understandable.

But my question is how can we have both the same log-level (trace) output in Console and File? (adding tag with package name and level did not work)

Related to this question: log4j2 xml configuration - Log to file and console (with different levels)

Ya Yan
  • 63
  • 13

1 Answers1

0

I'm not sure if I read your question correctly but it seems that you want to render some unique value in the log output, such that the same log event has the same unique value in the Console log and the File log output.

The sequence number pattern converter will increment every time a log event is rendered. The same log event is rendered separately for each Appender, so different Appenders will never have the same sequence number.

There are a number of alternatives. One idea is to include %nano nanotime in the pattern layout. This value is captured when the application makes the logging call and will be the same for all appenders. An alternative is to create a custom Log4j2 pattern converter or lookup.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114