Adding a Log4j logging to a Vaadin 8 application - java

I've tried to follow a documentation here Piping to Log4j using SLF4J, but still can't get a correct adapter used. So, how could one use aLog4j in Vaadin 8 application? What are the dependencies to be added to pom.xml?

There are multiple additions/changes required to get logging working.
Along the dependencies mentioned in the documentation to be added:
SLF4J
jul-to-slf4j
slf4j-log4j12-x.x.x.j
A log4j-core should be added as well. The relevant snippet of pom.xml looks like this:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.29</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.29</version>
</dependency>
The next step is to add a log4j.properties file (in my case under src/main/resources/log4j.properties). Here you can configure your logging properties
As mentioned in the documentation, a SLF4JBridgeHandler should be added to a Servlet definition (in case, there is only one servlet)
#WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
#VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
static {
SLF4JBridgeHandler.install();
}
}
Imports used for Logger and LoggerFactory are:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
To verify a correct implementation is picked for a logger its simple name is printed System.out.println("Adapter name:" + logger.getClass().getSimpleName());. Output should be : Log4jLoggerAdapter
Note: I've used a log4-core less than 2.9 since I am using Java 8 and jetty Error scanning entry .... You should upgrade to a newer version if your jre is > 8

Related

Migrating log4j 1.2.x to log4j 2.16.0

Migrating log4j 1.2.x to log4j 2.16.0
Using PropertyConfigurator.configure method to load all log4j file are located at specific location on the system.
static {
PropertyConfigurator.configure("C:/users/log4j.properties").
}
What is alternative way for configure log4j properties from external location?
What is alternative way of using PropertyConfigurator.configure in log4j2?
You have to do some code changes after adding new dependencies.
Add the following dependencies to the pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.16.0</version>
</dependency>
replace your java class file import import org.apache.log4j.Logger; with import org.apache.logging.log4j.LogManager; and import org.apache.logging.log4j.Logger;
then replace private static final Logger logger = Logger.getLogger(Test.class); with private static final Logger logger = LogManager.getLogger(Test.class);
For further reference: click here

Migrating JUL to log4j2, performance is not as expected. What went wrong?

I am a new user of log4j2. Based on Log4j2 performance tests introduction, and it may need to generate a large number of csv files for data processing. I plan to migrate a project from JUL to log4j2. This is a multi-threaded project that uses a lot of loggers. But the test results are not satisfactory . Where did I get it wrong?
the compare results
The dependency configuration is as follows:
<!-- Log4j2 api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<!-- Log4j2 impl.-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<!-- for Log4j2 asynchronous logging-->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
<!-- for Log4j2 csvlayout-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.5</version>
</dependency>
File "log4j2.xml" has multiple similar appenders and loggers, for example:
partial configuration for log4j2.xml
Java related code is as follows:
System.setProperty("log4j2.isThreadContextMapInheritable", "true");
String renameCsvFile = new String( inputDir.getName() + StaticUtils.SEPARATOR +"log"+ StaticUtils.SEPARATOR + "rename");
ThreadContext.put("module3", renameCsvFile);
rename_logger = LogManager.getLogger("myrenamelog");
rename_logger.info("rename logging", "Timestamp","Level", "OrignalPath","RenamedPath","Status", "Message", "Layer#", "Thread#");
rename_logger.info("rename logging...", Instant.now(),"info",file.getAbsolutePath(),newName.getAbsolutePath(),"success"," ","Layer#",Thread.currentThread().getName());
I would suggest you upgrade to the latest release. Specifically, LOG4J2-2644, which was addressed in Log4j 2.12.1, should impact some of your performance times. However, as in LOG4J2-2792, some applications that rely heavily on filters might need to disable eagerly obtaining the location.
If after testing with a newer version you still have problems please create a Jira issue with Log4j. Ideally you should either provide a sample test suite to demonstrate the problem and/or a snapshot created with a profiling tool such as YourKit that can help identify the problem areas.

How can we disable the hibernate logs?

I am working on Hibernate project, But I have to write the logs into a file then both will combined(Hibernate log added in file) How can we disable the hibernate logs.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
Here are two approaches:
Setting the logging level to NONE should turn off all logging.
Since Hibernate uses the slf4j facade, you should be able to use the NOPLogger; see Get a dummy slf4j logger?.
Replace the log4j-core dependency with this:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.30</version>
</dependency>
I've used this but its not working for me:
java.util.logging.Logger.getLogger("org.hibernate")
.setLevel(java.util.logging.Level.OFF);
Yea, that won't work. According the the dependency in your question, you have selected the log4j as the backend. The code snippet above is configuring the java.util.logging logger ...
Look at the javadocs for the log4j classes.
This worked for me..
static {
// Initialized log4j2 Property and disable hibernate log
Configurator.initialize("LOGS", "Projects/log4j.properties");
Configurator.setLevel("org.hibernate", org.apache.logging.log4j.Level.OFF);
}

SpringMVC send email

I'm trying to send email using SpringMVC. I've made a bean JavaMailSender and get an error.
#Bean
public JavaMailSender javaMailSender(){
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setUsername("test");
javaMailSender.setPassword("test");
javaMailSender.setPort(56);
javaMailSender.setHost("smtp.test.ru");
return javaMailSender;
}
Error:
04-Dec-2016 20:05:50.699 SEVERE [RMI TCP Connection(31)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed
java.lang.NoClassDefFoundError: org/springframework/mail/javamail/JavaMailSender
My context with this bean:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
#Configuration
#PropertySource("classpath:util.properties")
#PropertySource(value = {"classpath:auth.properties"})
public class MailContext {
#Bean
public JavaMailSender javaMailSender(){
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setUsername("test");
javaMailSender.setPassword("test");
javaMailSender.setPort(556);
javaMailSender.setHost("test.ru");
javaMailSender.setProtocol("smtp");
Properties properties = new Properties();
properties.setProperty("mail.debug", "true");
javaMailSender.setJavaMailProperties(properties);
return javaMailSender;
}
}
My pom :
...
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
...
What did I do wrong?
It appears that you need, but do not have spring-context-support-4.3.4.RELEASE.jar on your classpath.
To add this to your application's classpath in IntelliJ, edit your project's Project Structure, and add a Library under Libraries referencing the Spring jar file. Add or edit your Module and add the library to the Module. Add or edit Artifacts and make sure the library or module is one of the Available Elements for the Artifact.
Under the Run menu, click Edit Configurations for your Tomcat configuration, on the Deployment tab, add the artifact (or library) to the Deploy at server startup list.
This information at JetBrains provides more information on configuring your Intellij setup to get desired jars on your classpath.
It is also possible that you need to make a simple configuration change to your dependency in your pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.4.RELEASE</version>
<scope>runtime</scope>
</dependency>
(NOTE the addition of the <scope> element. More information on Maven scopes available here.)
(These instructions may vary depending on the version of Intellij that you are using.)
In summary, you need to get the Spring jar on your application's classpath at runtime.

How to use and configure logback in Adobe CQ5 projects?

I newbie in CQ5. I started my first project in CQ and I want to write any exceptions and debug info of project's components, services and servlets (in bundles) to log files in crx-repo (...\crx-quickstart\logs).
I want to use slf4j with logback implementation in my project.
I tried below steps:
Add dependencies in project's pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.3</version>
</dependency>
Add logback.xml with configuration (loggers and appenders) to
project's bundle resource package.
Try to instantiate new logger in service (for example):
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
.....
#Component(metatype = true, label = "Some", description = "Sone service" ,
immediate = true)
#Service(SomeService.class)
public class SomeService {
private final Logger logger = LoggerFactory.getLogger(MyDao.class);
.....
logger.debug("debug");
logger.info("info");
logger.error("error");
......
}
These steps (It isn't work, of course) I gave an example because i don't know how really work with logback in Adobe CQ5. Any suggestions? I would be grateful for any help!
CQ does log to crx-quickstart/logs/error.log with its default configuration, and the underlying Apache Sling framework provides the necessary bridge so you just need to acquire an slf4j Logger and write to that.
The Java code of your SomeService example looks correct to me but in the pom you only need the slf4j-api dependency, with scope provided, as API and implementation packages are provided by the CQ runtime.
You can also have a look at a Sling sample like Slingbucks which will log to that error.log if installed on a default CQ instance.

Categories

Resources