Linux 使用 log4j 将日志数据写入 syslog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13234727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:41:55  来源:igfitidea点击:

Writing log data to syslog using log4j

linuxlog4jsyslogrsyslog

提问by Sandeep Rao

I'm unable to write log messages into syslog. Any help would be great. Here is my simple log4j program

我无法将日志消息写入 syslog。任何帮助都会很棒。这是我的简单 log4j 程序

import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class log4jExample
{
  /* Get actual class name to be printed on */
  static Logger log = Logger.getLogger(log4jExample.class.getName());

  public static void main(String[] args) throws IOException,SQLException
  {

     log.error("Hello this is an error message");
     log.info("Hello this is an info message");
     log.fatal("Fatal error message");
  }
}

My syslog properties file

我的系统日志属性文件

# configure the root logger
log4j.rootLogger=INFO, SYSLOG


# configure Syslog facility LOCAL1 appender
log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.threshold=WARN
log4j.appender.SYSLOG.syslogHost=localhost
log4j.appender.SYSLOG.facility=LOCAL4
log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.conversionPattern=[%p] %c:%L - %m%n

采纳答案by Sandeep Rao

Add the following lines to rsyslog.conf file

将以下行添加到 rsyslog.conf 文件

$ModLoad imudp
$UDPServerRun 514

It worked for me.

它对我有用。

Need to restart the rsyslog after modfications.

修改后需要重启rsyslog。