SyslogAppender 不工作
Posted
技术标签:
【中文标题】SyslogAppender 不工作【英文标题】:SyslogAppender not working 【发布时间】:2012-09-19 07:29:01 【问题描述】:我有一个 java 程序,我想在 fedora 机器上登录 /var/log/messages 文件。我正在使用 log4j SyslogAppender,但它不起作用。
我的 log4j 属性文件包含
# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE, SYSLOG
#log4j.rootCategory=INFO, CONSOLE, LOGFILE
# Set the enterprise logger priority to DEBUG
log4j.logger.com.locaid=INFO, CONSOLE, LOGFILE, SYSLOG
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=/home/dev/app.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=[%ddd/MM/y HH:mm:ss][%t][%1p] %c - %m%n
log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost=localhost
log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.conversionPattern=%dISO8601 %-5p [%t] %c2 %x - %m%n
log4j.appender.SYSLOG.Facility=LOCAL1
log4j.appender.SYSLOG.Threshold=debug
log4j.appender.SYSLOG.FacilityPrinting=true
在/etc/rsyslog.conf
我有
local1.* /var/log/app.log
在/etc/sysconfig/rsyslog中有
SYSLOGD_OPTIONS="-r -m 0 -c 4"
在重新启动 rsyslog 服务时会创建 app.log 文件,但不会附加任何日志。我也尝试过使用默认 USER 工具,尽管 logger -p LOCAL1.info
cmd 正在工作并将日志附加到 app.log,但它无法正常工作。需要帮助。
【问题讨论】:
我猜你的 log4j.properties 文件中有更多行。你能显示整个文件吗?我认为我们需要看看您如何将 appender 与 logger 一起使用。 完整的 log4j 文件已更新... :) 我尝试了 facility.level 的不同组合,但不起作用。 【参考方案1】:我在 log4j 端看不到任何问题,但我的 /etc/default/rsyslog
(在 Ubuntu 上)说
# Options for rsyslogd
# -m 0 disables 'MARK' messages (deprecated, only used in compat mode < 3)
# -r enables logging from remote machines (deprecated, only used in compat mode < 3)
# -x disables DNS lookups on messages received with -r
# -c compatibility mode
# See rsyslogd(8) for more details
这表明-r
和-m 0
不能与-c 4
结合使用。与其尝试在此处设置远程访问,不如编辑您的 /etc/rsyslogd.conf
并添加(或取消注释)
$ModLoad imudp
$UDPServerRun 514
【讨论】:
logger
命令不使用 UDP 套接字机制与 rsyslogd 通信,它使用 Unix 套接字 /dev/log
。【参考方案2】:
#This Configuration File is used for Logger Module which is used for either using Log4J or SysLog4J
log4j.rootLogger = DEBUG,LOGFILE
#------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------
#[Log4j]
#------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------
# Log4j implements Rolling File Appender Configurations
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
# The log4j layout
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
# The Log4j configuration file location
log4j.appender.LOGFILE.File= C:/Documents and Settings/bgh28706/Desktop/log_output.cfg
# The Log4j conversion layout to apply for
log4j.appender.LOGFILE.layout.ConversionPattern=%dISO8601 %t %p %c %M %m %n
# The Log4j maximum file size to keep for before renaming to the backup file
log4j.appender.LOGFILE.MaxFileSize=5KB
# The maximum number of log4j files to be kept in the system
log4j.appender.LOGFILE.MaxBackupIndex=2
#Additivity set to False makes the output not to be produced in any other appender
log4j.additivity.LOGFILE.file=false
#------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------
#[Syslog]
#------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------
# The syslog appender to be configured for the syslog configuration to affect
log4j.appender.SYSLOGFILE=org.apache.log4j.net.SyslogAppender
# The hostname to log the syslogger information
log4j.appender.SYSLOGFILE.SyslogHost=localhost
# The facility name in the logger where the log file shall be logged
log4j.appender.SYSLOGFILE.facility=local5
# The log filename layout of the syslogger appender
log4j.appender.SYSLOGFILE.layout=org.apache.log4j.PatternLayout
# The syslogger configuration pattern
log4j.appender.SYSLOGFILE.layout.ConversionPattern=%dISO8601 %t %p %c %M %m %n
#Additivity set to False makes the output not to be produced in any other appender
log4j.additivity.SYSLOGFILE.file=false
看看这是否可以帮助你,因为它对我来说运行良好! 问候 阿南德·巴特
【讨论】:
【参考方案3】:问题来了
SYSLOGD_OPTIONS="-r -m 0 -c 4"
应该是
SYSLOGD_OPTIONS="-r -m 0"
这些选项不能一起使用
【讨论】:
以上是关于SyslogAppender 不工作的主要内容,如果未能解决你的问题,请参考以下文章
Java Log4j2 Syslog Appender 不发送消息
Mac 上的 Log4j2 Syslog Appender 格式