I am using Log4j in spring-boot app because I needed automatic configuration reloading. I am able to configure it and automatic configuration is working fine. BUT the problem is I want to create single log4j2.properties file where same logs can be written to console and log.file . I have tried my hand with it but no luck. Here is the sample file which writes logs to console.
name=PropertiesConfig
property.filename = appLogger
appenders = console, file
monitorInterval=10
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=.\\logs\\appLogger.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
rootLogger.level = INFO
rootLogger.appenderRefs = stdout,LOGFILE
rootLogger.appenderRef.stdout.ref = STDOUT
If you are not limited to using .properties
and can use .yaml
then here is a working example.
Configutation:
name: Default
Properties:
Property:
name: log-path
value: "/var/log/service"
Appenders:
#Appender will write the logs to console
Console:
name: Console_append
target: SYSTEM_OUT
PatternLayout:
pattern: "%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=blue, DEBUG=green bold, TRACE=black}"
#Appender will write the logs to File
File:
name: file_append
fileName: ${log-path}/service.log
PatternLayout:
pattern: "%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=blue, DEBUG=green bold, TRACE=black}"
#Appender will write the logs to rolling file
RollingFile:
- name: Roll_file_append
fileName: ${log-path}/service-rolling.log
filePattern: "${log-path}/service.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:
#Roll the log file and zip if if the size exceeds 1MB
SizeBasedTriggeringPolicy:
size: 1 MB
#Keep below defined number of rolling files before deleting them
DefaultRollOverStrategy:
max: 45
Loggers:
#Root logging level for application
Root:
level: info
AppenderRef:
- ref: Console_append
- ref: file_append
#Application specific logging
Logger:
- name: com.system.serivce
additivity: false
level: debug
AppenderRef:
- ref: Console_append
- ref: file_append
- ref: Roll_file_append
Reference: here
Note: you need to include jackson-dataformat-yaml
and jackson-databind
at the class path for yaml
to work.
This will help you to write the logs on console as well as in E2Elog.txt file.
log4j.rootLogger=INFO,CONSOLE,LOGFILE,TESTAPPENDER
log4j.appender.TESTAPPENDER=com.automate.commonUtils.TestNGReportAppender
log4j.appender.TESTAPPENDER.layout=org.apache.log4j.PatternLayout
log4j.appender.TESTAPPENDER.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Follow=true
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n
log4j.appender.LOGFILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOGFILE.File=E2Elog.txt
log4j.appender.LOGFILE.DatePattern='.'yyyy-MM-dd
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %m%n
Apart from this you have to add below code also.
package com.automate.commonUtils;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
import org.testng.Reporter;
public class TestNGReportAppender extends AppenderSkeleton {
@Override
protected void append(final LoggingEvent event) {
Reporter.log(eventToString(event));
}
private String eventToString(final LoggingEvent event) {
final StringBuilder result = new StringBuilder(layout.format(event));
if(layout.ignoresThrowable()) {
final String[] s = event.getThrowableStrRep();
if (s != null) {
for (final String value : s) {
result.append(value).append(Layout.LINE_SEP);
}
}
}
return result.toString();
}
@Override
public void close() {
}
@Override
public boolean requiresLayout() {
return true;
}
}
Hope this will work :)
Add rootLogger.appenderRef.file.ref = LOGFILE
to the bottom of your configuration file.
Actually, I prefer using .xml instead of .properties , because I think .xml is more user-friendly.
EG
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n"/>
</Console>
<File name="MyFile" fileName="logs/app.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.