Moving from Log4j 1 to Log4j 2 questions

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Moving from Log4j 1 to Log4j 2 questions

Postby tkilshaw1553613063 » Mon Jan 17, 2022 7:01 pm

When we first started using Servoy about 2 and a half years ago we did not have logging working from our Java classes to the Servoy console or to its own log file. Either Tuan or Sean or maybe both of them got that working, at least for writing to our own log file. Our Java classes never logged to the Servoy console.

We were and still are using log4j 1. The solution at that time was to add this to the finally clause of the onSolutionOpen method for our login module.

Code: Select all
finally
  {
    // stops automatic saves

    databaseManager.setAutoSave( false );

    // *** set up our Java code logging file ***

    // resets stuff so it doesn't log the same thing multiple times

    Packages.org.apache.log4j.BasicConfigurator.resetConfiguration( );

    //create layout

    var pl = new Packages.org.apache.log4j.PatternLayout( "%-5p %d{ISO8601} [%t] - %m%n" );

    //create rolling appender

    var rfaPath = application.getUserProperty( "qfiJavaLogPath" );
    var rfa = new Packages.org.apache.log4j.RollingFileAppender( pl, rfaPath + 'qfi-java.log' );
    rfa.setThreshold( Packages.org.apache.log4j.Level.INFO );
    rfa.setMaxFileSize("10MB");
    rfa.setMaxBackupIndex(10);
   
     //set up root config with appender

    Packages.org.apache.log4j.BasicConfigurator.configure( rfa );


The servoy.properties file also had these values set:

Code: Select all
> log4j.appender.A2=org.apache.log4j.RollingFileAppender
> log4j.appender.A2.MaxBackupIndex=5
> log4j.appender.A2.MaxFileSize=10MB
> log4j.appender.A2.file=%%servoy_app_server_dir%%\\qfi.log
> log4j.appender.A2.layout=org.apache.log4j.PatternLayout
> log4j.appender.configservlet=com.servoy.j2db.server.util.SlidingWindowAppender
> log4j.appender.configservlet.layout=com.servoy.j2db.server.util.Log4JHTMLTableLayout
> log4j.appender.configservlet.layout.dateTimeFormat=yyyy-MM-dd HH\:mm
> log4j.appender.configservlet.windowSize=1000
> log4j.appender.debugconsole=com.servoy.j2db.server.util.DebugConsoleAppender
> log4j.appender.debugconsole.Threshold=WARN
> log4j.appender.debugconsole.layout=org.apache.log4j.EnhancedPatternLayout
> log4j.appender.debugconsole.layout.ConversionPattern=%p %c - %m
> log4j.appender.file=org.apache.log4j.RollingFileAppender
> log4j.appender.file.File=%%servoy_app_server_dir%%\\servoy_log.txt
> log4j.appender.file.MaxBackupIndex=1
> log4j.appender.file.MaxFileSize=10MB
> log4j.appender.file.layout=org.apache.log4j.PatternLayout
> log4j.appender.file.layout.ConversionPattern=%d %p [%t] %c - %m [%X{clientid} %X{solution}]%n
> log4j.appender.stdout=org.apache.log4j.ConsoleAppender
> log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
> log4j.appender.stdout.layout.ConversionPattern=%d %p [%t] %c - %m%n
> log4j.debug=false
> log4j.logger.com.org.sablo.websocket=WARN
> log4j.logger.com.servoy.j2db.dataprocessing.editedRecords=WARN
> log4j.logger.com.servoy.j2db.datasource=WARN
> log4j.logger.com.servoy.j2db.datasource.ClientManager=WARN
> log4j.logger.com.servoy.j2db.persistence.XMLExporter=WARN
> log4j.logger.com.servoy.j2db.persistence.XMLInMemoryImportHandlerVersions11AndHigher=WARN
> log4j.logger.com.servoy.j2db.server=WARN
> log4j.logger.com.servoy.j2db.server.main.WebServer=WARN
> log4j.logger.com.servoy.j2db.server.ngclient.property.types=WARN
> log4j.logger.com.servoy.j2db.server.persistence.Server=WARN
> log4j.logger.com.servoy.j2db.util.Debug=WARN
> log4j.logger.org.apache.wicket=WARN
> log4j.logger.org.apache.wicket.request.target.component.listener.BehaviorRequestTarget=ERROR
> log4j.logger.org.sablo=WARN
> log4j.logger.org.sablo.specification.property=WARN
> log4j.rootCategory=WARN, file, configservlet


I began at first by trying to emulate the log4j 1 code in the onSolutionOpen method in log4j2. As part of that, for test purposes I changed our Login class to use a logger made from log4j 2. So this:

Code: Select all
> import org.apache.log4j.Logger;


was changed to this:
Code: Select all
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;


and this:
Code: Select all
  //private static Logger oLogger = Logger.getLogger( Login.class.getName() );


to this:
Code: Select all
private static Logger oLogger = LogManager.getLogger( Login.class.getName() );


and these statements were added to the Login class DoLogin method just for testing:

Code: Select all
>     oLogger.info( "In Login. Should be an info statement!");
>     oLogger.warn( "In Login. Should be a warn statement!");
>     oLogger.info( "In Login. Should be a second info statement!");
>     oLogger.error( "In Login. Should be an error statement!");


I noticed that when I logged in and this code was called, I saw in the Servoy console:

Code: Select all
> WARN com.quantechsoftware.qfi.Login - In Login. Should be a warn statement!
> ERROR com.quantechsoftware.qfi.Login - In Login. Should be an error statement!


And when I checked, those log statements were also sent to servoy_log.txt!

So this is new and valuable. Until now we were unable to ever see log statements from our Java code in the Servoy console. This also avoids the need for a separate C:\Servoy_202006\developer\qfi-java.log file.

This continued to work when I removed all of the new log4j 2code I had been messing with in onSolutionOpen. So I turned my attention to the two files in my C:\Servoy_202006\application_server called log4j.xml and log4j2-new.xml.

As far as I can see log4j2-new.xml is not used. Our current log4j.xml looks like this:

Code: Select all
> <?xml version="1.0" encoding="UTF-8"?>
<Configuration debug="false" status="WARN">
   <Appenders>
     <Console name="stdout" target="SYSTEM_OUT">
       <PatternLayout pattern="%d %p [%t] %c - %m%n"/>
     </Console>
     <DebugConsole name="debugconsole">
       <PatternLayout pattern="%p %c - %m"/>
     </DebugConsole>
     <SlidingWindow name="configservlet" windowSize="1000" dateTimeFormat="yyyy-MM-dd HH:mm"/>
     <RollingFile name="file" fileName="${log4j:configParentLocation}/servoy_log.txt" filePattern="${log4j:configParentLocation}/servoy_log-%i.txt.zip" immediateFlush="true" append="true">
       <Policies>
         <SizeBasedTriggeringPolicy size="10MB"/>
       </Policies>
       <PatternLayout pattern="%d %p [%t] %c - %m [%X{clientid} %X{solution}]%n"/>
     </RollingFile>
     <Async name="asyncfile">
       <AppenderRef ref="file"/>
     </Async>
   </Appenders>
   <Loggers>
     <Logger name="com.servoy.j2db.util.Debug" level="WARN"/>
     <Logger name="com.servoy.j2db.dataprocessing.editedRecords" level="WARN"/>
     <Logger name="com.servoy.j2db.server.persistence.Server" level="WARN"/>
     <Logger name="com.servoy.j2db.persistence.XMLInMemoryImportHandlerVersions11AndHigher" level="WARN"/>
     <Logger name="com.servoy.j2db.persistence.XMLExporter" level="WARN"/>
     <Logger name="com.servoy.j2db.server" level="WARN"/>
     <Logger name="com.servoy.j2db.server.main.WebServer" level="WARN"/>
     <Logger name="com.servoy.j2db.datasource" level="WARN"/>
     <Logger name="com.servoy.j2db.datasource.ClientManager" level="WARN"/>
     <Logger name="org.sablo" level="WARN"/>
     <Logger name="org.sablo.specification.property" level="WARN"/>
     <Logger name="org.sablo.websocket" level="WARN"/>
     <Logger name="com.servoy.j2db.server.ngclient.property.types" level="WARN"/>
     <Logger name="org.apache.wicket" level="WARN"/>
     <Logger name="org.apache.wicket.request.target.component.listener.BehaviorRequestTarget" level="ERROR"/>
     <Logger name="com.servoy.automation.jsunit.runner.ImportClient" level="WARN"/>
     <Logger name="com.servoy.automation.jsunit.SolutionJSTestSuite" level="WARN"/>
     <Root level="WARN">
       <AppenderRef ref="asyncfile"/>
       <AppenderRef ref="configservlet"/>
       <AppenderRef ref="debugconsole"/>
       <!-- <AppenderRef ref="stdout" /> -->
     </Root>
   </Loggers>
</Configuration>


When I changed all WARN values to INFO, restarted Servoy Developer and logged in, all four logging statements came through, the two INFOs, the WARN and the ERROR.

This leaves us with the following issues and questions:

- if we change all the WARNs to INFOs in the log4j.xml file we get too much stuff that we don't want in the log.

Is there a way to limit the INFO setting to just our Java classes/jars?

We have two jar files located in my Windows at C:\Servoy_202006\application_server\plugins. And it is the 202006 version of Servoy we are using.

- once we change over to using log4j 2 in our Java code am I correct that we will no longer need the log4j code in the onSolutionOpen/finally clause. and that we could also remove the log4j stuff in the servoy.properties file too?

Your thoughts on this much appreciated,

Terry
tkilshaw1553613063
 
Posts: 47
Joined: Tue Mar 26, 2019 5:11 pm

Re: Moving from Log4j 1 to Log4j 2 questions

Postby jcompagner » Mon Jan 17, 2022 8:25 pm

why not keep everything on WARN and add your own logger on your own class? like

<Logger name="com.quantechsoftware.qfi.Login" level="INFO"/>

in the log4j xml file?
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8828
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: Moving from Log4j 1 to Log4j 2 questions

Postby tkilshaw1553613063 » Tue Jan 18, 2022 6:12 pm

Johan,

that sounds interesting.
Can you give more details?
Does this go to a separate file?
Can I do that on the package level with "com.quantechsoftware.qfi"?

As you can see, the log4j stuff is not immediately comprehensible to me!

thanks,

Terry
tkilshaw1553613063
 
Posts: 47
Joined: Tue Mar 26, 2019 5:11 pm

Re: Moving from Log4j 1 to Log4j 2 questions

Postby jcompagner » Tue Jan 18, 2022 6:32 pm

yes see what we have inside it it are all just loggers for example these are also packages:

<Logger name="org.sablo.websocket" level="WARN"/>
<Logger name="com.servoy.j2db.server.ngclient.property.types" level="WARN"/>
<Logger name="org.apache.wicket" level="WARN"/>

you can just put yours after all those other <Logger> Servoy now and then also adds one..
Its nothing special to servoy its just basic log4j configuration.
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8828
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 16 guests