Own log file for a class, thread java log4j2 - java

There is a class Task. Inherited from Thread.
How do I configure the log4j2 logger to create and write Task-1.log for each thread Task?
My xml:
<?xml version="1.0" encoding="UTF-8"?>
<Properties>
<Property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [thread-id %T] %l - %msg%n</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="${sys:logFolder}/test.log" filePattern="${sys:logFolder}/test-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${pattern}" />
</Console>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<appender-ref ref="console" />
<appender-ref ref="fileLogger" />
</Root>
<Logger name="com.test.*" level="info">
<appender-ref ref="fileLogger" level="info" />
</Logger>
</Loggers>

Related

Log4j2 : Changing log level for a specific user and specific loggers : not working as expected

Requirement is to change the log level for a specific user . Using DynamicThresholdFilter at the context level does work but applies to all the loggers . My requirement is to only apply it for specific loggers and not impact the others. With this configuration file all the appenders are logging at debug levell.
This is how my file looks like . Any recommendations on applying the same filter or any other filter which can enable debug logging for specific loggers ?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="30">
<Properties>
---------------------
</Properties>
<DynamicThresholdFilter
key="customLogging-enabled" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG" />
</DynamicThresholdFilter>
<Appenders>
<Console name="consoleAppender" target="SYSTEM_OUT"
follow="true">
<PatternLayout pattern="${CONSOLE_LOG_PATTERN}" />
</Console>
<RollingFile name="customLoggingAppender"
fileName="${sys:log_dir}/customLogging.log"
filePattern="customLogging-%d{yyyy-MM-dd}-%i.log" append="true">
<ThreadContextMapFilter onMatch="DENY"
onMismatch="NEUTRAL">
<KeyValuePair key="customLogging-enabled" value="true" />
</ThreadContextMapFilter>
<PatternLayout>
.......................
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<RollingFile name="standardAppender"
fileName="${sys:log_dir}/standard.log"
filePattern="standard-%d{yyyy-MM-dd}-%i.log" append="true">
<PatternLayout>
....................
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
.......More appenders here
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="consoleAppender" />
<AppenderRef ref="standardAppender" />
</Root>
<!-- DEFAULTED TO ROOT LOGGER - START -->
<Logger name="com.abc" level="ERROR">
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.abc.helpers" level="ERROR">
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.abc.business.persist.utils" level="ERROR" />
<Logger
name="com.abc.business.persist.ManagerFactory"
level="ERROR" />
<Logger name="com.da.handlers" level="ERROR" additivity="false">
<AppenderRef ref="abcAppender" />
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.handler.audit" level="ERROR"
additivity="false">
<AppenderRef ref="xyzAppender" />
</Logger>
</Loggers>
</Configuration>
I tried applying the same filter at the appender level but it did not work
<RollingFile name="customLoggingAppender" fileName="${sys:log_dir}/customLogging.log" filePattern="customLogging-%d{yyyy-MM-dd}-%i.log" append="true">
<DynamicThresholdFilter key="customLogging-enabled" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG"/>
</DynamicThresholdFilter>
<PatternLayout>
....
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
My expectation is that ONLY the loggers referring to this appender will be logging at debug level

Log4j2: not write into file

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>

Unable to log messages in console using Log4j2 but they get logged in the file

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>

CSV Layout in Apache log4j2 generating only log statements from particular set of classes. How do we enable it from all classes?

I recently enabled my log4j2 configuration to generate a CSV file from the loggers. The CSV file is generating fine. However, only the log statements from the web service client classes are realized in the CSV i.e. log statements from managers, action classes (Struts 2 Actions), Utility Classes, Mapper classes and web service request/response printing handlers are not being generated at all in the CSV file. What could be the reason for this?
Here is my code :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="infoLog.filename">/logs/info.log</Property>
<Property name="debugLog.filename">/logs/debug.log</Property>
<Property name="errorLog.filename">/logs/error.log</Property>
<Property name="communicationLog.filename">/logs/communication.log</Property>
<Property name="csvLog.fileName">/logs/csvLog</Property>
<Property name="file-header">column1,column2,column3</Property>
</Properties>
<Appenders>
<RollingFile name="csvFile" fileName="${csvLog.fileName}.csv"
filePattern="${csvLog.fileName}-%d{MM-dd-yyyy}-%i.csv">
<BurstFilter level="WARN" rate="16" maxBurst="100"/>
<CsvParameterLayout delimiter="," header="${file-header}\n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
<RollingFile name="debugFile" fileName="${debugLog.filename}"
filePattern="${debugLog.filename}-%d{MM-dd-yyyy}-%i.log">
<PatternLayout
pattern="%-5p [%t] -%-10c (%L):-(%method) %m%n%throwable" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
<RollingFile name="infoFile" fileName="${infoLog.filename}"
filePattern="${infoLog.filename}-%d{MM-dd-yyyy}-%i.log">
<PatternLayout
pattern="%d{dd MMM yyyy HH:mm:ss,SSS} %-5p %X{userId} [%t] -%-10c (%-L):-(%method) %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
<RollingFile name="errorFile" fileName="${errorLog.filename}"
filePattern="${errorLog.filename}-%d{MM-dd-yyyy}-%i.log">
<PatternLayout
pattern="%d{dd MMM yyyy HH:mm:ss,SSS} %-5p %X{userId} [%t] -%-10c (%-L):-(%method) %m%n%throwable" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
<RollingFile name="communicationLog" fileName="${communicationLog.filename}"
filePattern="${communicationLog.filename}-%d{MM-dd-yyyy}-%i.log">
<PatternLayout
pattern="%d{dd MMM yyyy HH:mm:ss,SSS} %-5p %X{userId} [%t] -%-10c (%-L):-(%method) %m%n%throwable" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout
pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | %X{userId} [%t] -%-10c (%-L):-(%method) %m%n" />
</Console>
</Appenders>
<Loggers>
<logger name="com.syb.core.manager" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="communicationLog" />
<appender-ref ref="csvFile" />
</logger>
<logger name="com.syb.as.manager" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="communicationLog" />
<appender-ref ref="csvFile" />
</logger>
<logger name="com.syb.ao.manager" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="communicationLog" />
<appender-ref ref="csvFile" />
</logger>
<logger name="error.unhandled" additivity="false">
<level value="ERROR" />
<appender-ref ref="STDOUT" level="INFO" />
<appender-ref ref="errorFile" level="ERROR" />
<appender-ref ref="csvFile" />
</logger>
<Root level="debug" additivity="false">
<AppenderRef ref="debugFile" level="debug"/>
<AppenderRef ref="errorFile" level = "error"/>
<AppenderRef ref="infoFile" level="info"/>
<AppenderRef ref="STDOUT" level="info"/>
<AppenderRef ref="csvFile" level="all"/>
</Root>
</Loggers>
Is there anything that could be done to enable CSV record generation in CSV files for log statements throughout the application? It would be really great if you could help.

log4j vs log4j2: log to different file

Using log4j I can log to different files invoking this method:
Logger.getLogger("test")
where test is an appender defined in log4j.properties. How can I log to different files with log4j2? This is my configuration file and I would choose programmatically where to log:
<Properties>
<Property name="log-path">C:/logs</Property>
</Properties>
<Loggers>
<Logger name="it.mypackage" level="debug" additivity="false">
<appender-ref ref="file" level="debug" />
<appender-ref ref="file2" level="error" />
</Logger>
</Loggers>
<Appenders>
<!-- file.log -->
<RollingFile name="file" fileName="${log-path}/file.log" filePattern="${log-path}/file-%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>
<!-- file2.log -->
<RollingFile name="file2" fileName="${log-path}/file2.log" filePattern="${log-path}/file2-%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>
[SOLUTION]
Oh Yes, I'm missing a logger:
<Properties>
<Property name="log-path">C:/logs</Property>
</Properties>
<Loggers>
<Logger name="logger1" level="debug" additivity="false">
<appender-ref ref="file" level="debug" />
</Logger>
<Logger name="logger2" level="debug" additivity="false">
<appender-ref ref="file" level="debug" />
<appender-ref ref="file2" level="error" />
</Logger>
</Loggers>
<Appenders>
<!-- file.log -->
<RollingFile name="file" fileName="${log-path}/file.log" filePattern="${log-path}/file-%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>
<!-- file2.log -->
<RollingFile name="file2" fileName="${log-path}/file2.log" filePattern="${log-path}/file2-%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>
I can choose logger in this way:
private static Logger logger = LogManager.getLogger("logger1");
If you keep the rest of the configuration the same, but modify the Loggers section:
<Loggers>
<Logger name="logger1" level="debug" additivity="false">
<appender-ref ref="file" level="debug" />
</Logger>
<Logger name="logger2" level="debug" additivity="false">
<appender-ref ref="file2" level="error" />
</Logger>
</Loggers>
Now, you can select the appender in your code by getting the logger by name:
Logger logger1 = LogManager.getLogger("logger1");

Categories