syslog

syslog(int $priority, string $message): true

向系统日志器发送消息(LOG_INFO 等优先级常量与 openlog 的 facility 组合);返回值恒 true,消息落点由 syslog.conf/journald 决定,CLI 下发送不崩。

C C23

C23
#include <stdio.h>
#include <syslog.h>

int main(void) {
    // POSIX syslog(3)——直接的原版;消息交给本地日志器
    openlog("codeabc", LOG_PID, LOG_USER);
    syslog(LOG_INFO, "demo info");
    syslog(LOG_WARNING, "demo warning");
    closelog();
    printf("info sent: yes\n");
    printf("warning sent: yes\n");
    return 0;
}
输出
info sent: yes
warning sent: yes

C++ C++26

C++26
#include <cstdio>
#include <syslog.h>

int main() {
    // C++ 无封装——直接用 POSIX <syslog.h> API
    openlog("codeabc", LOG_PID, LOG_USER);
    syslog(LOG_INFO, "demo info");
    syslog(LOG_WARNING, "demo warning");
    closelog();
    std::printf("info sent: yes\n");
    std::printf("warning sent: yes\n");
}
输出
info sent: yes
warning sent: yes

C# v11.0.100

v11.0.100
// BCL 无 syslog;经典线格式是发往 127.0.0.1:514 的 UDP 数据报(RFC 3164)。
// 默认没有监听者——UDP 发送照样成功(发完即弃)。
using System.Net.Sockets;
using System.Text;

byte[] info = Encoding.UTF8.GetBytes("<14>codeabc: demo info");
byte[] warn = Encoding.UTF8.GetBytes("<12>codeabc: demo warning");
using (UdpClient udp = new UdpClient()) {
    udp.Send(info, info.Length, "127.0.0.1", 514);
    udp.Send(warn, warn.Length, "127.0.0.1", 514);
}
Console.WriteLine("info sent: yes");
Console.WriteLine("warning sent: yes");
输出
info sent: yes
warning sent: yes

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"net"
)

func main() {
	// log/syslog 依赖本地 /dev/log(许多容器里没有);
	// 可移植兜底是 RFC 3164 UDP 数据报——发完即弃
	conn, err := net.Dial("udp", "127.0.0.1:514")
	if err != nil {
		fmt.Println("info sent: no")
		return
	}
	fmt.Fprint(conn, "<14>codeabc: demo info")
	fmt.Fprint(conn, "<12>codeabc: demo warning")
	conn.Close()
	fmt.Println("info sent: yes")
	fmt.Println("warning sent: yes")
}
输出
info sent: yes
warning sent: yes

Java v27.0.1

v27.0.1
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) throws Exception {
        // JDK 无 syslog API;RFC 3164 UDP 数据报是共通的线格式
        DatagramSocket s = new DatagramSocket();
        byte[] info = "<14>codeabc: demo info".getBytes(StandardCharsets.UTF_8);
        s.send(new DatagramPacket(info, info.length, new InetSocketAddress("127.0.0.1", 514)));
        byte[] warn = "<12>codeabc: demo warning".getBytes(StandardCharsets.UTF_8);
        s.send(new DatagramPacket(warn, warn.length, new InetSocketAddress("127.0.0.1", 514)));
        s.close();
        System.out.println("info sent: yes");
        System.out.println("warning sent: yes");
    }
}
输出
info sent: yes
warning sent: yes

JavaScript v26.13.1

v26.13.1
// Node 无 syslog 模块;线格式是 UDP 数据报(RFC 3164)
const dgram = require("node:dgram");
const s = dgram.createSocket("udp4");
s.send("<14>codeabc: demo info", 514, "127.0.0.1");
s.send("<12>codeabc: demo warning", 514, "127.0.0.1", () => {
    console.log("info sent: yes");
    console.log("warning sent: yes");
    s.close();
});
输出
info sent: yes
warning sent: yes

PHP v8.6.0

v8.6.0
// syslog() 把消息交给系统日志器;无条件返回 true,
// 消息落点由 syslog.conf/journald 决定
openlog("codeabc", LOG_PID, LOG_USER);
$ok1 = syslog(LOG_INFO, "demo info");
$ok2 = syslog(LOG_WARNING, "demo warning");
closelog();
echo "info sent: ", $ok1 ? "yes" : "no", "\n";
echo "warning sent: ", $ok2 ? "yes" : "no", "\n";
输出
info sent: yes
warning sent: yes

Python v3.15.2

v3.15.2
# Unix 专用的 syslog 模块直接封装 C API
import syslog

syslog.openlog("codeabc", syslog.LOG_PID)
syslog.syslog(syslog.LOG_INFO, "demo info")
syslog.syslog(syslog.LOG_WARNING, "demo warning")
syslog.closelog()
print("info sent: yes")
print("warning sent: yes")
输出
info sent: yes
warning sent: yes

Rust v1.99.1

v1.99.1
use std::net::UdpSocket;

fn main() {
    // 标准库无 syslog;RFC 3164 UDP 数据报是所有实现共通的线格式
    let s = UdpSocket::bind("0.0.0.0:0").unwrap();
    s.send_to(b"<14>codeabc: demo info", "127.0.0.1:514").unwrap();
    s.send_to(b"<12>codeabc: demo warning", "127.0.0.1:514").unwrap();
    println!("info sent: yes");
    println!("warning sent: yes");
}
输出
info sent: yes
warning sent: yes

TypeScript v26.13.1

v26.13.1
// Node 无 syslog 模块;线格式是 UDP 数据报(RFC 3164)
import * as dgram from "node:dgram";
const s: dgram.Socket = dgram.createSocket("udp4");
s.send("<14>codeabc: demo info", 514, "127.0.0.1");
s.send("<12>codeabc: demo warning", 514, "127.0.0.1", () => {
    console.log("info sent: yes");
    console.log("warning sent: yes");
    s.close();
});
输出
info sent: yes
warning sent: yes

语言差异说明

C/C++:POSIX syslog(3) 原版,openlog/closelog 配对的 ident 与 facility 会污染同线程后续日志(Apache 场景知名坑)。

C#:BCL 无 syslog;EventLog 是 Windows 事件日志(跨平台不可用),跨平台方案就是 RFC 3164 UDP 数据报。

Go:log/syslog 依赖 /dev/log,容器里常缺席;UDP 兜底永远成功(发完即弃,无确认)。

Java:JDK 无此 API;日志生态(SLF4J/Logback 的 SyslogAppender)或裸 UDP。

JavaScript:无内置;dgram 十几行即发。

PHP:syslog() 与 error_log($msg, 0)/error_log($msg, LOG_ERR, ...) 不同——第三参为 LOG_ERR|LOG_USER 这类组合即路由到 syslog(error_log 条目呼应);返回值恒 true,不表示已落盘。

Python:syslog 模块 Unix 专用;跨平台用 logging.handlers.SysLogHandler。

Rust:标准库无;syslog crate 或裸 UDP。

TypeScript:同 JavaScript。

线格式:<优先级>ident: 消息 的 BSD syslog(RFC 3164),UDP 514 或 /dev/log 本机套接字。