issue with my log4j2 setup - java

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.

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.

Log4j2.xml doesn't create file suddenly in java hibernate framework

I have a maven project in NetBeans with hibernate framework. For a long time it was doing its job correctly. After some modification in database, console log is still working but writing to file has stopped without any error or exception. I have deleted log file directory and restart project, but this time it couldn't even create file path directory.
here is my log4j2.xml configuration(in src/main/resources)
I have changed path to D:\logs and D:/logs, and tried different levels (debug, info, error). Also tried to run my "Server.jar" from command line by administrator. And it should be mentioned that I tried the solution in Log4J creates log file but does not write to it and lots of other suggested solutions, but no success achieved.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Properties>
<Property name="path">${sys:user.home}/logs/server</Property>
</Properties>
<Appenders >
<RollingFile name="connections" append="true" fileName="${path}/connections.log"
filePattern="${path}/connections-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="unexpected-events" append="true" fileName="${path}/unexpected-events.log"
filePattern="${path}/unexpected-events-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="readouts" append="true" fileName="${path}/readouts.log"
filePattern="${path}/readouts-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="connections" level="info" additivity="false">
<appender-ref ref="connections" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Logger name="unexpected-events" level="info" additivity="false">
<appender-ref ref="unexpected-events" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Logger name="readouts" level="info" additivity="false">
<appender-ref ref="readouts" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Root level="error" additivity="false">
<!--<AppenderRef ref="console-log"/>-->
</Root>
</Loggers>
</Configuration>
and this is how I used it in my java class:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......
private static final Logger connectionsLog = LogManager.getLogger("connections");
....
connectionsLog.info("device" + deviceNumber + " disconnected");
I have seen weirdness like this when both commons-logging and jcl-over-slf4j are on the classpath together. Excluding commons-logging from the Maven classpath, anywhere it appears, often helps.
By special thanks to user944849 , I understood that the problem could be related to maven dependencies. Then I checked previous version of my project (which has correct logging) and compare dependencies and found out that a library "log4j-to-slf4j" is added (which has added because of importing "org.springframework.boot"). Then first I exclude it from dependency in pom.xml file, as a result it worked fine in NetBeans IDE but not out of it. Then I removed that dependency from pom.xml and problem solved like a charm!

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

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