要获取UNIX syslog库信息,我们需要在程序中使用syslog模块。此模块的syslog具有与syslog库不同的模块。
要使用此模块,我们应该使用-导入它
import syslog
方法如下-
此方法用于将字符串类型的消息发送到系统记录器。每个消息都有一个优先级。priority参数可用于设置给定消息的优先级。
此方法用于记录后续syslog调用的选项。ident参数是字符串类型的参数;它装在每条消息中。
此方法用于重置syslog模块。导入模块后,该模块进入该状态。
此方法用于将优先级掩码设置为maskpri,它返回以前的掩码值。没有优先级时,maskpri被忽略。
import syslog, sys syslog.openlog(sys.argv[0]) syslog.syslog(syslog.LOG_NOTICE, "This is a Log Notice") syslog.openlog()
输出结果
$ python3 posix_example.py $ sudo cat /var/log/syslog Oct 7 00:05:23 unix_user-VirtualBox anacron[14271]: Job `cron.daily' terminated Oct 7 00:05:23 unix_user-VirtualBox anacron[14271]: Normal exit (1 job run) Oct 7 00:17:01 unix_user-VirtualBox CRON[14396]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 7 00:22:35 unix_user-VirtualBox gnome-software[1599]: no app for changed ubuntu-dock@ubuntu.com Oct 7 00:22:35 unix_user-VirtualBox gnome-software[1599]: no app for changed ubuntu-appindicators@ubuntu.com Oct 7 00:22:36 unix_user-VirtualBox gnome-shell[1296]: [AppIndicatorSupport-DEBUG] Registering StatusNotifierItem :1.59/org/ayatana/NotificationItem/software_update_available Oct 7 00:22:37 unix_user-VirtualBox gvfsd-metadata[3664]: g_udev_device_has_property: assertion 'G_UDEV_IS_DEVICE (device)' failed Oct 7 00:22:37 unix_user-VirtualBox gvfsd-metadata[3664]: g_udev_device_has_property: assertion 'G_UDEV_IS_DEVICE (device)' failed Oct 7 00:25:47 unix_user-VirtualBox snapd[5511]: storehelpers.go:398: cannot refresh: snap has no updates available: "core", "gnome-3-26-1604", "gnome-calculator", "gnome-characters", "gnome-logs", "gnome-system-monitor", "gtk-common-themes" Oct 7 00:25:47 unix_user-VirtualBox snapd[5511]: autorefresh.go:387: auto-refresh: all snaps are up-to-date Oct 7 00:27:32 unix_user-VirtualBox example.py: This is a Log Notice