Migrating log4j 1.2.x to log4j 2.16.0 - java

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

Related

Cannot access org.springframework.ui.velocity.VelocityEngineFactory Method setVelocityEngine()

i have upgraded my java web project from spring boot 1.5.22 to 2.6.6. During this Upgrade the Velocity package is not even deprecated, it got removed. I know that it is recommended to switch to FreeMarker, but as a quick fix i was trying to fix my project.
First i included the following three dependencies to get the old velocity package and classes.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.25.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
After this most of my code got fixed after some changes.
The last remaining problem in my configuration bean is my VelocityConfigurer. I am trying to init a VelocityEngine with some properties and to create a VelocityConfigurer with the freshly created VelocityEngine afterwards. Like i did it before the spring boot update.
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
#Configuration
public class MailConfig {
#Bean
#Primary
public VelocityConfigurer velocityEngineBean() {
VelocityEngine engine = new VelocityEngine();
engine.setProperty(Velocity.RESOURCE_LOADER, "ds");
engine.setProperty("ds.resource.loader.class", "XXX.CustomDataResourceLoader");
engine.setProperty("spring.velocity.checkTemplateLocation=false", "false");
engine.setProperty("spring.velocity.velocimacro.library", "XXX.vm");
engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Slf4jLogChute.class.getName());
engine.init();
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
velocityConfigurer.setVelocityEngine(engine);
return velocityConfigurer;
}
}
But i get the following error. Error: Cannot access org.springframework.ui.velocity.VelocityEngineFactory
I can figure out why it can access this method.
The .jar with the Class is there.VelocityEngineFactory
This won’t work as Spring Boot 2.6 requires Spring Framework 5.3. Velocity support was deprecated in Spring Framework 4.3 and removed in 5.0. If you want to use an up-to-date and supported version of Spring Boot (2.5.x or 2.6.x at the time of writing), you should migrate to an alternative templating engine.

Unable to send the logs into Splunk using programatically configure log4j2 in Java SE

I'm using log4j2 and splunk within java to send logs into my Splunk Enterprise HEC (HTTP Event Collector) Splunk Enterprise is running in my local machine.
I'm doing all log4j2 configuration programmatically. (I know this is not the correct way to do this but I'm still doing this for learning purpose).
I tried to send the logs into Splunk Enterprise directly from postman with the same URL and token and it works fine, but when I tried to send the logs from java using log4j2 I don't get anything in splunk.
My code is =>
package com.mypack;
import java.util.*;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.apache.logging.log4j.core.layout.PatternLayout;
import com.splunk.logging.*;
import java.io.*;
public class Main {
private static final Logger log;
static {
configureLog4J();
log = LogManager.getLogger(Main.class);
}
public static void configureLog4J() {
ConfigurationBuilder<BuiltConfiguration> builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
// configure a splunk appender
builder.add(
builder.newAppender("splunk", "SplunkHttp")
.add(
builder.newLayout(PatternLayout.class.getSimpleName())
.addAttribute(
"pattern",
"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
)
)
.addAttribute("sourcetype", "log4j")
.addAttribute("index", "main")
.addAttribute("url",
"http://localhost:8088/services/collector")
.addAttribute("token", "Splunk 6b243d66-7e88-4a0d-9d1a-1ceba2c7ad4d")
.addAttribute("host", "java")
);
//configure console appender
builder.add(
builder.newAppender("console", "Console")
.add(
builder.newLayout(PatternLayout.class.getSimpleName())
.addAttribute(
"pattern",
"%logger{36}-%msg%n"
)
));
// configure the root logger
builder.add(
builder.newRootLogger(Level.INFO)
.add(builder.newAppenderRef(("splunk")))
.add(builder.newAppenderRef(("console")))
);
// apply the configuration
Configurator.initialize(builder.build());
}
public static void main(String ar[]) {
System.out.println("START");
log.info("ok");
log.log(Level.INFO, "BY from log4j2");
log.log(Level.ERROR, "BY Error from log4j2");
System.out.println("END");
}
}
my POM file
<dependencies>
<!-- https://mvnrepository.com/artifact/com.splunk/splunk-sdk-java -->
<dependency>
<groupId>com.splunk.logging</groupId>
<artifactId>splunk-library-javalogging</artifactId>
<version>1.11.4</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.splunk</groupId>
<artifactId>splunk</artifactId>
<version>1.6.5.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>splunk-artifactory</id>
<name>Splunk Releases</name>
<url>https://splunk.jfrog.io/splunk/ext-releases-local</url>
</repository>
</repositories>
I can see only logs of console appender in console. I cannot see any logs in splunk of Splunk Appender. Even I don't get any errors too. Did I miss something ?

Adding a Log4j logging to a Vaadin 8 application

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

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