syslog
syslog(int $priority, string $message): true
Sends a message to the system logger (LOG_INFO-style priority constants combined with the openlog facility); always returns true, the destination is up to syslog.conf/journald, and sending never crashes under CLI.
C C23
#include <stdio.h>
#include <syslog.h>
int main(void) {
// POSIX syslog(3) - the direct original; the message goes to the local logger
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
#include <cstdio>
#include <syslog.h>
int main() {
// No C++ wrapper - the POSIX <syslog.h> API is used directly
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
// No BCL syslog; the classic wire format is a UDP datagram to 127.0.0.1:514 (RFC 3164).
// Nothing listens there by default - UDP send still succeeds (fire-and-forget).
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
package main
import (
"fmt"
"net"
)
func main() {
// log/syslog needs a local /dev/log (absent in many containers);
// the portable fallback is the RFC 3164 UDP datagram - fire-and-forget
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
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 {
// The JDK has no syslog API; the RFC 3164 UDP datagram is the shared wire format
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
// Node has no syslog module; the wire format is a UDP datagram (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
// syslog() hands the message to the OS logger; it returns true unconditionally,
// and the message lands wherever syslog.conf/journald routes it
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
# The Unix-only syslog module wraps the C API directly
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
use std::net::UdpSocket;
fn main() {
// No std syslog; the RFC 3164 UDP datagram is the wire format everyone shares
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
// Node has no syslog module; the wire format is a UDP datagram (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
Language differences
C/C++: the POSIX syslog(3) original; the openlog/closelog ident and facility can pollute later logs from the same thread (a well-known Apache pitfall).
C#: no BCL syslog; EventLog is the Windows event log (not cross-platform) - the cross-platform route is the RFC 3164 UDP datagram.
Go: log/syslog needs /dev/log, often absent in containers; the UDP fallback always succeeds (fire-and-forget, no ack).
Java: nothing in the JDK; the logging ecosystem (SLF4J/Logback SyslogAppender) or bare UDP.
JavaScript: nothing built in - a dozen lines with dgram.
PHP: syslog() differs from error_log($msg, 0) - pass LOG_ERR|LOG_USER-style combinations as error_log's third argument to route into syslog (echoes the error_log entry); the return value is always true and does not mean the message hit disk.
Python: the syslog module is Unix-only; use logging.handlers.SysLogHandler for cross-platform code.
Rust: nothing in std; the syslog crate or bare UDP.
TypeScript: same as JavaScript.
Wire format: BSD syslog (RFC 3164) "<priority>ident: message" over UDP 514 or the /dev/log local socket.