I'm trying to achive the following using log4j2:
Log all general content to console, but log MarkerFilter.FILTERED only to a specific file, not to console.
<Configuration>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
<RollingFile name="FILTERED" fileName="filtered.txt" />
<MarkerFilter marker="FILTERED" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="CONSOLE" />
<AppenderRef ref="FILTERED" />
</Root>
</Loggers>
</Configuration>
The following works in general, BUT logs any FILTERED content to console additionally.
But when I remove the <AppenderRef ref="FILTERED" /> form <root> loggers, nothing is logged to the file anymore!
How about using the marker filter on the console appender with a DENY action?
You can set the level on the appender-ref, so you don't need to use a ThresholdFilter for that.
<Configuration status="warn">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<MarkerFilter marker="FILTERED" onMatch="DENY" onMismatch="ACCEPT"/>
</Console>
<RollingFile name="FILTERED" fileName="filtered.txt" />
<MarkerFilter marker="FILTERED" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="CONSOLE" level="info" />
<AppenderRef ref="FILTERED" />
</Root>
</Loggers>
</Configuration>
Related
I added Log4j2 into my project
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
and added next properties:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx"/>
<!-- Крутое, но бесцветное логирование-->
<!-- <PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}.%M():%L - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}"/>-->
</Console>
<RollingFile name="File" fileName="C:/Users/Mi/output.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz" immediateFlush="true">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
<Logger name="org.apache.logging.log4j.filelogger" level="info" additivity="false">
<AppenderRef ref="File"/>
</Logger>
</Loggers>
</Configuration>
App write logs to console, but not in file. As I wait, app shall write logs into file by url C:/Users/Mi/output.log, it creates automatically, I can't delete it while app is wotking, but there no logs. Please advice, what to do.
You are only logging stuff from org.apache.logging.log4j.filelogger package into the "File" appender. The Logger name property is the package/class reference
You should do something like this to get logging to file instead of console:
<Root level="info">
<AppenderRef ref="File"/>
</Root>
If you want console and file do:
<Root level="info">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Root>
I changed the path of log4j2.xml configuration file and now the log files will create but application doesn't log into the files. I get no error messages.
My order structur:
Log4j2.xml:
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5level ${hostName} --- [%15.15thread] %-100.100class : %message%n%exception
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="RootAppender" fileName="./log/log.log" filePattern="./log/log-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100MB" />
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
<RollingFile name="CurlAppender" fileName="./log/curl.log" filePattern="./log/curl-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="500MB" />
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Root>
<Logger name="CurlLogger" level="trace" additivity="false">
<AppenderRef ref="CurlAppender"/>
</Logger>
</Loggers>
I've found the issue.
In the application.properties file there must be set the "logger.config" attribute.
After setting this attribute, the file logging works.
I think there is some issue in Loggers tag.
In the Logger name please add your package name and also change your AppenderRef in Logger.
Please change your xml code to below:
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Root>
<Logger name="com.example.springbackend.controller" level="trace" additivity="false">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Logger>
</Loggers>
Here <Logger name="com.example.springbackend.controller" level="trace" additivity="false"> Logger name is project package name.
I think the issue is in root level.
Please change the root level logger to debug or trace.
So I am trying to log the info and error messages to both the console and the log file.
The messages get saved successfully in the log file but do not get logged in the console. Have attached the xml file below:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="basePath">./DemoLogs</Property>
</Properties>
<Appenders>
<RollingFile name="File" fileName="${basePath}/app.log" filePattern="${basePath}/app-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %c{2} -%msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100 KB"/>
</Policies>
</RollingFile>
<Console name="Console">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="File"/>
</Root>
<Logger name="Demo" level="debug" additivity="false">
<AppenderRef ref="File"/>
</Logger>
</Loggers>
</Configuration>
You need to add a reference to the console appender.
<Root level="all">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Root>
My log4j2.xml is as follows :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Async name="ASYNC">
<AppenderRef ref="R"/>
</Async>
<RollingFile name="R" fileName="${sys:catalina.home}/logs/myServer.log" filePattern="${sys:catalina.home}/logs/myServer.log.%d{yyyy-MM-dd}">
<PatternLayout pattern="%d %-5p [%t] %C{2} - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.ps" level="WARN" additivity="false">
<AppenderRef ref="ASYNC"/>
</Logger>
<Root level="WARN">
<AppenderRef ref="ASYNC"/>
</Root>
</Loggers>
</Configuration>
This configuration only creates myServer.log file and does not roll file also does not write logs into it.
Log file is created with permissions :
ls -ltr my*
-rw-------. 1 root root 0 Feb 14 18:15 myServer.log
What I am doing wrong?
You should add reference to your Rolling file Appender to get logs written in your File.
<Root level="WARN">
<AppenderRef ref="ASYNC"/>
<AppenderRef ref="R"/>
</Root>
More information on configuring log4j2 can be found here
Add <AppenderRef ref="R"/> as well under <Root level="WARN"> tag and try again.
I've been searching the web for days now but somehow nothing seems to help.
I try to migrate from log4j 1.x to log4j 2 but something is not right here and I hope one of you can help.
I have a JSF application running on Websphere 8 and this is is my configuration so far:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Filter type="ThresholdFilter" level="trace"/>
<Appenders>
<RollingFile name="DEBUG" filePattern="C:/Files/varm_debug.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_debug.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="INFO" filePattern="C:/Files/varm_info.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_info.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="WARN" filePattern="C:/Files/varm_warn.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_warn.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="ERROR" filePattern="C:/Files/varm_error.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_error.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<File name="comparisonLogAppender" fileName="C:/Files/VARM-output.log" append="true">
<PatternLayout pattern="%d{ISO8601}; %m%n"/>
</File>
</Appenders>
<Loggers>
<!-- Logger to compare results -->
<!-- Logger to log infos/errors throughout comparison -->
<Logger name="comparisonLogger" level="trace" additivity="true" immediateFlush="true">
<AppenderRef ref="comparisonLogAppender"/>
</Logger>
<Logger name="de.saltsolutions.varm" level="info" >
<AppenderRef ref="DEBUG" level="debug"/>
<AppenderRef ref="INFO" level="info"/>
<AppenderRef ref="WARN" level="warn"/>
<AppenderRef ref="ERROR" level="error"/>
</Logger>
<Logger name="org.hibernate" level="warn">
<AppenderRef ref="DEBUG" level="debug"/>
<AppenderRef ref="INFO" level="info"/>
<AppenderRef ref="WARN" level="warn"/>
<AppenderRef ref="ERROR" level="error"/>
</Logger>
<Root level="debug">
<AppenderRef ref="DEBUG" level="debug"/>
</Root>
<Root level="info">
<AppenderRef ref="INFO" level="info"/>
</Root>
<Root level="warn">
<AppenderRef ref="WARN" level="warn"/>
</Root>
<Root level="error">
<AppenderRef ref="ERROR" level="error"/>
</Root>
</Loggers>
</Configuration>
All the files are generated okay but they stay empty.
My main problem is the comparisonLogger. If I use it (LogManager.getLogger("comparisonLogger") in my main class, it writes the log file perfectly.
But when I try using the exact same logger in my ManagedBean (ViewScoped) nothing gets logged.
Does anybody know why it doesn't work in my ManagedBean? Does this have anything to do with the Websphere?
I hope anybody can help because I have absolutely no idea what I could possibly do to make this work.
Thanks so much in advance
I finally figured out my problems.
I use Log4j2 in a web application which means that I needed to add some stuff in my web.xml (further information here: http://logging.apache.org/log4j/2.x/manual/webapp.html)
You can only configure ONE root logger
Once I adjusted that, everything worked just as expected.
My Log4j2 Jar-Files are now in my client and my ear and my XML config is in my client but it also works if you split the config files (one part in my web project the other for the client on C:\temp or some other directory)
You may need to put the log4j2 jar files and log4j2.xml config in a place so that they are always in the classpath.