Log4cxx custom appender

前端 未结 3 1995
梦谈多话
梦谈多话 2021-01-05 16:30

Is it possible to write a custom appender for log4cxx and have it configurable via a properties file (like the built-in appenders)? I\'d prefer doing this without having to

3条回答
  •  生来不讨喜
    2021-01-05 16:58

    brlcad's suggestion is correct - at my company we've used this method for our own custom appenders.

    You can in fact already configure these from a configuration file - the macros like DECLARE_LOG4CXX_OBJECT(CustomAppenderClassName) in the class definition set up the log4cxx environment. You can refer to your appender type by your "CustomAppenderClassName". A configfile (old-style) showing a standard and custom appender, using the standard layouts for the custom appender:

    # Set "realtime" logger level to DEBUG and create two appenders - one to be
    # a standard rolling file appender, the other custom.
    log4j.logger.realtime=ERROR, StandardAppender, CustomAppender
    
    # StandardAppenderis set to be a RollingFileAppender
    log4j.appender.StandardAppender=org.apache.log4j.RollingFileAppender
    log4j.appender.StandardAppender.File=example.log
    
    log4j.appender.StandardAppender.MaxFileSize=100KB
    # Keep one backup file
    log4j.appender.StandardAppender.MaxBackupIndex=1
    
    # StandardAppender uses PatternLayout.
    log4j.appender.StandardAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.StandardAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
    
    # CustomAppender uses PatternLayout too
    log4j.appender.CustomAppender=CustomAppenderClassName
    log4j.appender.CustomAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.CustomAppender.layout.ConversionPattern=%m
    

提交回复
热议问题