Initializing Log4J with Spring?

You could configure your Log4j listener in the web.xml instead of the spring-context.xml <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.web.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> So it is up before Spring starts.

How to give dynamic file name in the appender in log4j.xml

It’s much easier to do the following: In log4j.xml define variable as ${variable}: <appender name=”FILE” class=”org.apache.log4j.FileAppender”> <param name=”File” value=”${logfilename}.log” /> <layout class=”org.apache.log4j.PatternLayout”> <param name=”ConversionPattern” value=”%d::[%t]::%-5p::%c::%x – %m%n” /> </layout> </appender> Then make sure you set the system property when you start your JVM such as: java -Dlogfilename=my_fancy_filename example.Application That will create a dynamic log file … Read more

configure log4j to log to custom file at runtime

You can also do this from the log4j.properties file. Using the sample file below I have added the system property ${logfile.name}: # logfile is set to be a RollingFileAppender log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=${logfile.name} log4j.appender.logfile.MaxFileSize=10MB log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=[%-5p]%d{yyyyMMdd@HH\:mm\:ss,SSS}\:%c – %m%n The log file name can then be set two different ways: As a command line, system property passed to … Read more

How to configure log4j with a properties file

I believe that the configure method expects an absolute path. Anyhow, you may also try to load a Properties object first: Properties props = new Properties(); props.load(new FileInputStream(“log4j.properties”)); PropertyConfigurator.configure(props); If the properties file is in the jar, then you could do something like this: Properties props = new Properties(); props.load(getClass().getResourceAsStream(“/log4j.properties”)); PropertyConfigurator.configure(props); The above assumes that … Read more

Making a log4j console appender use different colors for different threads

You can use MulticolorLayout from jcabi-log. Add this dependency to the project: <dependency> <groupId>com.jcabi</groupId> <artifactId>jcabi-log</artifactId> <version>0.17.1</version> </dependency> And then configure it in log4j.properties: log4j.rootLogger=INFO, CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=com.jcabi.log.MulticolorLayout log4j.appender.CONSOLE.layout.ConversionPattern=[%color{%p}] %c: %m%n Same in log4j.xml: <appender name=”CONSOLE” class=”org.apache.log4j.ConsoleAppender”> <param name=”Target” value=”System.out” /> <layout class=”com.jcabi.log.MulticolorLayout”> <param name=”ConversionPattern” value=”[%color{%p}] %m%n” /> </layout> </appender> In this example, %p will be … Read more

Websphere all logs are going to SystemOut.log

The problem might be that WebSphere 6.1 uses Jakarta Commons Logging (JCL) internally, and if any of your code or 3rd-party libraries also use JCL, WebSphere’s configuration conflicts with your application trying to use log4j. If this is happening, you’ll see exactly what you’re seeing. There are multiple references and blog posts that describe ways … Read more