How to add log4j to a web application in java - java

I am trying to add log4j to my web application to record the exchange between the server and the client so I added the dependency in my pom.xml:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
and I created the log4j.proporties:
# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log
log4j.appender.file.File=C:\\test.log
log4j.appender.file.MaxFileSize=10MB
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%
and the following is my code :
static final Logger logger = Logger.getLogger(hellologger.class);
public static void main(String[] args) {
String log4JPropertyFile = "C:\\Users\\xxxx\\Desktop\\log4j.properties";
Properties p = new Properties();
try {
p.load(new FileInputStream(log4JPropertyFile));
PropertyConfigurator.configure(p);
logger.info("Wow! I'm configured!");
} catch (IOException e) {
}
}
But finally I got this error :
Infos: org.osgi.framework.BundleException: Unresolved constraint in bundle com.mycompany.webclient [329]: Unable to resolve 329.7: missing requirement [329.7] osgi.wiring.package; (&(osgi.wiring.package=org.apache.log4j)(version>=1.2.0)(!(version>=2.0.0)))
at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2037)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:1175)
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:1153)
at org.apache.felix.fileinstall.internal.DirectoryWatcher.processAllBundles(DirectoryWatcher.java:1146)
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:456)
at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:263)
any idea how to fix this problem ?!!!!

You can go through this tutorial on log4j .
The properties file and pom.xml files are explained properly .
logs generated are like this :
2014-07-02 20:52:39 DEBUG HelloExample:19 - This is debug : mkyong
2014-07-02 20:52:39 INFO HelloExample:23 - This is info : mkyong
2014-07-02 20:52:39 WARN HelloExample:26 - This is warn : mkyong
2014-07-02 20:52:39 ERROR HelloExample:27 - This is error : mkyong
2014-07-02 20:52:39 FATAL HelloExample:28 - This is fatal : mkyong

Related

Java - log4 prints to neither console nor file

I'm trying to use log4j to print messages from a standalone Java app to the console in Eclipse, but it's not printing anything. Here's the code:
Declaration:
static Logger logger = LoggerFactory.getLogger(AnalyticsBackupUtil.class);
Print statements:
public static void main(String args[]) {
String log4jConfigFile = System.getProperty("user.dir") + File.separator + "log4j.properties";
PropertyConfigurator.configure(log4jConfigFile);
logger.debug("this is a debug log message");
logger.info("this is a information log message");
logger.warn("this is a warning log message");
logger.error("this is an error log message");
...
properties file:
log4j.rootLogger=DEBUG, Appender1,Appender2
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=applog.txt
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
Maven:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Any idea what the problem might be?
Log4j 1.x does not have a method LoggerFactory.getLogger. You are probably using org.slf4j.Logger and org.slf4j.LoggerFactory.
If this is intentional (using a logging API like SLF4j allows to easily change the backend), you need to add the slf4j-log4j12 binding to the classpath.

PropertyConfigurator for Log4j does not exists anymore?

I only get this on the console:
13:08:18.379 [main] ERROR log4.prueba - This is Error
13:08:18.381 [main] FATAL log4.prueba - This is Fatal
I dont get a file or all the other levels.
What I am doing wrong?
My directory looks like this:
projectFolder/src/log4/prueba.java
projectFolder/src/log4j.properties
The code:
package log4;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class prueba {
private static Logger logger = LogManager.getLogger(prueba.class);
public static void main( String[] args )
{
logger.debug("This is Debug");
logger.info("This is Info");
logger.warn("This is Warn");
logger.error("This is Error");
logger.fatal("This is Fatal");
}
}
And the properties file look like this:
# Root logger option
log4j.rootLogger=DEBUG, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
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
# 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
Log4j 2 also supports configuration in properties files, but be aware:
the file needs to be called log4j2.properties
the configuration syntax is different from the Log4j 1.2 syntax (the configuration in the question uses the old Log4j 1.2 syntax, Log4j 2 will not understand this)
The Log4j 2 user manual has mostly XML examples, so many people find it easier to use the XML configuration syntax. (Again, the config file should be named log4j2.xml, Log4j 2 will ignore log4j.xml.)
define log4j property file
# prueba.java
import org.apache.log4j.Logger;
public class prueba {
private static Logger LOGGER = Logger.getLogger(prueba.class);
public static void main(String[] args) {
LOGGER.debug("This is Debug");
...
}
}
# pom.xml
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
at src/resources/log4j.properties
# Root logger option
log4j.rootLogger=DEBUG, file, 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
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log/log4j-application.log
log4j.appender.file.MaxFileSize=10MB
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
output to stdout and log/log4j-application.log file
2018-05-11 14:29:43 DEBUG prueba:8 - This is Debug
2018-05-11 14:29:43 INFO prueba:9 - This is Info
2018-05-11 14:29:43 WARN prueba:10 - This is Warn
2018-05-11 14:29:43 ERROR prueba:11 - This is Error
2018-05-11 14:29:43 FATAL prueba:12 - This is Fatal

log4j:WARN No appenders could be found for logger (org.springframework.web.filter.CharacterEncodingFilter). with log4j2

I am in the process of migrating from log4j 1.2 to log4j 2.
I have my whole configuration into a log4j2.properties file.
I noticed that I got a new error message in my logs while starting my tomcat :
log4j:WARN No appenders could be found for logger (org.springframework.web.filter.CharacterEncodingFilter).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I have verified with my previous logs, and I did not have this warning previously.
Here is my previous configuration (in the log4j.properties file):
log4j.rootLogger=INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{dd-MM HH:mm:ss} %p %t %c - %m%n
log4j.logger.com.example=DEBUG
log4j.logger.org.springframework=INFO
Here is the new one (in log4j2.properties) :
appender.console.type = Console
appender.console.name = ConsoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d %p %t %c - %m%n
loggers = log1, log2
logger.log1.name = org.springframework
logger.log1.level = INFO
logger.log1.appenderRef = ConsoleAppender
logger.log2.name = com.example
logger.log2.level = DEBUG
logger.log2.appenderRef = ConsoleAppender
rootLogger.level = ERROR
rootLogger.appenderRef.stdout.ref = ConsoleAppender
So, what I've understand so far is :
I create one appender, named ConsoleAppender, of type Console
I create two loggers that goes each onto ConsoleAppender, one on org.springframework (level INFO) and the other on com.example (level INFO)
I use this page to try to understand how to do the migration.
Do you see any explanation about why I should have this particular message ?
As seen in this answer, the issue may be due to the fact that I was also using slf4j-log4j12 (but in the latest version already) and it was in conflict with other log4j apis :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
So I did what's told and I removed the org.slf4j:slf4j-log4j12 dependancy and added log4j-api & log4j-slf4j-impl dependancies :
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.2</version>
</dependency>
... and for know it seems to solve the error (it doesn't appear anymore into my starting logs
To be more clear, I first only added the three above dependancies without removing the slf4j-log4j12 one, and I got this as well into the logs :
SLF4J: Found binding in
[jar:file:/myuser/apache-tomcat-7.0.75/webapps/mywebapp/WEB-INF/lib/log4j-slf4j-impl-2.8.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/myuser/apache-tomcat-7.0.75/webapps/mywebapp/WEB-INF/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
After having removed the slf4j-log4j12 dependancy, the whole error logs disappeared.

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