在Golang中,你可以使用标准库中的"log"包来记录日志。但是,如果你想要记录到CentOS的系统日志中,你需要使用第三方库,比如"go-syslog"。以下是一个简单的示例,展示了如何使用"go-syslog"库将日志记录到CentOS的系统日志中:

go get github.com/RackSec/srs-lib-go/logmain.go的文件,并添加以下代码:package mainimport ("github.com/RackSec/srs-lib-go/log")func main() {// 设置日志参数log.SetLogger("myapp", log.LOG_INFO, log.LOG_DAEMON)// 记录日志log.Info("This is an info message")log.Warn("This is a warning message")log.Error("This is an error message")}在这个示例中,我们设置了日志记录器的名称(“myapp”),日志级别(LOG_INFO)和日志设施(LOG_DAEMON)。然后,我们使用log.Info、log.Warn和log.Error函数记录不同级别的日志。
go build main.go./main这将在CentOS的系统日志中生成以下条目:
Oct1 00:00:00 myapp daemon.info This is an info messageOct1 00:00:00 myapp daemon.warning This is a warning messageOct1 00:00:00 myapp daemon.error This is an error message注意:你可能需要使用sudo权限运行程序,以便将日志写入系统日志。