我在spring-boot应用程序中使用Log4j,因为我需要自动配置重新加载。我能够配置它,自动配置工作正常。但问题是我想创建单个log4j2.properties文件,其中相同的日志可以写入console和log.file 。我已经尝试过它,但没有运气。以下是将日志写入控制台的示例文件。
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
答案 0 :(得分:0)
如果您不仅限于使用.properties
并且可以使用.yaml
,那么这是一个有效的示例。
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
参考:here
注意:您需要在jackson-dataformat-yaml
的类路径中包含jackson-databind
和yaml
才能正常工作。
答案 1 :(得分:0)
这将帮助您在控制台和E2Elog.txt文件中编写日志。
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
除此之外,您还必须添加以下代码。
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;
}
}
希望这会奏效:)
答案 2 :(得分:0)
将rootLogger.appenderRef.file.ref = LOGFILE
添加到配置文件的底部。
实际上,我更喜欢使用 .xml 而不是 .properties ,因为我认为 .xml 更加用户友好。
E.G。
<?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>