log4j with spring doesn't work - java

In my pom.xml, I've added a dependency for using Log4j
pom.xml
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
Then, I executed the following command
mvn clean install
After this command, I notice that the log4j-1.2.9.jar is well added in my lib directory itself in the target directory
Then, I started the google app engine server with the following command in my project :
appengine:devserver
And my log4j.properties in src/main/resources
# 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
After, by opening the jar file in winzip, I noticed that the Logger.class was in org/apache/log4j directory, so, when I tried to import the Logger class in a java file, it didn't detect the package 'log4j'.
Do you have any solutions ?
Thank you

Related

getting "Handler java.util.logging.ConsoleHandler is not defined" error on jenkins console when jboss is trying to restart

I have generated a war file using maven for one of my java projects, and tried to deploy it to jboss application server using jenkins CI/CD.
Getting below error on the jenkins console and it runs forever and doesn't come out of the build when jboss is restarted.
"Handler java.util.logging.ConsoleHandler is not defined"
Below is the command used to start jboss:
cd /apps/jboss-eap-7.2/bin
nohup ./standalone.sh -c standalone-full.xml -b 0.0.0.0 -Djboss.as.management.blocking.timeout=5000 > nohup.out &
I have tried below options:
created a logging.properties file in src/main/resources folder and added below line:
handlers = java.util.logging.ConsoleHandler.level= INFO
tried adding below dependancies in my pom.xml file:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
None of the above options worked.
I have uploaded the jenkins console log image of jboss-restart error part.
jboss-restart-log
Can anyone help me with this. Thanks in advance.

log4j2.properties file is not taken into account while log4j.properties file is

I migrated our spring boot application from the web.xml configuration file to java based configuration. In this application we use log4j2 for logging however we also have log4j because of some surrounding system which uses it, so we can not remove it.
After finishing with all the migration the logs were not working. I figured out that I have to add the following code into my application.properties file
logging.level.project.base.package=TRACE
logging.level.org.springframework=INFO
logging.level.org.hibernate=INFO
Adding all these made logs to appear, however only the property file log4j.properties is recognized and the logs which are using log4j2 have the default logging pattern.
If I remove the log4j.properties file the log4j2 still doesn't work, moreover I get the following warning message
log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
I also tried to specify the config file path in application.properties in bought of the following ways
logging.config=application.properties
logging.config=classpath:application.properties
Our pom.xml dependencies are the following
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
We are not running using an embedded tomcat, so we have a config class that extends SpringBootServletInitializer. We use the 2.0.1.RELEASE Spring Boot Version.
Bellow you can find my 2 log4j config files
log4j.properties
conversionPatternShort=OLDLOGGGGER %5p %d %-30C{1}:%4L [%X{username}] - %m%n
### APPENDER
log4j.appender.crt=org.apache.log4j.ConsoleAppender
log4j.appender.crt.layout=org.apache.log4j.PatternLayout
log4j.appender.crt.layout.ConversionPattern=${conversionPatternShort}
### Define Levels per package
log4j.rootLogger=ERROR,crt
log4j.logger.some.package1=DEBUG,crt
log4j.logger.some.package2=DEBUG,crt
log4j2.properties
status=ERROR
dest=err
name=LocalConfig
### PATTERN
property.conversionPatternShort=%5p %d %-30C{1}:%4L [%X{username}] - %m%n
### APPENDER
appenders=crt
appender.crt.type=Console
appender.crt.name=crt
appender.crt.layout.type=PatternLayout
appender.crt.layout.pattern=${conversionPatternShort1}
## LOGGER
## defining some loggers, foreach logger I have defined
## (name, level, additivity, appenderRef.crt.ref)
logger.xxxxx.name=namex
logger.xxxxx.level=DEBUG
logger.xxxxx.additivity=false
logger.xxxxx.appenderRef.crt.ref=crt
logger.yyyyy.name=namey
logger.yyyyy.level=DEBUG
logger.yyyyy.additivity=false
logger.yyyyy.appenderRef.crt.ref=crt
logger.hibernateUtil.name=org.hibernate.engine.jdbc.spi.SqlExceptionHelper
logger.hibernateUtil.level=ERROR
logger.hibernateUtil.additivity=false
logger.hibernateUtil.appenderRef.crt.ref=crt
rootLogger.level=WARN
rootLogger.appenderRef.crt.ref=crt
I really can not find what change made the log4j2 not work anymore. Could you please help?

log4j not writing logs

Below is my log4j configuration
#log4j.additivity.org.apache.qpid=false
log4j.rootLogger=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.threshold=DEBUG
log4j.appender.console.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.logger.javax.jms=DEBUG
log4j.logger.org.apache.qpid=DEBUG
log4j.logger.org.apache.qpid.amqp_1_0=DEBUG
log4j.logger.org.apache.qpid.amqp_1_0.jms=DEBUG
and then in code
String log4jConfigFile = System.getProperty("user.dir") + File.separator + "log4j.properties";
PropertyConfigurator.configure(log4jConfigFile);
logger.debug("this is a debug log message");
my debug message this is a debug log message do get printed but the log messages from org.apache.qpid are not getting printed on console
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-amqp-1-0-client-jms</artifactId>
<version>0.22</version>
</dependency>
EDIT
I am a newbie in java... The logging dependencies I have added. Do I need add some setting somewhere to redirect sl4j logs to log4j??
<slf4j-version>1.6.6</slf4j-version>
<log4j-version>1.2.17</log4j-version>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
</dependency>
The (deprecated) qpid-amqp-1-0-client-jms client used java.util.logging, and not log4j. To quote from a mail I sent back in 2014 to the users#qpid.apache.org mailing list:
you can turn it on by setting the Java system property
java.util.logging.config.file to point to a file that looks something
like this:
handlers=java.util.logging.FileHandler FRM.level=ALL
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tc] %4$s: %5$s%n
java.util.logging.FileHandler.level=ALL`
# (The output file is placed in the directory
# defined by the "user.home" System property.)
java.util.logging.FileHandler.pattern=%h/qpid-jms-%u.log`
When you run the client it should then generate a file called
qpid-jms-0.log in your home directory, with output that looks
something like:
[Mon Feb 24 18:45:58 CET 2014] FINE: RECV[/127.0.0.1:5672|0] :
SaslMechanisms{saslServerMechanisms=[ANONYMOUS]}
Note that the logging in this old client is really very minimal and ideally you should instead migrate your code to the supported Qpid JMS client for AMQP 1.0 https://qpid.apache.org/components/jms/index.html which does use slf4j, but uses different configuration syntax for connections and queues.

How to use log4j in RCP application with Maven?

I am working on a small RCP project which has Maven nature and now I wish to add log4j dependencies with that.
For that purpose what I did was :
Added log4j.properties under bin folder inside the project. bin is the folder where all the class files are getting generated. The file looks like this:
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
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
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\Srijani\\Personal Workspace\\RCP\\EditorApp\\log\\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
Added this dependency in pom.xml
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
In java code I wrote like this:
import org.apache.log4j.Logger;
import com.app.editor.constants.Constants;
public class DatabaseConnection {
final static Logger logger = Logger.getLogger(DatabaseConnection.class);
}
This code does not creata any problem while compiling. But, while running the code, I got this error:
!ENTRY org.eclipse.e4.ui.workbench 4 0 2016-01-13 13:50:35.397
!MESSAGE Unable to create class 'org.eclipse.ui.internal.e4.compatibility.CompatibilityView' from bundle '55'
!STACK 0
org.eclipse.e4.core.di.InjectionException: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:62)
Note: I did not add anything to MANIFEST.MF and build.properties.
Please help!
Thanks
Update: Got the issue but not sure how to solve it. When I am downloading the jar manually and set it in the class path, I am able to use log4j in Eclipse RCP. But, when I am trying download it via Maven, it is not working. Any idea why this is happening?
If you use Maven in combination with Eclipse RCP you should consider using Tycho[1]. Tycho uses the MANIFEST-first approach, so that you don't need to edit the pom-files.
In addition you should put your log4j.properties into a fragment with the log4j-bundle as host-plugin.
[1] https://eclipse.org/tycho/
I hope in this website explained clearly. It will helps to you.
http://www.mkyong.com/logging/log4j-hello-world-example/
My sincere suggestion is, before implement in project try to create one sample example. If it success, you can apply same into project.

How to create log file using log4j in struts2+hibernate application?

I am using Struts2,hibernate web application. On that file i used following dependency for log4j in pom.xml
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
In my Actionclass i used the following code to write log file:
public class loginAction extends action{
static Logger log = Logger.getLogger(com.action.LoginAction.class);
public String checklogin(){
log.debug("Debug Message(LOGIN)!");
log.info("Info Message(LOGIN)!");
log.warn("Warn Message(LOGIN)!");
log.error("Error Message(LOGIN)!");
log.fatal("Fatal Message(LOGIN)!");
//my coding for checking logged status.
}
}
I have placed log4j.properties file under WEB-INF/classes folder with the following code,
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C\:\\logfile.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=debug, file
When i run and deploy this application on server the property file is placed in correct path. but after running statement in action class nothing get affect from log4j.properties file. I dont know where i did wrong.
So anyone please help me to find this issue. Thanks in Advance.

Categories

Resources