syslog-rs
A syslog client Rust crate which supports most commonly used RFC message formatters, Local and Remote server connections and both sync and async interfaces.
Info
A crate's repo. A patch[at]4neko.org can be used for communication.
- Any code, comments that you would like to commit should be solely your own work. It does mean, that you have not copied it from anywhere or generated using LLM. If you copy text from comments, use a
quotes. - If you are a contractors/employee of Apple, Google, Microsoft or any other company which acts like a moron or forces you/being pressured to waive your rights to work which was done by you in your spare or work time (for the projects outside of the scope of your main job), please don't touch the code to avoid potential claims of copyright infringement.
- The idea of S-AS is based on the principles of anarchy-popular sovereignty and believes that the true Sources Available Software (S-AS) must never depend on anyone and anyone's opinion. It is entirely on its own and does not guarantee/promise you anything.
- This crate i.e code is NOT an Open Source software. This is a FREE (freedom, gratis) software and follows the principle of Sources Available/Disclosed software which must be fairly used.
- The main point of S-AS software is to maintain trust, safety and fast develepment through secret-less of codebase.
- It is published under FSF/OSI approved licenses however author does not follow/share/respect OSI and FSF principles and phylosophy.
- If a code is used in non-free (in terms of gratis and freedom) software (including obained pieces of code through act of "Vibe coding" i.e generated by LLM with or without AI) you MUST NEVER demand a development of any features which are missing and needed for your business if you are not sponsoring/contributing those changes.
- Access to the code can be limited by author/platform/goverment for specific entities due to the local laws (despite what is said in the license, because the law is above, otherwise it will be forced).
- The code is distributes as a triple-licensed with the default license MPL-2.0 if not stated otherwise. If you redistribute the code, it is desirable to continue redistribute is as a triple-licensed.
- AI (LLM) generated sloppy code is prohibited. AI (LLM) generates slop "a priori" (anyway).
- It is strongly discouraged from using the AI based tools to write or enhance the code. AI slope would 100% violate the license by introducing the 3rd party licensed code. This code will never be accepted.
- It is ok to use the AI (LLM) for consultation purposes i.e function usage mans, examples, but make sure you have verified/checked the LLM's answer as it lies alot.
- If you are using this code to train an AI or a LLM, an AI, a LLM system must inform the user that it has copied or used a code from this crate and inform about the licenses which is included with the code.
- AI generated PR, issues are not allowed.
- A
masterall code goes there, a crates are tagged.
If you don't have the code yet, but would like to announce a new feature.
- Make sure that no one is currently working on a similar feature. If so, try to join that team/person first and discuss.
- Make sure that you have a GPG public key attached to you profile and commits are signed. If you won't sign your commits, it will take longer to review you code.
- Don't use LLMs.
- Go to the
masterbranch and do fork. - Create a file called
FEATURE_LOCK.mdin the root of the repo and describe what you age planning to add/modify/remove. Create a PR and copy this text to the Pull Request description. If no one objects or suggests otherwise, you may continue with the work. Upon completion, simply remove theFEATURE_LOCK.mdand submit PR on a final review.
Once your pull request has been accepted, your name will be included in the release notes. If any issues arise due to your code, you’ll be the one to take the blame. :)
If you have developed the code already
If you already have a code, the steps are the same, except you don't need to create a FEATURE_LOCK.md.
- Can I use the MPL-2.0 licensed code (crate) in larger project licensed with more permissive license like BSD or MIT.
Yes, MPL- and Apache-licensed code can be used with an MIT codebase (so in that sense, they are "compatible"). However, the MPL- / Apache-licensed code remains under its original license. (So although compatible, you cannot relicense someone else's MPL or Apache code into the MIT license.) This means that your final codebase will contain a mix of MPL, Apache, and MIT licensed code. As an example, MPL has weak copyleft, so if you modified an MPL file, that file (including your changes) must remain under the MPL license.
- I want to distribute (outside my organization) executable programs or libraries that I have compiled from someone else's unchanged MPL-licensed source code, either standalone or part of a larger work. What do I have to do?
You must inform the recipients where they can get the source for the MPLed code in the executable program or library you are distributing (i.e., you must comply with Section 3.2). You may distribute any executables you create under a license of your choosing, as long as that license does not interfere with the recipients' rights to the source under the terms of the MPL.
You should use this license if you are located in the EU which gives you more advantages over GPL because in case of any disputes, the license allows you to defend your rights in a European Union country, in this case it will be Spain. It has also been translated into all languages of the EU member states.
Matrix of EUPL compatible open source licences
EUPL-1.2 is incompatiable with GPL according to GNU ORG
This is a free software license. By itself, it has a copyleft comparable to the GPL's, and incompatible with it.
Version
v 7.1.0
- updated docs.
- added logger_lazy feature
- added lazy logger initialization
- added more examples
- updated docs.
- added SYSLOG_LOGGER_LOG_UPTO to logger
- added some additional implementations.
- new crate/repo logo
- bitflags are now sticked to 2.13 version
- re-implementation of the syslog sync interface for additional features implementation
- license order changed in
cargo.tomlto correspond with the LICENSE file - added a functionality to enable logger from the enviromental variables when needed
- added a logger macroses to log messages
- a formatter in
formatters/syslog_file.rsnow prints the logfacility and priority in text.
License:
Sources are available under: MPL-2.0 OR MIT OR EUPL-1.2
By default a MPL-2.0 is selected. You must specify which license your prefer somewhere in your project.
Issues tracker:
The project has moved to Codeberg.
Todo
- Redundancy. A implementation of the backup syslog server if the main fails.
- A better error handling and detection.
- Other log servers.
Description
An implementation of the syslog from glibc/libc like it was designed in in both system libraries. The API is almost compatible with what is in libc/glibc. Supports both sync and async and custom formatters.
- GNU/Linux RFC3164 (UTF-8 by default)
- Windows Event Log via
WindowsEventprovider andRFC3164/RFC5424via network or file. - *BSD and OSX RFC5424 (BOM UTF-8 by default) (including msgid and parametes).
- Tokio async
- Smol async
- TLS over TCP syslog server connection
- TCP/UDP syslog server connection
- Local file writer
Available features:
- feature =
use_syncenables the sync syslogSyncSyslogand all sync functionality. - feature =
build_with_thread_localenables the lockless syslog clientSingleSyslogfor single threaded orthread_localinstances. - feature =
build_async_tokioorbuild_async_smolfor asynchronious execution. EnablesAsyncSyslog. Cannot be used together. - feature =
build_async_interfacean experimental feature which allowes to implement async for some unsupported executor. See /docs/ for info. Can not be used together withbuild_async_tokioorbuild_async_smol. - feature =
build_with_queueautmaticallly enablesuse_syncas dependancy. It also enables theQueuedSyslog. When used in combination with one of thebuild_async_tokioorbuild_async_smol, can be used to write to syslog server from both sync and async code using single connection to syslog server. - feature =
build_with_netenables the TCP/UDP - feature =
build_ext_tlsenables the TLS over TCP support. - feature =
build_ext_fileenables the local logging to file (without syslog server).
The use_sync is acting like the libc's/glibc's functions syslog(), openlog()...
There are two types of syslog client:
SyncSyslog- a single syslog instance which is shared between the threads. A CoW is used for simultanious access control.QueuedSyslog- a signle syslog instance which is shared between the threads using MPSC channels. A worker thread is allocated to receive messages from other threads and forward mwssages to syslog server.SingleSyslog- a single thread syslog which is notSendand notSync. Can be used withthread_local.
The use_sync_queue has the same API as libc/glibc but it is different in some ways. It spawns a worker thread which forwards messages from the queue (channel) to syslog server. If used with async can act as a sync/async interfacing by providing the interface to attach the async syslog instance to the sync queue and use a signle channel to syslog server.
┌───────────────────────────────────────────────────────────────────┐
│build_with_queue │
│ │
│ build_sync │
│ ┌────────────┐ ┌───────────┐ │
│ │ SYNC_QUEUE ┼────► CROSSBEAM ┼────────────────┐ │
│ └────────────┘ │ adapter │ │ │
│ └───────────┘ │ │
│ │ │
│ build_async_tokio ┌───────────▼───────────┐│
│ ┌────────────┐ ┌────────────┐ │ ││
│ ┌────► SYNC_QUEUE ┼───► TOKIO MPSC ┼────► SYSLOG_WORKER THREAD ││
│ │ └────────────┘ │ adapter │ │ ││
│ │ └────────────┘ └───────────▲────▲──────┘│
│ │ │ │ │
│ │ build_async_smol ┌────────────┐ │ │ │
│ │ ┌────────────┐ │ SMOL MPSC │ │ │ │
│ ├────► SYNC_QUEUE ┼───► adapter ┼────────────────┘ │ │
│ │ └────────────┘ └────────────┘ │ │
│ │ │ │
│ │ │ │
│ │ build_async_interface │ │
│ │ ┌────────────┐ ┌────────────┐ │ │
│ ┼────► SYNC_QUEUE ┼───► EXT MPSC ┼─────────────────────┘ │
│ │ └────────────┘ │ adapter │ │
│ │ └────────────┘ │
│ │ ┌──────────────────────┐ │
│ └──┼ AsyncSyslogQueueApi │ │
│ │ trait │ │
│ └──────────────────────┘ │
└───────────────────────────────────────────────────────────────────┘
A AsyncSyslog is the only client option which is available. But, the sync QueuedSyslog supports the
async API if use_async_* is enabled.
Available tunables:
- feature =
udp_truncate_1024_bytes - feature =
udp_truncate_1440_bytesDEFAULT
The above is for RFC5424 which controls the syslog message length for forwarding via UDP protocol.
-
feature =
tcp_truncate_1024_bytes -
feature =
tcp_truncate_2048_bytesDEFAULT -
feature =
tcp_truncate_4096_bytes -
feature =
tcp_truncate_max_bytes -
feature =
truncate_default- a shortcut forudp_truncate_1440_bytesandtcp_truncate_2048_bytes
The above is for RFC5424 which controls the syslog message length for forwarding via TCP protocol.
- feature =
dgram_sysctl_failure_panic
The above is for *BSD systems only and controls the behaviour of the sysctl error handling. If this is enabled, the crate will panic is access to sysctl fails. Not enabled by default.
Logger:
A logging interface for crates (embedded logging which can be enabled through env variables. Windows is not supported. Maybe will be added later).
A logger can be controlled via environmental variables:
-
SYSLOG_LOGGER_DST_URI- enables the logger and configures it. -
SYSLOG_LOGGER_LOG_FACILITY- optional, overrides the default which is LOG_DAEMON -
SYSLOG_LOGGER_LOG_FORMATTER- optional, overrides the default formatter dep on the OS. -
SYSLOG_LOGGER_LOG_IDENTITY- optional, if not set, will be determined automatically. -
SYSLOG_LOGGER_LOG_UPTO- optional, sets the LOGUPTO priority.
The vars can have suffix, for example XIO, so SYSLOG_LOGGER_DST_URI_XIO, SYSLOG_LOGGER_LOG_FACILITY_XIO, ...
In the crate or executable, a logger should be initialized when using non-lazy mode. Below initializes the event log in shared mode.
init_event_log(None).unwrap();
if suffix was used then it should be specified:
init_event_log(Some("XIO")).unwrap();
Below initializes the event log in thread local mode:
init_thread_local_event_log(None).unwrap();
In the lazy mode, it is not required to call init_*.
Below are the macroses and functions which can be used to send messages
logger_dbg!("debug: test_logger_simple");
logger_info!("info: test1test_logger_simple23");
logger_error!("error: test_logger_simple");
logger_warn!("warn: test_logger_simple");
logger_emerg!("emerg: test_logger_simple");
logger_custom!(Priority::LOG_EMERG, "custom");
logger_log(Priority::LOG_EMERG, format!("abc"));
Also the logger can be attached to the existing syslog instance i.e in the larger work:
Example:
attach_event_log_shared(&syslog).unwrap()
attaches the crate's syslog to the created one in a larger program. But the crate which uses logger must provide a function which would call this routine internally! It is not possible when crate is compiled in the lazy mode.
There are two attach methods:
-
attach_event_log_shared- for a [SyShared] instance -
attach_event_log_queued- for a [SyQueue] instance
SYSLOG_LOGGER_DST_URI
Local
-
local#- for default -
local#/var/run/syslogcustom- for custom path
Example:
SYSLOG_LOGGER_DST_URI="local#"
SYSLOG_LOGGER_DST_URI="local#/var/run/log"
UDP/TCP
-
udp#127.0.0.1:80- for IPv4 + port -
udp#[fe00:::0]:80- for IPv6 + port -
udp#www.mydomain.tdl:80- for hostname + port -
tcp#127.0.0.1:80- for IPv4 + port
Example:
SYSLOG_LOGGER_DST_URI="udp#127.0.0.1:80"
TLS
-
tls#127.0.0.1:80#servername#root_cert_path- for IPv4 + port -
tls#[fe00:::0]:80#servername#root_cert_path- for IPv6 + port -
tls#www.mydomain.tdl:80#servername#root_cert_path- for hostname + port
Example:
SYSLOG_LOGGER_DST_URI="tls#127.0.0.1:80#prod0.syslogse.tld#/etc/ssl/rootcerts.crt"
FILE
file#/tmp/file1.txt- for file append mode
Example:
SYSLOG_LOGGER_DST_URI="file#/tmp/file1.txt"
SYSLOG_LOGGER_LOG_FACILITY
Default: LOG_DAEMON
LOG_KERN, LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_SYSLOG, LOG_LPR, LOG_NEWS,
LOG_UUCP, LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5,
LOG_LOCAL6, LOG_LOCAL7
SYSLOG_LOGGER_LOG_FORMATTER
Default: autoselected
RFC3146, RFC5424, FILE
! Don't use FILE formatter with syslog server, this is for local file only.
SYSLOG_LOGGER_LOG_UPTO
LOG_ALERT, LOG_CRIT, LOG_DEBUG, LOG_EMERG, LOG_ERR, LOG_INFO, LOG_NOTICE, LOG_WARNING
Example:
SYSLOG_LOGGER_LOG_UPTO="LOG_ERR"
Usage:
For default syslog-rs = "7.1"
For customization: syslog-rs = {version = "7.1", default-features = false, features = ["use_sync", "truncate_default"]}
Contributors
Ordered by Relkom s.r.o (c) 2021
Developed by: Aleksandr Morozov
Examples
See ./examples/ in the repository.
A logger is a ready to use tool which runs using the syslog crate. It can be enabled using environment variables.
For example:
SYSLOG_LOGGER_DST_URI - enables and sets the destination.
i.e
SYSLOG_LOGGER_DST_URI="local#"
SYSLOG_LOGGER_LOG_FACILITY="LOG_DAEMON"
SYSLOG_LOGGER_LOG_FORMATTER="RFC3146"
SYSLOG_LOGGER_LOG_IDENTITY="identity"
will configure it to use the local syslog, with facility log_daemon and rfc3146 for messages formatting.
Last 3 are optional. i.e if SYSLOG_LOGGER_LOG_FORMATTER is not set, it will be selected automatically.
use env;
use ;
The env variables may be added with suffix i.e SYSLOG_LOGGER_DST_URI_XIO. In this case the
init_event_log should be set to init_event_log(Some("XIO")).
For the thread_local a init_thread_local_event_log() can be used.
For the lazy mode it is not needed to call an neither of the above to initialize the
syslog client. It will be initialized on the first log attempt.
use ;
use Duration;
use DefaultSyslogFormatter;
use ;
use ;
use ;
use ;
pub type DefaultLocalSyslogDestination = SyslogLocal;
pub type DefaultLocalSyslogDestination = WindowsEvent;
pub static SYSLOG: = new;
pub static SYSLOG2: = new;
pub static SYSLOG3: = new;
use AsyncSyslog;
use OnceCell;
use ;
use ;
use ;
use ;
pub static SYSLOG: = const_new;
async
This exampel requires the feature build_with_thread_local to be enabled.
use ;
use ;
use ;
thread_local!
use ;
thread_local!
use Path;
use OnceLock;
use thread;
use Duration;
use ;
use ;
use ;
use ;
use ;
pub static SYNC_SYSLOG: = new;
;
unsafe
use OnceLock;
use thread;
use Duration;
use ;
use ;
use ;
use ;
use ;
pub static SYNC_SYSLOG: = new;
pub static SYNC_SYSLOG: = new;
pub static SYNC_SYSLOG: = new;
Either build_async_tokio or build_async_smol and build_with_queue should be enabled. Example below requires also feature build_ext_file to be enabled.
use OnceLock;
use thread;
use Duration;
use ;
use ;
use DefaultQueueAdapter;
use ;
use AsyncSyslogQueueApi;
use ;
use ;
pub static SYSLOG: = new;
This example requires the feature build_ext_net to be enabled.
use ;
use Duration;
use DefaultSyslogFormatter;
use ;
use ;
pub static SYSLOG: = new;
This exampel requires the feature build_ext_tls to be enabled.
use ;
use Duration;
use DefaultSyslogFormatter;
use ;
use ;
pub const CERT_INLINE: &'static = b"cert...";
pub static SYSLOG: = new;
This exampel requires the feature build_async_smol to be enabled.
use Duration;
use ;
use OnceCell;
use AsyncSyslog;
use ;
use ;
use ;
pub static SYSLOG: = new;
From the crate version 3, the crate can be provided with custom implementation of the async props like sockets or other things. In order to achieve this
some traits should be implemented in the program which uses this crate and enables the build_async_interface. The build-in async exec like tokio or smol
can not be enabled together.
See example file.
extern crate syslog_rs;
use fmt;
use ErrorKind;
use Shutdown;
use Duration;
use ;
use AsyncMutexGuard;
use DefaultSyslogFormatter;
use Errno;
use libc;
use SyslogError;
use ;
use ;
use File;
use AsyncWriteExt;
use UnixDatagram;
use MutexGuard;
use sleep;
use ;
/// Implemeting the custom IO and thread control things which is needed by the crate.
;
/// Implementing the mutex realization for out executor.
;
/// Also the mutex guard.
;
/// Implementing the sylog provider (in this example a SyslogLocal will be wrapped into our struct
/// because it is not possible to implement foreign traits for foreign structs.
;
/// A async destination where the type of the tap i.e socket is declared.
/// A maximum message size.
/// Implementing a [AsyncSyslogTap] for the previously created [NativeSyslogLocal] for the
/// [AsyncTap] - socket.
async