i have issue with struts2 and log4j2.0 My program working properly but log4j2.0 is not respond pls help me to solve dis..
im using following jar:
1.log4j-core-2.0-beta8
2.log4j-api-2.0-beta8
3.log4j-web-2.0-beta2
my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration strict="true" schema="Log4J-V2.0.xsd" status="OFF" monitorInterval="300">
<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</Console>
<File name="MyFile" fileName="Logger/knight.log">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</File>
</appenders>
<loggers>
<root>
<appender-ref ref="CONSOLE" />
<appender-ref ref="MyFile" />
</root>
</loggers>
</configuration>
my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<display-name>Struts2_Verified</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>log4j2.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.logging.log4j.core.web.Log4jContextListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
I forgot what the default level is, but try setting it as verbose as possible at first.
<root level="TRACE">
<appender-ref ref="CONSOLE" level="DEBUG" /> <!-- can have their own level -->
<appender-ref ref="MyFile" />
</root>
Also, to see if there is a problem with the log4j2 configuration, use this setting:
<configuration status="TRACE" monitorInterval="300">
If the above does not help, can you raise a Jira ticket for this on the log4j-2 issue tracker? (https://issues.apache.org/jira/browse/LOG4J2) The log4j team should be able to help out.
Related
I'm developing a Web app and I'm using log4j2. In developing mode, I'm logging using RollingFile and Console appenders.
Everything is working properly, but I'd want to disable the Console appender when my project will be released and it will be in production mode.
Here's a slice of my log4j2.xml code:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="PropertiesConfig" packages="com.project.application">
<!-- PROPERTIES -->
<Properties>
<Property name="webName">Project</Property>
<Property name="logBaseDir">${sys:catalina.base}/logs/</Property>
<Property name="consolePattern">%highlight{[%-5level] [%d{yyyy-MM-dd HH:mm:ss,SSS}] [%c{1}] - %msg%n}</Property>
<Property name="rollingFilePattern">[%-5level] [%d{yyyy-MM-dd HH:mm:ss,SSS}] [%c{1}] - %msg%n</Property>
</Properties>
<!-- APPENDERS -->
<Appenders>
<!-- Console -->
<Console name="Console"
target="SYSTEM_OUT"
immediateFlush="true">
<PatternLayout>
<pattern>${consolePattern}</pattern>
</PatternLayout>
</Console>
<!-- RollingFile -->
<RollingFile name="RollingFile"
fileName="${sys:logBaseDir}${webName}/${webName}.log"
filePattern="${sys:logBaseDir}${webName}.%d{yyyy-MM-dd}.log"
immediateFlush="true">
<PatternLayout>
<pattern>${rollingFilePattern}</pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<!-- LOGGERS -->
<Loggers>
<Logger name="com.project.application" additivity="true" level="warn">
<AppenderRef ref="RollingFile" />
</Logger>
<Root level="info"> <!-- #TODO disable in production -->
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Thank you!
Use a filter, e.g. ThreadContextMapFilter:
<Console name="Console" target="SYSTEM_OUT" immediateFlush="true">
<ThreadContextMapFilter onMatch="DENY" onMismatch="NEUTRAL">
<KeyValuePair key="is-production" value="1"/><!-- skip on production -->
</ThreadContextMapFilter>
<PatternLayout>
<pattern>${consolePattern}</pattern>
</PatternLayout>
</Console>
The initialization of the ThreadContext entry can be perfomed in a ServletContextListener, e.g.:
#WebListener
public class Log4jThreadContextInitializer implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent sce) {
String isProduction = isProduction() ? "1" : "0";
sce.getServletContext().log("Setting 'is-production' flag for Log4j to " + isProduction);
org.apache.logging.log4j.ThreadContext.put("is-production", isProduction);
}
private boolean isProduction() {
// TODO: production detection
}
#Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
Spring-lookup made my life easier. My appender is like this:
<Appenders>
<Console name="console-local" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss:SSS} [%thread] %-5level %logger{2}:%L - %msg%n" />
</Console>
</Appenders>
I have added a property like this:
<Properties>
<Property name="console-appender">console-${spring:profiles.active}</Property>
</Properties>
And the logger is like this:
<Loggers>
<root level="info">
<appender-ref ref="${console-appender}"/>
</root>
</Loggers>
If my active profile is local, thus console-appender will be set to console-local, and the log will be shown in the console, as ref will find console-local.
Again, suppose, my active profile is prod, then console-appender will be set to console-prod, and the log will not be shown in the console, as ref will not find console-prod. Because Console's appender name is still console-local.
My log4j version is 2.14.1
I am specifying the filename to log4j2 logger in the XML as below. All works fine but i get the error
2017-09-06 16:54:33,496 main ERROR Unknown object "RandomAccessFile" of type org.apache.logging.log4j.core.appender.RandomAccessFileAppender is ignored: try nesting it inside one of: ["Appenders", "Loggers", "Properties", "Scripts", "CustomLevels"].
<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j.test"
status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd/MM/yyyy HH:mm:ss} %c %m%n"/>
</Console>
</Appenders>
<RandomAccessFile name="FILE" fileName="${sys:log4j.saveDirectory}/CMSAutomation.${date:yyyy-MM-dd_hh-mm-ss}.log" append="true" immediateFlush="false">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-6p %C{1}.%t:%L >>> %m%n"/>
</RandomAccessFile >
<Loggers>
<Logger name="log4j.test.Log4jTest" level="debug">
<AppenderRef ref="Console"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
This error is coming because RandomAccessFile appender is not defined within <Appenders> XML element.
Below is the correct way of defining any appender including RandomAccessFile appender -
<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j.test"
status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd/MM/yyyy HH:mm:ss} %c %m%n"/>
</Console>
<RandomAccessFile name="FILE" fileName="${sys:log4j.saveDirectory}/CMSAutomation.${date:yyyy-MM-dd_hh-mm-ss}.log" append="true" immediateFlush="false">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-6p %C{1}.%t:%L >>> %m%n"/>
</RandomAccessFile>
</Appenders>
<Loggers>
<Logger name="log4j.test.Log4jTest" level="debug">
<AppenderRef ref="Console"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
You can check the same in log4j2 documentation here.
Additional one point, you are not using RandomAccessFile appender in any logger. If you will not define it in any logger, it wouldn't work.
I am using log4j2
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
and a very basic configuration xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</layout>
</appender>
<category name="org.apache.log4j.xml">
<priority value="info" />
</category>
<Root>
<priority value ="debug" />
<appender-ref ref="STDOUT" />
</Root>
</log4j:configuration>
For some reason logging is not working. I am getting
ERROR StatusLogger Error parsing /home/sfalk/workspace/java/lazy-model-access/lamoa-parent/lamoa-server/target/classes/log4j2.xml
ERROR StatusLogger No logging configuration
What am I doing wrong here?
Your configuration file is for log4j 1.x, not 2.5
in the config file how to separate the package in different appaender using asynch
i have two different package and using different appender i want separate log output
first two appender is different package and third appender is different one
and i need all appender connect to asynch...
pls help me to solve this
here my config file..
<?xml version="1.0" encoding="UTF-8"?>
<configuration strict="true" status="WARN">
<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</Console>
<File name="MyFile" fileName="Logger/knight.log">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</File>
<File name="Except" fileName="log1/Execption9.log">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</File>
<Async name="Async">
<appender-ref ref="CONSOLE" />
<appender-ref ref="MyFile" />
<appender-ref ref="Except" />
</Async>
</appenders>
<loggers>
<root level="all">
<appender-ref ref="Async" />
</root>
</loggers>
</configuration>
You could configure three separate AsyncAppenders, one for each of the File appenders, and one for console, then configure root as:
<root>
<appender-ref ref="async-console" level="info" />
<appender-ref ref="async-file1" level="trace" />
<appender-ref ref="async-file2" level="error" />
</root>
But this solution gives you three threads. Why not use AsyncLoggers (using the selector command line option, see the docs for AsyncLoggers)?
That way you only have one logging thread, and it is probably a lot faster too...
I have a simple Netbeans 7.1.2 (NON MAVEN) project that use glassfish 3.1 server for testing.
I created a log4j2.xml file and placed it on the classpath
here it is
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="debug"/>
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} [%t] %-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="rolling-file" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="c:\tmp\Program-Name.log"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="4"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n"/>
</layout>
</appender>
<logger name="org.hibernate">
<level value="info" />
</logger>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
<appender-ref ref="rolling-file" />
</root>
</log4j:configuration>
the project uses hibernate to store data from a web service to a database.
However I am not able to log anything. I can see the logs of hibernate into the Netbeans IDE but I cannot see the created on the filesystem file.
I have this error when calling the web service
SEVERE: ERROR StatusLogger Unknown object "logger" of type org.apache.logging.log4j.core.config.LoggerConfig is ignored
SEVERE: ERROR StatusLogger root contains an invalid element or attribute "priority"
SEVERE: ERROR StatusLogger Unknown object "root" of type org.apache.logging.log4j.core.config.LoggerConfig is ignored
could someone pls help or give some advice I googled and stackoverflowed but without chance.
Paolo
I think that your log4j2.xml file is mixed with the old log4j style xml.
I'm having the same trouble as you when it comes to appending hibernate log to my application's log file. But I think that I can help with the logger error.
Try this file instead (and note the changes that I made):
<?xml version="1.0" encoding="UTF-8" ?>
<configuration name="SOME_PROJ_NAME" status="OFF">
<appenders>
<RollingFile name="rolling-file" fileName="c:/tmp/Program-Name.log" filePattern="c:/tmp/$${date:yyyy-MM}/Program-Name-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d{ABSOLUTE} [%t] %-5p %c{1} - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</appenders>
<loggers>
<root level="info"
<appender-ref ref="rolling-file"/>
</root>
<logger name="org.hibernate level="info">
<appender-ref ref="rolling-file"/>
</logger>
</loggers>
</configuration>
It is quite similar to the one that I'm using. Note that this file should be on classpath for log4j2 auto configuration to kick in.
The most important thing you should do when using the net to configure log4j2 is that you're looking at log4j2 and not log4j style configuration.
I suggest that you look in http://logging.apache.org/log4j/2.x/ for further info. They have a good downloadable pdf that you can check out.
In addition, check out my question about a similar problem.