发布于2026-07-01 阅读(0)
扫一扫,手机访问

package main
import (
"log"
"os"
)
func main() {
// 日志输出到标准输出
log.SetOutput(os.Stdout)
log.Println("This is an info message")
// 或者写到文件
file, err := os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
log.SetOutput(file)
defer file.Close()
}
log.Println("This is another info message")
}
对,就这么简单,日志源就准备好了。
sudo yum install rsyslog
装好后,编辑配置文件(通常是 `/etc/rsyslog.conf` 或 `/etc/rsyslog.d/50-default.conf`),打开远程接收能力。下面这段关键是加载 UDP 或 TCP 模块,并指定一个规则把日志转发走:
# 加载imudp模块
module(load="imudp")
input(type="imudp" port="514")
# 或者加载imtcp模块
module(load="imtcp")
input(type="imtcp" port="514")
# 规则集定义
ruleset(name="remote") {
action(type="omfwd" Target="your.remote.log.server" Port="514" Protocol="udp")
}
# 应用规则集
if $fromhost-ip == 'your.local.ip' then {
call remote
}
& stop
记得把 `your.remote.log.server` 换成你远程日志服务器的真实 IP。
sudo systemctl restart rsyslog
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8