Log4j logging from a Neo4j server extension - neo4j

My unmanaged server extension uses slf4j-log4j logging. When log4j.properties is bundled with the extension, logging works fine.
When it's not bundled but instead placed in Neo4j's conf directory, I assumed
wrapper.java.additional=-Dlog4j.configuration=file:conf/log4j.properties
from neo4j-wrapper.conf would ensure that it's picked up.
However, I don't see the log file being created or the specified log level being used.
The config file must be correct because it works as designed when bundled with the extension.
Adding -Dlog4j.debug as suggested in other posts adds no further information.
Is there something I've missed? I'm using Neo4j 2.1.3 on Mac OS X

Neo4j internally has the logback jars aboard, so every logging using slf4j will be handled by logback.
My approach to logging from an unmanaged extension is as follows:
Set up your logger in the unmanaged extension have a dependency on slf4j-api (do not add and other slf4j implementations to the classpath) and use the logger like this:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(com.mycompany.UnmanagedExtension.class);
Modify conf/custom-logback.xml and amend at the end:
<appender name="EXTENSIONLOG" class="ch.qos.logback.core.FileAppender">
<file>extensions.log</file>
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSSZ} %-5level [%logger{15}]: %message%n</pattern>
</encoder>
</appender>
<logger name="com.mycompany" level="debug">
<appender-ref ref="EXTENSIONLOG"/>
</logger>
conf/custom-logback.xml is included by Neo4j's internal logback configuration, see https://github.com/neo4j/neo4j/blob/master/community/kernel/src/main/resources/neo4j-logback.xml

Related

What should be the logger.rolling name in log4j2.properties when we have multiple packages?

I have created multiple packages in my maven project and I'm using Junit and Cucumber. I was using log4j before and now I want to migrate it to log4j2. I just searched for the log4j2 properties file format and found the below configurations in the file:
logger.rolling.name = com.example.my.app
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
What package should I give in the logger.rolling.name when I have multiple packages in my project?
You can use the Root logger as the catch all for packages you don't want to specify and then create loggers for any prefixes you do want. For example, if you have classes in the following packages - com.example.my.app, com.example.your.stuff, com.example.my.stuff - you can configure loggers for each of them or if you configure a logger for com.example.my then but the app and stuff packages will use that. If you configure a logger for com.example then all three packages would use that (unless you have a logger that is a better match).

Log4j2 logging level becomes different in Java 8 and Java 11

Log4j2 release 2.11.0 / 2.11.1 worked fine in Java 8 applications but not in Java 11
Here is an example of log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="log-path">C:\\MyTestXXXX\\EnterpriseLogs</Property>
</Properties>
<Appenders>
<RollingFile name="MyTestOnly" fileName="${log-path}\ServerLog.log" filePattern="${log-path}\ServerLog_%d{yyyy-MM-dd}_%i.log" append="true">
<PatternLayout>
<Pattern>[#%d{HH:mm:ss}|%p|%m#]%n</Pattern>
</PatternLayout>
<Policies>
<CronTriggeringPolicy schedule="0 0 0 * * ?" evaluateOnStartup="true"/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="OnlyLOG" additivity="true">
<AppenderRef ref="MyTestOnly" level="ALL" />
<Logger>
<Root level="ALL" />
</Loggers>
</Configuration>
In the Java source code, I did this:
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.core.Logger;
...
// Get org.apache.logging.log4j.core.Logger
Logger logger = (Logger) LogManager.getLogger("OnlyLOG");
logger.log(Level.forName("INFO", 600), "Log this text now!", new Exception("xxxxxx"));
Running in Java 8, I can see that serverLog.log has the content, which is good.
Running in Java 11, there is no log.
Through further study (debugging), I found this line interesting:
Logger logger = (Logger) LogManager.getLogger("OnlyLOG");
After execution, in Java 8, logger was something like "ALL in 2f333739", and log was fine.
But in Java 11, logger was something like "ERROR in 2f333739" and there was no log. Obviously the level became "ERROR" instead of "ALL".
So, I tries this in Java 11:
Level loggerLevel = logger.getLevel(); // Obviously it is ERROR level
logger.log(loggerLevel, "Log this text now!", new Exception("xxxxxx"));
The log happened in console (likely went through Root), not in the ServerLog.log that was configured in log4j2.xml for logger OnlyLOG.
So, what caused the level difference between Java 8 and Java 11? In other words, what should be modified so that in Java 11, log can still work the same way as in Java 8?
The issue is resolved. Here is the reason.
I used a new Eclipse IDE to run Java 11, when adding a project, I added lib folder to the class path, since log4j2.xml was in the lib folder. Now I inspected the IDE configuration a second time and found out that the lib folder was not in the class path. After a second attempt, the configuration is fine and logging becomes normal.
I made a second test by adding a brand new project into the Eclipse IDE. Interestingly enough, when adding lib folder to the class path, indeed I had to try twice, because the first time it was not added. By the way, the computer runs Windows 10. This did not happen when using a computer running in Windows 7.
Regardless, Log4j2 works fine in both Java 8 and Java 11.

Log file doesn't create in Log 4j with JSF 2

I have created Web Application by using JSF 2.0, Log 4j 1.2.14 and JBoss 7. When I run testcase, the log file is created. And the log file can't create when I run web application.
I there is anything I need to configur, please tell me.
Take a look at this maybe can help you.
The following filejboss-deployment-structure.xmlneeds to contain the following:
<jboss-deployment-structure>
<deployment>
<!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
<exclusions>
<module name="org.apache.log4j" />
</exclusions>
</deployment>
</jboss-deployment-structure>
Make sure the configuration file (log4j.xml or log4j.properties) is in the classpath of the web application (in this case, in the binaries).
WEB-INF/classes/log4j.properties
If you have both files (log4.properties, log4j.xml) only is considered log4j.xml. The first time you init or use some instance of org.apache.log4j.Logger, log4j search the configuration file in the classpath, then the configuration is loaded.
If you want to see this process of searching and loading more closely, add the following argument to the virtual machine:
-Dlog4j.debug

Struts2 with SLF4J and Logback produces double logs

I have a very weird problem with Struts 2.1.8 and SLF4J with Logback. I think I have setup everything as it should be (logback-core, logback-classic, slf4j-api and removed commons-logging from Struts JARs), but the logs look like this:
16:23:59,985 INFO [stdout] (ajp-localhost-127.0.0.1-8009-3) 2013-05-11 16:23:59,985 DEBUG [BasicTilesContainer.java:615] [ODk3Cc2-mn-X8eVfnemQn5WZ.undefined] - Render request recieved for definition 'DocumentList'
Clearly there are two timestamps, and one logging framework is logging through another somewhere, which causes this problem.
JBoss 7.1.1 is used.
Any ideas how to solve this problem?
EDITED:
This is how Logback configuration looks like:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p [%F:%L] [%X{sessionId}] - %m%n</pattern>
</encoder>
</appender>
I had a problem with modules in JBoss 7, but now although they are solved and Logback is really loaded, but the log files still are a mess. They look like if they are logged through JUL:
20:23:46,128 INFO [stdout] (connector_xxxx) 2013-05-11 20:23:46,127 DEBUG [ReConnector.java:83] [] - Next connection attempt in (ms) 20000
It looks like one logger is logging through the other.

how to config JDNI in tomcat7

I just have a test. Config jndi in $CATALINA_HOME/conf/context.xml like below:
<Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:#tnsname" username="test" password="test" maxActive="20" maxIdle="10" defaultAutoCommit="false" maxWait="5000" validationQuery="select 1 from dual" testWhileIdle="true" timeBetweenEvictionRunsMillis="5000" removeAbandoned="true" removeAbandonedTimeout="30" logAbandoned="true" />
and I didn't config it in any others places like $CATALINA_HOME/conf/server.xml or /WEB-INF/web.xml or WEB-INF/context.xml. But I still can use it normally in JAVA code.
But from http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html, it seems that at least two files needed to be configed for JNDI.
So could anyone tell me how to config JDNI in tomcat with standard methods. Thanks!
You configured JNDI data source for your application correctly, in the context.xml file for your application. The capability to configure JNDI data sources in the web.xml is preserved in Tomcat 7.0 for compatibility with older versions of Tomcat and, if I'm not mistaken, older versions of Java EE spec.

Resources