Empty log file when log4j2.xml is outside the jar - java

When log4j2.xml is in the jar (in resources), then everything works. But when I moved log4j2.xml outside the jar, then log file is created but it's empty all the time. I run my application in that way:
java.exe -Dfile.encoding=UTF-8 -Dlog4j.configurationFile=C:\config\log4j2.xml -jar C:\app\myApp.jar
Here is my log4j2.xml file:
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<!-- Rolling File Appender -->
<RollingFile name="FileAppender" fileName="logs/application.log"
filePattern="logs/application-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="FileAppender" />
</Root>
</Loggers>

Try using the URI: file://c:/config/log4j2.xml

Related

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.

log4j2 writing method and class

I'm using log4j2 in a java program..
this is the line of code where it is initialized
private static final Logger logger = LogManager.getLogger("application-
log");
and this is the configuration file where the format is decided
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="30" status="info" strict="true">
<Properties>
<Property name="logApplicativo">../logs/logApplicativo.log</Property>
<Property name="logCdr">../logs/logCdr.log</Property>
</Properties>
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="%m MDC%X%n"/>
</Console>
<RollingRandomAccessFile name="fileLogApplicativo"
fileName="${logApplicativo}" filePattern="${log-Applicativo}-%d{yyyy-MM-
dd}-%i.log" immediateFlush="false" append="true"
ignoreExceptions="false" >
<PatternLayout>
<pattern>%d [%-6p] %C.%M(%F:%L) - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile >
<RollingRandomAccessFile name="fileLogCdr" fileName="${logCdr}"
filePattern="${log-Cdr}-%d{yyyy-MM-dd}-%i.log" >
<PatternLayout>
<pattern>%d %-5p %c{2} - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncRoot level="error">
<AppenderRef ref="STDOUT"/>
</AsyncRoot>
<AsyncLogger name="application-log" level="debug" additivity="false">
<AppenderRef ref="fileLogApplicativo" level="debug"/>>
</AsyncLogger>
<AsyncLogger name="cdr-log" level="debug" additivity="false">
<appender-ref ref="fileLogCdr" level="debug"/>
</AsyncLogger>
</Loggers>
I do not understand why, but class name, code name, line code arent printed. Probably the problem is trivial but I'm going crazy from one day.
Thank you all
If you're using asynchronous loggers or asynchronous appenders try adding includeLocation="true" to your logger or appender configuration.
You can read more about it in the log4j2 manual

Start configuration snippet for log4j2.properties?

I'm looking for a log4j2.properties snippet that I can drop in to src/test/resources just to make the No logj2 configuration file found warning disappear. If it contains other useful sample like configuration examples that's great as well.
Basically I'm looking for the .properties version of this:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss} %c{1.} [%t] %-5level} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
I haven't tried it but this should do the trick.
status = error
name = PropertiesConfig
property.filename = target/logs/test.log
appender.file.type = File
appender.file.name = LogFile
appender.file.fileName = ${filename}
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d %p %C{1.} [%t] %m%n
rootLogger.level = error
rootLogger.appenderRef.stdout.ref = LogFile
Here is a log4j2.xml that I actually use in some of my unit tests.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR">
<properties>
<property name="LOG_DIR">target/logs</property>
</properties>
<MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ABSOLUTE} %-5level # %class.%method %m%n" />
</Console>
<RollingFile name="log4j" fileName="${LOG_DIR}/log4j.txt" filePattern="${LOG_DIR}/archive/log4j.txt.%d{yyyyMMdd_HH}-%i">
<PatternLayout>
<MarkerPatternSelector defaultPattern="%d [%t] %-5p %X{loginId, userId, ipAddress, corpAcctNumber} %C{1.}.%M:%L - %m%n">
<PatternMatch key="FLOW" pattern="%d [%t] %-5p %X{loginId, userId, ipAddress, corpAcctNumber} -------- %C{1.}.%M:%L %msg --------%n"/>
</MarkerPatternSelector>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="30 MB"/>
</Policies>
<DefaultRolloverStrategy min="1" max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="log4j" />
</Root>
</Loggers>
</Configuration>

issue with my log4j2 setup

I was using log4j2 and it was logging statements for me with no issues. I might have made some changes (moved from info to debug and back) but its quite possibly I might have messed up the config in some other fashion. I am copying my config file below (I have not moved any log4j2 and slf4j jar files from my project). Any thoughts?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="TestApp" packages="">
<Appenders>
<RollingRandomAccessFile name="RollingRandomAccessFile" fileName="logs/test.log" immediateFlush="false" append="false"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="FATAL_LOGGER" level="fatal" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<Root level="debug" includeLocation="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
I tested the config in a test project and it works without any issues. Ensure that you do not have any locks on the file and have correct read/write privileges on the file/directory.

How to write different logs in different files with log4j2 (MDC in xml)?

Now I'm using structure like this:
Appender:
<RollingFile name="user.log" append="true" fileName="users/%MDC{USERNAME}.txt"
filePattern="users/archive/%MDC{USERNAME}-%d{MM-dd-yyyy}-%i.txt.gz">
<PatternLayout pattern="%-5p %d{MMMM-dd HH:mm:ss} %X: %c - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
</RollingFile>
Logger:
<appender-ref ref="user.log">
<ThreadContextMapFilter onMatch="ACCEPT" onMismatch="DENY" operator="or">
<KeyValuePair key="USERNAME" value="%X{USERNAME}"/>
<KeyValuePair key="IP" value="%X{IP}"/>
</ThreadContextMapFilter>
</appender-ref>
But it doesn't work with MDC keys. How could I use MDC keys in xml to config RollingFileAppender?
Take a look at RoutingAppender. Maybe this can get you started:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="DEBUG" name="MyApp" packages="">
<appenders>
<Routing name="Routing">
<Routes pattern="$${ctx:USERNAME}">
<Route>
<RollingFile name="user.log" append="true" fileName="users/${ctx:USERNAME}.txt"
filePattern="users/archive/${ctx:USERNAME}-%d{MM-dd-yyyy}-%i.txt.gz">
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
</appenders>
<loggers>
<root level="TRACE">
<appender-ref ref="Routing" level="DEBUG" />
</root>
</loggers>
</configuration>

Categories

Resources