---
title: "用syslog-ng建立日志中心"
author: "jedy"
date: "2005-12-02T09:09:00Z"
tags: []
slug: "syslog-ng"
draft: false
lastmod: "2005-12-02T09:09:00Z"
id: 304763
url: "https://jedy.iteye.com/blog/304763"
---

使用syslog可以把本地的log记录发送给log server。  
在syslog的配置文件(/etc/syslog.conf)中加入

```
mail.*     @log_server_ip
```

相同的facility可以有多条配置，这样可以在发往server的同时在本地也保存一份。  
如果也要用此记录apache的log，那么可以在apache中配置

```
CustomLog " /usr/bin/logger -p user.info" combined
```

然后再syslog中加入配置

```
user.info @log_server_ip
```

log server用syslog接受时无法把各机器发送过来的log分开，所以在log server用的是  
syslog-ng，当然client也可以用syslog-ng，只不过现在client太多，要改过于麻烦。

从http://www.balabit.com/downloads/syslog-ng/stable/src/下载安装最新的syslog-ng，  
安装很简单

```
./congifure &&amp; make && make install
```

解开的目录下有一个contrib目录，其中有一些配置文件的样本和启动脚本，可以选用符合自己  
系统的。  
我用的是Redhat

```
cp init.d.Redhat /etc/init.d/syslog-ng
```

把

```

# chkconfig: 2345 12 88
# description: syslog-ng is the next generation of the syslog daemon. # syslog-ng gives you the flexibility of logging not only by facility and # severity, but also by host, message content, date, etc. it can also replace # klogd's function of logging kernel messages
```

加到script中

```

chkconfig --add syslog-ng
chkconfig --level 345 syslog-ng on
```

配置文件中关于client的部分为

```

source s_remote {
        tcp(ip(0.0.0.0) port(514));
        udp(ip(0.0.0.0) port(514));
};

destination d_separatedbyhosts {
        file("/var/log/syslog-ng/$HOST/$FACILITY.log" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes));
};

log { source(s_remote); destination(d_separatedbyhosts); };
```