Log4j Warning while initializing? [duplicate]

You’re missing the log4j.properties or log4j.xml in your classpath. You can bypass this by using BasicConfigurator.configure(); But beware this will ONLY log to System.out and is not recommended. You should really use one of the files above and write to a log file. A very simple example of log4j.properties would be #Log to Console as … Read more

Log4J: Strategies for creating Logger instances

Typically, you’d have loggers setup per class because that’s a nice logical component. Threads are already part of the log messages (if your filter displays them) so slicing loggers that way is probably redundant. Regarding application or layer based loggers, the problem is that you have to find a place to stick that Logger object. … Read more

Where to place log4j.xml

It finds the log4j.xml using the CLASSPATH. If log4j doesn’t find any config file, it will send an error to the console. If you don’t see any such error then it is likely that it is finding a config file which may not be the one you are editing. There is a command-line option to … Read more

log4j migration to log4j2

Not Exactly related to the Question, But the below are my learning in log4j2 migration. hope it helps someone. Since the log4j vulnerability which was discovered in late 2021, many organizations have upgraded to log4j2 to overcome it. and many developers needed to understand what exactly is log4j and what is the vulnerability. I recommend … Read more

logging with AOP in spring?

Spring makes it really easy for us to make use of AOP. Here’s a simple logging example: @Aspect public class MyLogger { private Logger log = Logger.getLogger(getClass()); @After(“execution(* com.example.web.HomeController.*(..))”) public void log(JoinPoint point) { log.info(point.getSignature().getName() + ” called…”); } } Then simply configure your applicationContext.xml (or equivalent): <aop:aspectj-autoproxy> <aop:include name=”myLogger”/> </aop:aspectj-autoproxy> <bean id=”myLogger” class=”com.example.aspect.MyLogger”/> You’ll … Read more

How to configure maven to use different log4j.properties files in different environments

You can use profiles to achieve the desired behavior: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>log4j</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>output_directory</outputDirectory> <resources> <resource>${log4j.file}</resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <log4j.file>path_to_file_A</log4j.file> </properties> </profile> <profile> <id>prod</id> <properties> <log4j.file>path_to_file_B</log4j.file> </properties> </profile> </profiles>