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>
Related
Below is my log4j2.xml'
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="LogToFile" fileName="logs/myapp_-${date:yyyyMMddHHmmss}.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="LogToConsole"/>
<AppenderRef ref="LogToFile"/>
</Root>
</Loggers>
</Configuration>
It generates log files based on timestamp in logs/ directory.
I want to print the logfile name ( value of LogToFile variable ) in my java code and give to user so that he can come to know what is the exact location and name of the log file of his run.
Any idea if this is possible?
Kind Regards,
Abhi
I changed my logging from sync to async but I am not sure how to put policies on it. I want to apply my sync logging settings to async logging. Please see below.
// I switched from this
<RollingFile name="fileLogger" fileName="${logPath}/log.log"
filePattern="${logPath}/log-%d{yyyy-MM-dd-hh}-%i.log">
<PatternLayout>
pattern="${logPattern}"/>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
//to this
<File name="prodLog" fileName="${logPath}/log.log">
<PatternLayout
pattern="${logPattern}"/>
</File>
<Async name="asyncLogger" includeLocation="true">
<AppenderRef ref="prodLog"/>
<ArrayBlockingQueue/>
</Async>
I dont think you can use the same policies for the File appender.
To achieve an asynchronous behavior for a rolling file, you could use the RollingRandomAccessFile, just like the following example:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>>
</PatternLayout>
</Console>
<RollingRandomAccessFile name="Rolling-Random-Access-File-Appender"
fileName="logs/rollingrandomaccessfile.log"
filePattern="archive/logs/rollingrandomaccessfile.log.%d{yyyy-MM-dd-hh-mm}.gz">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="guru.springframework.blog.log4j2async" level="debug">
<AppenderRef ref="Rolling-Random-Access-File-Appender"/>
</AsyncLogger>
<Root level="debug">
<AppenderRef ref="Console-Appender"/>
</Root>
</Loggers>
</Configuration>
More on this in this post: https://springframework.guru/asynchronous-logging-with-log4j-2/
Regards
I have bellow xml config file with static log filename "CRM_Update_dubg_Logs.log", I want to make this name with timestamp, java log4j 2.9.1
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS}{GMT+3} [%t] %-5level
%logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="CRM_Update_Logs.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS}{GMT+3} %c{2} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="dubg_Logs" fileName="CRM_Update_dubg_Logs.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS}{GMT+3} %c{2} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
<AppenderRef ref="MyFile"/>
<AppenderRef ref="dubg_Logs"/>
</Root>
<Logger level="ALL" additivity="false" name="dubg">
<AppenderRef ref="dubg_Logs"/>
</Logger>
</Loggers>
Edit filename in xml
fileName="CRM_Update_Logs.log"
to be
fileName="${sys:logFilename}_CRM_Update_Logs.log"
and add this to your java code
Date date = new Date();
String LogDate= new SimpleDateFormat("yyyyMMdd").format(date);
System.setProperty("logFilename", LogDate);
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
I'm getting the following error when I run my Log4j code :
2015-07-04 19:08:04,385 ERROR Appender AuditLogger cannot be located. Route ignored
I'm trying to use the RoutingAppender (I learned about it from this question ).
My log4j config files looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="MyFile" fileName="OutputLogFile.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<Routing name="Routing">
<Routes pattern="$${sd:type}">
<Route>
<RollingFile name="Rolling-${sd:type}" fileName="${sd:type}.log"
filePattern="${sd:type}.%i.log.gz">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<SizeBasedTriggeringPolicy size="500" />
</RollingFile>
</Route>
<Route ref="AuditLogger" key="Audit"/>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="all">
<Appender-Ref ref="Console"/>
<Appender-Ref ref="MyFile"/> <!-- added_in now -->
</Root>
</Loggers>
</Configuration>
And my code is just a simple client/server pair (with some logger.debug("xyz") code spread throughout) .
thanks