我使用log4j,并希望将某些记录器的输出路由到特定的文件。我已经有了多个appender。现在,为了使调试更容易,我想告诉log4j,由一个特定的类(例如foo.bar.baz)生成的输出应该被写入一个特定的日志文件。这能做到吗?
pxyaymoc1#
举个例子:
log4j.rootLogger=ERROR, logfile log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender log4j.appender.logfile.datePattern='-'dd'.log' log4j.appender.logfile.File=log/radius-prod.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n log4j.logger.foo.bar.Baz=DEBUG, myappender log4j.additivity.foo.bar.Baz=false log4j.appender.myappender=org.apache.log4j.DailyRollingFileAppender log4j.appender.myappender.datePattern='-'dd'.log' log4j.appender.myappender.File=log/access-ext-dmz-prod.log log4j.appender.myappender.layout=org.apache.log4j.PatternLayout log4j.appender.myappender.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n
b0zn9rqh2#
下面是关于XML配置的答案,请注意,如果不给予文件附加器一个ConversionPattern,它将创建0字节文件,并且不写入任何内容:
ConversionPattern
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> </layout> </appender> <appender name="bdfile" class="org.apache.log4j.RollingFileAppender"> <param name="append" value="false"/> <param name="maxFileSize" value="1GB"/> <param name="maxBackupIndex" value="2"/> <param name="file" value="/tmp/bd.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> </layout> </appender> <logger name="com.example.mypackage" additivity="false"> <level value="debug"/> <appender-ref ref="bdfile"/> </logger> <root> <priority value="info"/> <appender-ref ref="bdfile"/> <appender-ref ref="console"/> </root> </log4j:configuration>
wmtdaxz33#
下面是一个将详细信息记录到单独文件中的示例,该示例用于每个月转出文件并删除超过180天的日志:
# property settings property.LOG_DIR = /opt/logs/app_name/ # Console appenders appender.console.type = Console appender.console.name = STDOUT appender.console.layout.type = PatternLayout appender.console.layout.pattern = %d{yy/MM/dd HH:mm:ss.SSS} %p %c{1}: %m%n # root loggers rootLogger.level = INFO rootLogger.appenderRef.stdout.ref = STDOUT rootLogger.appenderRefs = stdout rootLogger.appenderRef.files.ref = Rolling # Rolling file appender appender.rolling.type = RollingFile appender.rolling.name = Rolling appender.rolling.fileName= ${LOG_DIR}/application.log appender.rolling.filePattern= ${logFile}.%i appender.rolling.layout.type=PatternLayout appender.rolling.layout.pattern=%d{yy/MM/dd HH:mm:ss.SSS} %p %c{1}: %m%n appender.rolling.policies.type=Policies appender.rolling.policies.size.type = SizeBasedTriggeringPolicy appender.rolling.strategy.type = DefaultRolloverStrategy appender.rolling.policies.size.size=50MB appender.rolling.strategy.max=20 # custom logging for audit-viewer logger.aduit.name = auditlogger logger.aduit.level = info logger.aduit.appenderRefs = auditRef logger.aduit.appenderRef.auditRef.ref = AuditAppender logger.aduit.additivity = false # Logging audit details to dedicated file appender.aduitfile.type = RollingFile appender.aduitfile.fileName = ${AUDIT_FILE} appender.aduitfile.filePattern= ${LOG_DIR}/audit.%d{yyyy-MM}.log appender.aduitfile.name = AuditAppender appender.aduitfile.layout.type = PatternLayout appender.aduitfile.layout.pattern = %d{yy/MM/dd HH:mm:ss.SSS} %p %c{1}: %m%n appender.aduitfile.policies.type = Policies appender.aduitfile.policies.time.type =TimeBasedTriggeringPolicy appender.aduitfile.strategy.type = DefaultRolloverStrategy # Rollout files based on month appender.aduitfile.policies.time.interval=1 appender.aduitfile.policies.time.modulate= true appender.aduitfile.ImmediateFlush=true appender.aduitfile.append=true appender.aduitfile.strategy.action.type = Delete appender.aduitfile.strategy.action.basepath = ${LOG_DIR} appender.aduitfile.strategy.action.maxdepth = 1 appender.aduitfile.strategy.action.PathConditions.type = IfFileName # This below field uses relative path from basepath appender.aduitfile.strategy.action.PathConditions.glob = audit.*.log appender.aduitfile.strategy.action.condition.type = IfLastModified appender.aduitfile.strategy.action.condition.age = P180D # Settings to quiet third party or custom logs that are too verbose loggers = mongodb, aduit logger.mongodb.name=org.mongodb.driver.cluster logger.mongodb.level=ERROR
这里如果你想把日志从某个类放到单独的文件中,比如auditlogger,那么在一个类中使用它,如下所示:
auditlogger
private val logger = LogManager.getLogger("auditlogger") //dont change this appender ref logger.info("Find me inside 'audit.log' file")
3条答案
按热度按时间pxyaymoc1#
举个例子:
b0zn9rqh2#
下面是关于XML配置的答案,请注意,如果不给予文件附加器一个
ConversionPattern
,它将创建0字节文件,并且不写入任何内容:wmtdaxz33#
下面是一个将详细信息记录到单独文件中的示例,该示例用于每个月转出文件并删除超过180天的日志:
这里如果你想把日志从某个类放到单独的文件中,比如
auditlogger
,那么在一个类中使用它,如下所示: