syslog --- Unix syslog ライブラリルーチン群


このモジュールは Unix の syslog ライブラリルーチンへのインターフェースを提供します。 syslog の機能の詳しい説明は、Unix のマニュアルページを参照してください。

利用可能な環境: Unix, not WASI, not iOS.

このモジュールはシステムの syslog ファミリのルーチンをラップしています。 syslog サーバーと通信できる pure Python のライブラリが、 logging.handlers モジュールの SysLogHandler にあります。

このモジュールには、以下の関数が定義されています:

syslog.syslog(message)
syslog.syslog(priority, message)

文字列 message をシステムロガーに送信します。 必要に応じて末尾に改行を追加します。 各メッセージには、facility と level からなる優先度が付けられます。 省略可能な priority 引数がメッセージの優先度を指定し、デフォルトは LOG_INFO です。 LOG_INFO | LOG_USER のように論理和を使って priority にファシリティを組み込んでいなければ、openlog() の呼び出しで指定した値を使います。

If openlog() has not been called prior to the call to syslog(), openlog() will be called with no arguments.

引数 priority, message を指定して 監査イベント syslog.syslog を送出します。

バージョン 3.2 で変更: In previous versions, openlog() would not be called automatically if it wasn't called prior to the call to syslog(), deferring to the syslog implementation to call openlog().

バージョン 3.12 で変更: This function is restricted in subinterpreters. (Only code that runs in multiple interpreters is affected and the restriction is not relevant for most users.) openlog() must be called in the main interpreter before syslog() may be used in a subinterpreter. Otherwise it will raise RuntimeError.

syslog.openlog([ident[, logoption[, facility]]])

Logging options of subsequent syslog() calls can be set by calling openlog(). syslog() will call openlog() with no arguments if the log is not currently open.

オプションの ident キーワード引数は全てのメッセージの先頭に付く文字列で、デフォルトでは sys.argv[0] から前方のパス部分を取り除いたものです。オプションの logoption キーワード引数 (デフォルトは 0) はビットフィールドです。組み合わせられる値については下記を参照してください。オプションの facility キーワード引数 (デフォルトは LOG_USER) は明示的に facility が encode されていないメッセージに設定される facility です。

引数 ident, logoption, facility を指定して 監査イベント syslog.openlog を送出します。

バージョン 3.2 で変更: 以前のバージョンではキーワード引数を使えず、ident は必須でした。

バージョン 3.12 で変更: この関数のサブインタープリタでの使用には制限があります (複数のインタープリタで実行されるコードだけが影響を受け、ほとんどのユーザーには関係ありません)。 メインインタープリタでのみ呼び出せます。 サブインタープリタで呼び出すと RuntimeError を送出します。

syslog.closelog()

syslog モジュールの値をリセットし、システムライブラリの closelog() を呼び出します。

This causes the module to behave as it does when initially imported. For example, openlog() will be called on the first syslog() call (if openlog() hasn't already been called), and ident and other openlog() parameters are reset to defaults.

引数無しで 監査イベント syslog.closelog を送出します。

バージョン 3.12 で変更: この関数のサブインタープリタでの使用には制限があります (複数のインタープリタで実行されるコードだけが影響を受け、ほとんどのユーザーには関係ありません)。 メインインタープリタでのみ呼び出せます。 サブインタープリタで呼び出すと RuntimeError を送出します。

syslog.setlogmask(maskpri)

Set the priority mask to maskpri and return the previous mask value. Calls to syslog() with a priority level not set in maskpri are ignored. The default is to log all priorities. The function LOG_MASK(pri) calculates the mask for the individual priority pri. The function LOG_UPTO(pri) calculates the mask for all priorities up to and including pri.

引数 maskpri を指定して 監査イベント syslog.setlogmask を送出します。

このモジュールでは以下の定数を定義しています:

syslog.LOG_EMERG
syslog.LOG_ALERT
syslog.LOG_CRIT
syslog.LOG_ERR
syslog.LOG_WARNING
syslog.LOG_NOTICE
syslog.LOG_INFO
syslog.LOG_DEBUG

優先度 (高いものから低いもの) 。

syslog.LOG_AUTH
syslog.LOG_AUTHPRIV
syslog.LOG_CRON
syslog.LOG_DAEMON
syslog.LOG_FTP
syslog.LOG_INSTALL
syslog.LOG_KERN
syslog.LOG_LAUNCHD
syslog.LOG_LPR
syslog.LOG_MAIL
syslog.LOG_NETINFO
syslog.LOG_NEWS
syslog.LOG_RAS
syslog.LOG_REMOTEAUTH
syslog.LOG_SYSLOG
syslog.LOG_USER
syslog.LOG_UUCP
syslog.LOG_LOCAL0
syslog.LOG_LOCAL1
syslog.LOG_LOCAL2
syslog.LOG_LOCAL3
syslog.LOG_LOCAL4
syslog.LOG_LOCAL5
syslog.LOG_LOCAL6
syslog.LOG_LOCAL7

ファシリティです。 LOG_AUTHPRIV、LOG_FTP、LOG_NETINFO、LOG_REMOTEAUTH、LOG_INSTALL、LOG_RAS は、<syslog.h> に定義されている場合に利用できます。

バージョン 3.13 で変更: LOG_FTP、LOG_NETINFO、LOG_REMOTEAUTH、LOG_INSTALL、LOG_RAS、LOG_LAUNCHD が追加されました。

syslog.LOG_PID
syslog.LOG_CONS
syslog.LOG_NDELAY
syslog.LOG_ODELAY
syslog.LOG_NOWAIT
syslog.LOG_PERROR

ログオプションです。 LOG_ODELAY、LOG_NOWAIT、LOG_PERROR は、<syslog.h> に定義されている場合に利用できます。

使用例

シンプルな例

1つ目のシンプルな例:

import syslog

syslog.syslog('Processing started')
if error:
    syslog.syslog(syslog.LOG_ERR, 'Processing started')

いくつかのログオプションを設定する例。ログメッセージにプロセスIDを含み、メッセージをメールのログ用の facility にメッセージを書きます:

syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL)
syslog.syslog('E-mail processing initiated...')