log4j with spring create empty file - java

I want to use log4j 1.2.9 with spring 4.0.1 and JBOSS 7.1 but every time i get log information on console but the file is created and empty ...
log4j.properties
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log\\loggingFile.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=INFO, file, stdout
web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Simple test Controller
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
#ManagedBean(name="MyController")
public class MyController {
static final Logger logger = Logger.getLogger(MyController.class);
public String test() {
BasicConfigurator.configure();
logger.debug(LoginController.class);
logger.info("Info.. ");
logger.error("Error..");
logger.fatal("Fatal");
return null;
}}

the solution was to add jboss-deployment-structure.xml in /WEB-INF to exclude org.apache.log4j
<jboss-deployment-structure>
<deployment>
dependencies -->
<exclusions>
<module name="org.apache.log4j" />
</exclusions>
</deployment>
</jboss-deployment-structure>

As your configurations look fine to me, I believe you have an extra copy of the log4j.jar in your webapp.
Moreover, why don't you use the logging subsystem present in JBoss AS 7.x? By this subsystem, you can have centralized configuration for all you want to achieve. It will be easier to maintain as well.
For this, just remove log4j.properties and import log4j/slf4j via dependencies and make sure you don't pack your own log4j/slf4j jars in your webapp.
Check out Logging Configuration for JBoss.
Shishir

Related

How to configure log4j in Eclipse to view debug messages of Maven dependency library?

I have a Maven project in Eclipse that has the following dependencies, among others:
<dependency>
<groupId>org.hyperledger.fabric-sdk-java</groupId>
<artifactId>fabric-sdk-java</artifactId>
<version>1.4.13</version>
</dependency>
<dependency>
<groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway-java</artifactId>
<version>2.2.2</version>
</dependency>
As part of my troubleshooting efforts, I would like to use log4j (or a similar library) to view the debug output that originates from these libraries. In my src/main/resources folder I have the following log4j.properties file:
# Root logger option
log4j.rootLogger=DEBUG, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I have changed the level of rootLogger to DEBUG but I am still unable to see the debug messages.
I would like to look at the debug messages of one or more specific classes - org.hyperledger.fabric.sdk.NetworkConfig for instance. How should I configure my log4j.properties to achieve this?
Update: I have just been made aware of the log4j security vulnerability. Nevertheless, I would like to continue with seeking help for my question as I have confidence that the situation will be resolved soon.

Jooq not able to log SQL queries

I am using Jooq along with JAX-RS to create a REST API.
I have the following dependencies in my maven pom file log4j 1.2.7 and jooq 3.9.4
I have put the log4j.properties file in resources folder. Here are the contents of the file:
log4j.rootLogger=DEBUG, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/home/ps06756/log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Still, Jooq is not logging the SQL statemennts being executed.
How should I correct this problem.
Please check if you have logback or slf4j on your classpath as that is the first logger the JooqLogger tries to find on the classpath.
If it fails it tries log4j and after that it falls to java.util.logger.
When it does find logback or slf4j but you don't have any configuration you will not see anything being logged.

Maven project log4j not working

I have a simple maven project and trying to get log4j implemented.
deploying to local tomcat. Nothing printing in console of eclipse where I have logger.debug().
Am I missing something?
This is my structure:
pom.xml:
<!-- http://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Class:
final static Logger logger = Logger.getLogger(myclass.class);
.properties file
# Root logger option
log4j.rootLogger=INFO, stdout
log4j.rootLogger=DEBUG, stdout
log4j.rootLogger=ERROR, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Simply remove the superfluous rootLogger declarations:
# Root logger option
log4j.rootLogger=DEBUG, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Logging levels are inclusive of those levels "above" them. For example, setting the logging level to DEBUG will include DEBUG, INFO, WARN, ERROR, and FATAL messages automatically. There is no need to declare logging levels for each one.

how to access log4j property in all class?

I am using log4j in my application. Now I have created property file.How to access this property file in all Java classes. Can anyone please explain me about it?
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${rootPath}WEB-INF/logs/MyLog.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class log4jExample{
/* Get actual class name to be printed on */
static Logger log = Logger.getLogger(
log4jExample.class.getName());
public static void main(String[] args)
throws IOException,SQLException{
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
}
}
Try adding this to your web.xml if you havent added that already
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>./conf/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<!-- Refresh log4j configuration every 5 minutes. -->
<param-value>300000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Check your catalina.log for any errors. Make sure that log4j is initialized correctly. If you have multilpe log4j jar files (one in your context and another in tomcat/lib) it may not have initialized properly. Try deleting duplicate jars

Enabling complete Path Logging in Spring MVC

I wrote the following in my log4j.properties file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.logger.org.springframework.web = TRACE, stdout
But I'm just getting the init logging. What should I do so as to get detailed logging so as to get the trace whenever i enter a url in my browser.
Its very simple to log the execution of program :
1) Add log4j dependency in pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.4.2</version>
</dependency>
2) Add the one Listner class in web.xml
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
3) Then add log4jConfigLocation in web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
and then add your log4j properties as follows :
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=debug, file, stdout
4) Go to the particular java file in your application import log4j package
and then implement your logging mechanism
5) Now you should be able to trace the log

Categories

Resources