Linux:syslog()的使用和示例

man手冊

命令行man openlog即可查看;寫的非常詳細,看完其實就懂了。

NAME
? ? ? ?closelog, openlog, syslog, vsyslog - send messages to the system logger

SYNOPSIS
? ? ? ?#include <syslog.h>

? ? ? ?void openlog(const char *ident, int option, int facility);
? ? ? ?void syslog(int priority, const char *format, ...);
? ? ? ?void closelog(void);

? ? ? ?void vsyslog(int priority, const char *format, va_list ap);

? ?Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

? ? ? ?vsyslog():
? ? ? ? ? ?Since glibc 2.19:
? ? ? ? ? ? ? ?_DEFAULT_SOURCE
? ? ? ? ? ?Glibc 2.19 and earlier:
? ? ? ? ? ? ? ?_BSD_SOURCE

DESCRIPTION
? ?openlog()
? ? ? ?openlog() opens a connection to the system logger for a program.

? ? ? ?The ?string ?pointed ?to by ident is prepended to every message, and is
? ? ? ?typically set to the program name. ?If ident is NULL, the program ?name
? ? ? ?is ?used. ? (POSIX.1-2008 ?does ?not specify the behavior when ident is
? ? ? ?NULL.)

? ? ? ?The option argument specifies flags ?which ?control ?the ?operation ?of
? ? ? ?openlog() ?and ?subsequent ?calls ?to ?syslog(). ?The facility argument
? ? ? ?establishes a default to be used if none ?is ?specified ?in ?subsequent
? ? ? ?calls ?to ?syslog(). ? The ?values that may be specified for option and
? ? ? ?facility are described below.

? ? ? ?The use of openlog() is optional; it will automatically ?be ?called ?by
? ? ? ?syslog() if necessary, in which case ident will default to NULL.

? ?syslog() and vsyslog()
? ? ? ?syslog() ?generates ?a ?log ?message, which will be distributed by sys‐
? ? ? ?logd(8).

? ? ? ?The priority argument is formed by ORing together a facility value ?and
? ? ? ?a ?level ?value ?(described ?below). ?If no facility value is ORed into
? ? ? ?priority, then the default value set by openlog() is used, or, if there
? ? ? ?was no preceding openlog() call, a default of LOG_USER is employed.

? ? ? ?The ?remaining ?arguments ?are a format, as in printf(3), and any argu‐
? ? ? ?ments required by the format, except that the two-character sequence %m
? ? ? ?will be replaced by the error message string strerror(errno). ?The for‐
? ? ? ?mat string need not include a terminating newline character.

? ? ? ?The function vsyslog() performs the same task as syslog() with the dif‐
? ? ? ?ference that it takes a set of arguments which have been obtained using
? ? ? ?the stdarg(3) variable argument list macros.

? ?closelog()
? ? ? ?closelog() closes the file descriptor being used to write to the system
? ? ? ?logger. ?The use of closelog() is optional.
? ?Values for option
? ? ? ?The ?option ?argument ?to ?openlog() is a bit mask constructed by ORing
? ? ? ?together any of the following values:

? ? ? ?LOG_CONS ? ? ? Write directly to the system ?console ?if ?there ?is ?an
? ? ? ? ? ? ? ? ? ? ? error while sending to the system logger.

? ? ? ?LOG_NDELAY ? ? Open ?the ?connection immediately (normally, the connec‐
? ? ? ? ? ? ? ? ? ? ? tion is opened when the first message is logged). ? This
? ? ? ? ? ? ? ? ? ? ? may ?be ?useful, ?for example, if a subsequent chroot(2)
? ? ? ? ? ? ? ? ? ? ? would make the pathname used internally by ?the ?logging
? ? ? ? ? ? ? ? ? ? ? facility unreachable.

? ? ? ?LOG_NOWAIT ? ? Do not ?wait ?for child processes that may have been cre‐
? ? ? ? ? ? ? ? ? ? ? ated while logging the message. ?(The GNU C library does
? ? ? ? ? ? ? ? ? ? ? not create a child process, so this option has no effect
? ? ? ? ? ? ? ? ? ? ? on Linux.)

? ? ? ?LOG_ODELAY ? ? The converse of LOG_NDELAY; opening of the connection is
? ? ? ? ? ? ? ? ? ? ? delayed until syslog() is called. ?(This is the default,
? ? ? ? ? ? ? ? ? ? ? and need not be specified.)

? ? ? ?LOG_PERROR ? ? (Not in POSIX.1-2001 or ?POSIX.1-2008.) ? Also ?log ?the
? ? ? ? ? ? ? ? ? ? ? message to stderr.

? ? ? ?LOG_PID ? ? ? ?Include the caller s PID with each message.

? ?Values for facility
? ? ? ?The ?facility ?argument is used to specify what type of program is log‐
? ? ? ?ging the message. ?This lets the configuration file specify ?that ?mes‐
? ? ? ?sages from different facilities will be handled differently.

? ? ? ?LOG_AUTH ? ? ? security/authorization messages

? ? ? ?LOG_AUTHPRIV ? security/authorization messages (private)

? ? ? ?LOG_CRON ? ? ? clock daemon (cron and at)

? ? ? ?LOG_DAEMON ? ? system daemons without separate facility value

? ? ? ?LOG_FTP ? ? ? ?ftp daemon

? ? ? ?LOG_KERN ? ? ? kernel messages (these can't be generated from user pro‐
? ? ? ? ? ? ? ? ? ? ? cesses)

? ? ? ?LOG_LOCAL0 through LOG_LOCAL7
? ? ? ? ? ? ? ? ? ? ? reserved for local use

? ? ? ?LOG_LPR ? ? ? ?line printer subsystem

? ? ? ?LOG_MAIL ? ? ? mail subsystem

? ? ? ?LOG_NEWS ? ? ? USENET news subsystem

? ? ? ?LOG_SYSLOG ? ? messages generated internally by syslogd(8)

? ? ? ?LOG_USER (default)
? ? ? ? ? ? ? ? ? ? ? generic user-level messages

? ? ? ?LOG_UUCP ? ? ? UUCP subsystem

Values for level
? ? ? ?This determines the importance of the ?message. ? The ?levels ?are, ?in
? ? ? ?order of decreasing importance:

? ? ? ?LOG_EMERG ? ? ?system is unusable

? ? ? ?LOG_ALERT ? ? ?action must be taken immediately

? ? ? ?LOG_CRIT ? ? ? critical conditions

? ? ? ?LOG_ERR ? ? ? ?error conditions

? ? ? ?LOG_WARNING ? ?warning conditions

? ? ? ?LOG_NOTICE ? ? normal, but significant, condition

? ? ? ?LOG_INFO ? ? ? informational message

? ? ? ?LOG_DEBUG ? ? ?debug-level message

? ? ? ?The function setlogmask(3) can be used to restrict logging to specified
? ? ? ?levels only.

示例

#include <unistd.h>
#include <iostream>
#include <syslog.h>
using namespace std;
int main(int argc, char const *argv[])
{
? ? openlog("writenbycwd",LOG_PID,LOG_USER);
? ? syslog(LOG_INFO, "my syslog OK");
? ? closelog();
? ? return 0;
}
?

效果

需注意,要在sudo下運行,因為寫入log的權限可能不夠。

查看文件/var/log/syslog

cwd@cwd:~$ cd /var/log
cwd@cwd:/var/log$ grep "writenbycwd" syslog
Mar? 1?09:20:57 cwd writenbycwd[4446]: my syslog OK
Mar? 1?09:21:56 cwd writenbycwd[4561]: my syslog OK
Mar? 1?09:28:11 cwd writenbycwd[4821]: my syslog OK

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/714330.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/714330.shtml
英文地址,請注明出處:http://en.pswp.cn/news/714330.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

刷題筆記day27-回溯算法2

216. 組合總和 III 這個思路還是&#xff0c;三部曲&#xff1a; 終止條件處理單層節點回溯節點 題中說的是&#xff0c;1到9的數&#xff0c;不能有重復。 k個數&#xff0c;和為n。 那么只要 len(path) k 的時候&#xff0c;判斷 n 為0&#xff0c;就可以入切片了。 fun…

如何更好的引導大語言模型進行編程的高效開發流程?

這張圖片展示了一種如何更好地引導大語言模型進行編程的方法。 首先&#xff0c;最簡單也是最有效的方法是讓大語言模型重復運行多次&#xff0c;每次增加一些額外的信息&#xff0c;直到獲得想要的結果。這種方法雖然簡單&#xff0c;但可能需要多次嘗試才能得到滿意的結果。…

2024綠色能源、城市規劃與環境國際會議(ICGESCE 2024)

2024綠色能源、城市規劃與環境國際會議(ICGESCE 2024) 一、【會議簡介】 隨著全球氣候變化和環境問題日益嚴重&#xff0c;綠色能源和可持續發展已成為全球關注的焦點。本次會議旨在匯聚全球在綠色能源、城市規劃與環境領域的專家、學者和實踐者&#xff0c;共同探討和分享關于…

Vue.js大師: 構建動態Web應用的全面指南

VUE ECMAScript介紹什么是ECMAScriptECMAScript 和 JavaScript 的關系ECMAScript 6 簡介 ES6新特性let基本使用const不定參數箭頭函數對象簡寫模塊化導出導入a.jsb.jsmain.js Vue簡介MVVM 模式的實現者——雙向數據綁定模式 Vue環境搭建在頁面引入vue的js文件即可。創建div元素…

1、jQuery介紹、css()、選擇器、事件、動畫

一、jQuery介紹&#xff1f; 1、什么是jQuery&#xff1f; 是一個JavaScript函數庫 2、jQuery特點 寫的少&#xff0c;做的多 3、jQuery的安裝 直接下載引入 <script src"jquery-1.10.2.min.js"></script>通過cdn引入 <script src"https…

python自動化之項目架構搭建與思路講解(第二天)

1.自動化測試的概念 自動化測試是指使用自動化工具和腳本來執行測試任務,以驗證軟件或系統的正確性和穩定性。它可以提高測試的效率和準確性,并節約時間和成本。 2.自動化腳本編寫的思路 xmind文檔如有需要,可在資源里自行下載 3.項目代碼工程創建 lib :基本代碼庫包 …

[滲透教程]-013-嗅探工具-wireshark操作

文章目錄 tor下載wireshark抓包類型啟動場景實戰tor下載 tor下載鏈接 zlibary暗網地址 2681506@gmail.com YanErrol123@wireshark Wireshark是網絡封包分析軟件,可以抓包.可以 使用winpcap與網卡直接進行數據交換.作用: 網絡管理員使用wireshark來檢測網絡問題,網絡工程師使用…

瑞_Redis_Redis命令

文章目錄 1 Redis命令Redis數據結構Redis 的 key 的層級結構1.0 Redis通用命令1.0.1 KEYS1.0.2 DEL1.0.3 EXISTS1.0.4 EXPIRE1.0.5 TTL 1.1 String類型1.1.0 String類型的常見命令1.1.1 SET 和 GET1.1.2 MSET 和 MGET1.1.3 INCR和INCRBY和DECY1.1.4 SETNX1.1.5 SETEX 1.2 Hash類…

Android 12.0 framework關于systemUI定制之導航欄透明背景的功能實現

1.概述 在12.0的系統rom產品定制化開發中,在對于系統原生SystemUI的導航欄背景在沉浸式導航欄的 情況下默認是會隨著背景顏色的變化而改變的,在一些特定背景下導航欄的背景也是會改變的,所以由于產品開發需要 要求需要設置導航欄背景為透明的,所以就需要在Activity創建的時…

《秦時明月》IP新高度:與陜西歷史博物館共同書寫文化傳承新篇章!

在IP產業風起云涌的今天&#xff0c;如何以創意和匠心為傳統文化注入新的活力&#xff0c;成為了擺在每一位文化工作者面前的重要課題。近日&#xff0c;《秦時明月》作為一部深受觀眾喜愛的國產動畫IP&#xff0c;在迎來其十七周年之際&#xff0c;聯手陜西歷史博物館&#xf…

c++11 標準模板(STL)(std::tuple)(二)

定義于頭文件 <tuple> template< class... Types > class tuple; (C11 起) 類模板 std::tuple 是固定大小的異類值匯集。它是 std::pair 的推廣。 若 (std::is_trivially_destructible_v<Types> && ...) 為 true &#xff0c;則 tuple 的析構函數是…

設計模式-備忘錄模式(C++)

備忘錄模式&#xff08;Memento Pattern&#xff09;是一種設計模式&#xff0c;用于在不破壞對象封裝的情況下&#xff0c;捕獲和保存對象的內部狀態&#xff0c;并在需要時恢復到之前的狀態。下面是一個簡單的 C 實現備忘錄模式的示例&#xff1a; #include <iostream>…

2024理解這幾個安全漏洞,你也能做安全測試!

如今安全問題顯得越來越重要&#xff0c;一個大型的互聯網站點&#xff0c;你如果每天查看日志&#xff0c;會發現有很多嘗試攻擊性的腳本。 如果沒有&#xff0c;證明網站影響力還不夠大。信息一體化的背后深藏著各類安全隱患&#xff0c;例如由于開發人員的不嚴謹導致為Web應…

章節一、認識three.js與開發環境學習筆記01;

一、如何學習WEB可視化3D技術與課程內容演示&#xff1b; 1、項目案例&#xff1a; 政府有大量的新基建的項目&#xff1a;如數字孿生、智慧城市、智慧園區、智慧工廠、智慧消防等等都涉及了3d的可視化技術&#xff1b; 2、如何系統的學號WEB 3D可視化技術&#xff1f; three…

網絡安全學習筆記1

1.了解kali及安裝 vmware安裝&#xff0c;用戶名密碼均為kali 2.metasploit是什么 3.metasploit攻擊windows系統 在kali中打來終端 數據msfconsole 進入metasploit的控制終端界面 msf的使用法則&#xff1a; 1.使用模塊 2.配置模塊必選項 3.運行模塊 三步操作、實現對…

邏輯回歸與交叉熵--九五小龐

什么是邏輯回歸 線性回歸預測的是一個連續值&#xff0c;邏輯回歸給出的“是”和“否”的回答 Singmoid sigmoid函數是一個概率分布函數&#xff0c;給定某個輸入&#xff0c;它將輸出為一個概率值 邏輯回歸損失函數 平方差所懲罰的是與損失為同一數量級的情形&#xff0…

8、Redis-Jedis、Lettuce和一個Demo

目錄 一、Jedis 二、Lettuce 三、一個Demo Java集成Redis主要有3個方案&#xff1a;Jedis、Lettuce和Redisson。 其中&#xff0c;Jedis、Lettuce側重于單例Redis&#xff0c;而Redisson側重于分布式服務。 項目資源在文末 一、Jedis 1、創建SpringBoot項目 2、引入依賴 …

電商小程序10分類管理

目錄 1 分類數據源2 搭建功能3 創建變量讀取數據4 綁定數據總結 本篇我們介紹一下電商小程序的分類管理功能的開發&#xff0c;先看我們的原型圖&#xff1a; 在首頁我們是展示了四個分類的內容&#xff0c;采用上邊是圖標&#xff0c;下邊是文字的形式。使用低代碼開發&#…

【系統分析師】-需求工程

一、需求工程 需求工程分為需求開發和需求管理。 需求開發&#xff1a;需求獲取&#xff0c;需求分析&#xff0c;需求定義、需求驗證。 需求管理&#xff1a;變更控制、版本控制、需求跟蹤&#xff0c;需求狀態跟蹤。&#xff08;對需求基線的管理&#xff09; 1.1需求獲取…

MySQL:合并查詢語句

1、查詢表的數據 t_book表數據 SELECT * FROM db_book.t_book; t_booktype表數據 SELECT * FROM db_book.t_booktype; 提醒&#xff1a; 下面的查詢操作的數據來自上圖查詢表的數據 2. 使用 UNION 查詢結果合并&#xff0c;會去掉重復的數據 使用UNION關鍵字是&#xff0c;數…