specific logs for multiple war having one ear approach - java

specific logs for multiple war having one ear .How to configure the same using log4j2.xml as i am not able to find any much help in this.i have 5 wars having one ear and when i configure the logs all war logs are printing to only one log file which got loaded first by the class loader.i want my log file to be separate out for each war in single ear.any help would be appreciated.

configuration xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<RollingFile name="app4war1" fileName="/logs/war1/app.log" filePattern="/logs/war1/app-%d{MM-dd-yyyy}.log.gz" ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<RollingFile name="app4war2" fileName="/logs/war2/app.log" filePattern="/logs/war2/app-%d{MM-dd-yyyy}.log.gz" ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="log4war1" level="trace">
<AppenderRef ref="app4war1" />
</Logger>
<Logger name="log4war2" level="trace">
<AppenderRef ref="app4war2" />
</Logger>
<Root level="error">
<AppenderRef ref="STDOUT" />
</Root>
</Loggers>
</Configuration>
and in your web applications use your own implementaion of LogManager.
package myloggermanagerimpl;
import org.apache.logging.log4j.Logger;
public class LogManager {
private static final String prefix = "log4war1.";
public static Logger getLogger(){
return org.apache.logging.log4j.LogManager.getLogger(prefix);
}
public static Logger getLogger(Class<?> clazz){
return org.apache.logging.log4j.LogManager.getLogger(prefix + clazz.getName());
}
// and other getLogger() methods in the same manner
}
now, you should implement this class in each of your web projects changing prefix for each.
then refactor your projects to change import org.apache.logging.log4j.LogManager; to import myloggermanagerimpl.LogManager;

Related

Changing log level for a keyword in log4j2

I am trying to change log levels programmatically for a given keyword. For example If I have the log level set to OFF but want to see logs that contain "database" keyword, how can I do that? Is there a way to to that via java?
#fatCop's answer is mostly correct. You can start with log4j2.xml configured as
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="AppName" packages="">
<RegexFilter regex="*database*" onMatch="DENY" onMismatch="DENY"/>
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Because this is using a global filter it will cause nothing to be logged. When you are ready to enable logging do
final LoggerContext loggerContext = LoggerContext.getContext(false);
final Configuration config = loggerContext.getConfiguration();
Filter filter = RegexFilter.createFilter("database", null, false, Result.ACCEPT, RESULT.DENY);
config.setFilter(filter);
When you want it disabled then replace the filter with a new one that is set back to DENY on a match.
Note that the logging level on the root logger is irrelevant since the filter is only accepting or denying events.
As per Log4j Filter Manual, you can use RegexFilter. Snippet referring to manual, the configuration for you can be like:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="AppName" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex="*database*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>

Log4j2.xml configuration file not found after configuration change

I'm having a problem with the log4j2.xml configuration file not being found after a change to the configuration file was made. Before the change the file was found and the simple logging was working. I haven't used log4j in a few years and have been making use of the java.util logging for application logging on the weblogic server so I decided to put log4j to use again.
I first create a simple configuration file, tested it out and everything was working just fine.
The original test configuration.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="filename">/opt/www/log</Property>
</Properties>
<Appenders>
<RollingFile name="StipLog" fileName="${filename}/stip-log.log" filePattern="${filename}/stip-log-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="stip-debug" level="debug" additivity="true">
<appender-ref ref="StipLog" level="debug"/>
</Logger>
<Root level="debug">
<AppenderRef ref="StipLog"/>
</Root>
</Loggers>
<Configuraiton>
Runing this code snippet in a backing bean for a page worked just fine with the original configuration file.
LOGGER.info("APP Home Page: User: ".concat(userInfoBean.getUserName()));
LOGGER.debug("DEBUG message!");
LOGGER.error("ERROR Message", new NullPointerException("I AM NULL"));
That all went well so I decided to try and configure different appenders for each log level I wanted to use. I changed the original configuration to the following.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<!-- For local logging change this porperty to a local directory. ex: C:\\Public\\log -->
<Property name="LOGGING_ROOT">C:/Public/log</Property>
</Properties>
<Appenders>
<!-- file appenders -->
<RollingFile name="debugLog" fileName="${LOGGING_ROOT}/app-debug.log" filePattern="${LOGGING_ROOT}/app-debug-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="infoLog" fileName="${LOGGING_ROOT}/app-info.log" filePattern="${LOGGING_ROOT}/app-info-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="errorLog" fileName="${LOGGING_ROOT}/app-error.log" filePattern="${LOGGING_ROOT}/app-error-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<!-- console appender -->
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="debug-logger" level="debug" additivity="true">
<appender-ref ref="debugLog"/>
</Logger>
<Logger name="info-logger" level="info" additivity="true">
<appender-ref ref="infoLog"/>
</Logger>
<Logger name="error-logger" level="error" additivity="true">
<appender-ref ref="errorLog"/>
</Logger>
<Root level="all">
<AppenderRef ref="console"/>
<AppenderRef ref="debugLog"/>
<AppenderRef ref="infoLog"/>
<AppenderRef ref="errorLog"/>
</Root>
</Loggers>
</Configuration>
After changing the configuration file I am now getting the following error message when the application is deploying.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
The configuration file is in the same location as it was when the loggin was working, it's in the source default package. I moved it WEB-INF to see if that would make a difference but the same problem remains. I even changed it back to the original configuration and still have the same problem.

How to define Log4j2 appender logging specific named logger only?

private static final Logger log = LogManager.getLogger(ABC.class);
private static final Logger resultLog = LogManager.getLogger("ResultLog");
How can we define a appender that would only logs specific to "ResultLog" logger?
My current Log4j2.xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="ABC" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/ABC.log"
filePattern="logs/$${date:yyyy-MM}/ABC-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d [%t] %p %c{1.} %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
<File name="resultFile" fileName="logs/result.log">
<PatternLayout pattern="%m%n"/>
</File>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %p %c{1.} %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="RollingFile"/>
<AppenderRef ref="Console"/>
<AppenderRef ref="resultFile"/>
</Root>
</Loggers>
</Configuration>
thanks
Add
<Logger name="ResultLog">
<AppenderRef ...>
</Logger>
to the Loggers tag.
You can refer to this manual: https://logging.apache.org/log4j/2.x/manual/configuration.html#XML

log4j: How can I direct logs into different files for a cron and for a webservice?

I have a Cron and a Webservice, both implemented using spring. The cron and the webservice use a set of classes A, B and C to achieve their objective.
In each class, I use log4j 2 as the logging mechanism as so:
Logger log = LogManager.getLogger(A.class.getName());
In the log4j.xml, I have a single RollingAppender which logs to a file.
Now, I would like the Cron to log to a different file i.e. use a different appender. But if I set the category for the cron to use a different appender, that still doesn't cause the logs from A, B and C to go into that appender.
Update: log4j configuration:
<Configuration status="warn" name="mylogger" packages="">
<Properties>
<Property name="baseDir">/var/log/tomcat</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}.log.gz">
<PatternLayout><Pattern>%5p %d{ISO8601} [%t][%x] %c - %m%n</Pattern></PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
You can use below mentioned configuration if you want to log into different files using same class.
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="rollingFileAppender"
fileName="/data/abc.log"
filePattern="/data/abc-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
<RollingFile name="rollingFilesAppender"
fileName="/data/cde.log"
filePattern="/data/fgh-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="CONSOLE" />
</Root>
<Logger name="rollingFilesLogger" additivity="false" level="WARN">
<AppenderRef ref="rollingFilesAppender" />
</Logger>
<Logger name="com.log4jtest" additivity="false" level="INFO">
<AppenderRef ref="rollingFileAppender" />
</Logger>
</Loggers>
In Java file you can use like :
private static final Logger LOGGER = LogManager.getLogger(Hello.class);
private static final Logger SECOND_LOGGER = LogManager.getLogger("rollingFilesLogger");
Using this you will be able to send logs in two different files.
Ref : https://github.com/ragnar-lothbrok/log4j2-example

Log4j2 saving file (using RollingFile appender)

I am trying log4j2 to create log file to the system I am developing right, I have followed the instruction on their site and there is no error occurred when I run it, but the log is not saved on where I set it (ex. "D:\logs\app.log").
Here is My log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<RollingFile name="MyRollingFile" fileName="D:/logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<logger name="Log_RollingFile" level="TRACE" additivity="false">
<appender-ref ref="MyRollingFile"/>
</logger>
<root level="ERROR">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
I tried to :
delete app.log to see if my configuration (D:\logs\app.log) works. When I run the application it creates app.log so I think it means that it sees the configuration and the only thing is it is NOT SAVING the log.info that I did in java application
Change root level to "TRACE", and it prints the log.info.
[EDIT:]
I have also these libraries on my classpath
log4j-api-2.0-beta3.jar
log4j-core-2.0-beta3.jar
Am I missing something on RollingFile configuration or a library (maybe)?
Thanks in advance.
Your logger name is incorrect.
As explained in the configuration instructions you linked to, the logger should be named according to the package/classes you wish to capture logging for.
In their example the logger named com.foo.Bar would log everything from the Bar class in package com.foo with TRACE level.

Categories

Resources