Perl:将日志发送给syslog

| 暂无评论 | 暂无引用通告

--> 使用模块Sys::Syslog

将日志发送给本机

#!/usr/bin/perl -w
use strict;

use Sys::Syslog qw(:DEFAULT setlogsock);

my $ident="inotify";  #进程命
my $logopt="ndelay";
my $facility="local2";  #设备名
my $priority="info";    #日志级别
openlog $ident, $logopt, $facility;       
syslog($priority,"hello"); #记入日志
closelog;


将日志发送到日志主机

#!/usr/bin/perl -w
use strict;

use Sys::Syslog qw(:DEFAULT setlogsock);

my $ident="inotify";  #进程命
my $logopt="ndelay";
my $facility="local2";  #设备名
my $priority="info";    #日志级别
setlogsock('udp','log.supersun.biz');
syslog($priority,"hello"); #记入日志
closelog;

如果使用tcp协议传输日志:
setlogsock('tcp','log.supersun.biz');
同时,更改/etc/services
将514/tcp的服务名改为syslog

log option的几个可设定的值

    * cons - This option is ignored, since the failover mechanism will drop down to the console automatically if all other media fail.
    * ndelay - Open the connection immediately (normally, the connection is opened when the first message is logged).
    * nofatal - When set to true, openlog() and syslog() will only emit warnings instead of dying if the connection to the syslog can't be established.
    * nowait - Don't wait for child processes that may have been created while logging the message. (The GNU C library does not create a child process, so this option has no effect on Linux.)
    * perror - Write the message to standard error output as well to the system log.
    * pid - Include PID with each message.

syslog记入的日志格式

Mar 18 13:17:13 host1.supersun.biz inotify: test message
时间  接收到日志的时间
主机名  发送日志的主机
进程名 前面定义的$ident
日志内容

相关文章

使用syslog-ng配置日志主机
将防火墙的日志发送给日志主机

暂无引用通告

发送引用通告网址: http://supersun.info/mt/mt-tb.cgi/925
如果您想引用这篇日记到您的Blog,请复制上面的链接,放置到您发表文章时的相应界面中。

发表评论

最新资源

  • IMG_1437.JPG
  • line.png
  • bar.png
  • perl_calander.jpg

关于此日记

此日记由 supersun 发表于 2009年3月18日 14:26

此Blog上的上一篇日记使用syslog-ng配置日志主机

此Blog上的下一篇日记inotify监控文件的更改

首页归档页可以看到最新的日记和所有日记。