uTools-Manuals/docs/python/syslog.html
2019-04-21 11:50:48 +08:00

8 lines
13 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="body" role="main"><div class="section" id="module-syslog"><h1><span class="yiyi-st" id="yiyi-10">35.13. <a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">syslog</span></code></a> - Unix syslog库例程</span></h1><p><span class="yiyi-st" id="yiyi-11">此模块提供到Unix <code class="docutils literal"><span class="pre">syslog</span></code>库例程的接口。</span><span class="yiyi-st" id="yiyi-12">有关<code class="docutils literal"><span class="pre">syslog</span></code>工具的详细说明请参阅Unix手册页。</span></p><p><span class="yiyi-st" id="yiyi-13">此模块包装系统<code class="docutils literal"><span class="pre">syslog</span></code>例程系列。</span><span class="yiyi-st" id="yiyi-14"><a class="reference internal" href="logging.handlers.html#module-logging.handlers" title="logging.handlers: Handlers for the logging module."><code class="xref py py-mod docutils literal"><span class="pre">logging.handlers</span></code></a>模块中可以使用可以与syslog服务器通信的纯Python库<code class="xref py py-class docutils literal"><span class="pre">SysLogHandler</span></code></span></p><p><span class="yiyi-st" id="yiyi-15">该模块定义了以下功能:</span></p><dl class="function"><dt id="syslog.syslog"><span class="yiyi-st" id="yiyi-16"> <code class="descclassname">syslog.</code><code class="descname">syslog</code><span class="sig-paren">(</span><em>message</em><span class="sig-paren">)</span></span></dt><dt><span class="yiyi-st" id="yiyi-17"><code class="descclassname">syslog。 t&gt;&gt; <code class="descname">syslog</code> <span class="sig-paren"></span> <em>priority</em><em>message</em> <span class="sig-paren"></span></code></span></dt><dd><p><span class="yiyi-st" id="yiyi-18">将字符串<em>消息</em>发送到系统记录器。</span><span class="yiyi-st" id="yiyi-19">如果需要,添加尾随换行符。</span><span class="yiyi-st" id="yiyi-20">每个消息都标记有由<em>设施</em><em>级别</em>组成的优先级。</span><span class="yiyi-st" id="yiyi-21">可选的<em>priority</em>参数(默认为<code class="xref py py-const docutils literal"><span class="pre">LOG_INFO</span></code>)确定消息优先级。</span><span class="yiyi-st" id="yiyi-22">如果在<em>优先级</em>中使用逻辑或(<code class="docutils literal"><span class="pre">LOG_INFO</span> <span class="pre">|</span> <span class="pre">LOG_USER</span> t1 &gt;),使用<a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal"><span class="pre">openlog()</span></code></a>调用中给出的值。</code></span></p><p><span class="yiyi-st" id="yiyi-23">If <a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal"><span class="pre">openlog()</span></code></a> has not been called prior to the call to <a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-func docutils literal"><span class="pre">syslog()</span></code></a>, <code class="docutils literal"><span class="pre">openlog()</span></code> will be called with no arguments.</span></p></dd></dl><dl class="function"><dt id="syslog.openlog"><span class="yiyi-st" id="yiyi-24"> <code class="descclassname">syslog.</code><code class="descname">openlog</code><span class="sig-paren">(</span><span class="optional">[</span><em>ident</em><span class="optional">[</span>, <em>logoption</em><span class="optional">[</span>, <em>facility</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-25">可以通过调用<a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal"><span class="pre">openlog()</span></code></a>来设置后续<a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-func docutils literal"><span class="pre">syslog()</span></code></a>调用的日志记录选项。</span><span class="yiyi-st" id="yiyi-26">如果日志当前未打开,则<a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-func docutils literal"><span class="pre">syslog()</span></code></a>将调用<a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal"><span class="pre">openlog()</span></code></a></span></p><p><span class="yiyi-st" id="yiyi-27">可选的<em>ident</em>关键字参数是一个字符串,附加在每个消息前面,默认为<code class="docutils literal"><span class="pre">sys.argv[0]</span></code></span><span class="yiyi-st" id="yiyi-28">可选的<em>标识</em>关键字参数默认值为0是一个位字段 - 请参阅下面的可能的值组合。</span><span class="yiyi-st" id="yiyi-29">可选的<em>设施</em>关键字参数(默认为<code class="xref py py-const docutils literal"><span class="pre">LOG_USER</span></code>)为没有显式编码的消息设置默认工具。</span></p><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-30"><span class="versionmodified">在版本3.2中已更改:</span>在以前的版本中,不允许使用关键字参数,并且需要使用<em>ident</em></span><span class="yiyi-st" id="yiyi-31"><em>ident</em>的默认值取决于系统库,通常是<code class="docutils literal"><span class="pre">python</span></code>而不是python程序文件的名称。</span></p></div></dd></dl><dl class="function"><dt id="syslog.closelog"><span class="yiyi-st" id="yiyi-32"> <code class="descclassname">syslog.</code><code class="descname">closelog</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-33">重置系统日志模块值并调用系统库<code class="docutils literal"><span class="pre">closelog()</span></code></span></p><p><span class="yiyi-st" id="yiyi-34">这导致模块在初始导入时的行为。</span><span class="yiyi-st" id="yiyi-35">例如,<a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal"><span class="pre">openlog()</span></code></a>将在第一个<a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-func docutils literal"><span class="pre">syslog()</span></code></a>调用(如果<a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal"><span class="pre">openlog()</span></code></a>尚未被调用)和<em>ident</em>和其他<a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal"><span class="pre">openlog()</span></code></a>参数复位为默认值。</span></p></dd></dl><dl class="function"><dt id="syslog.setlogmask"><span class="yiyi-st" id="yiyi-36"> <code class="descclassname">syslog.</code><code class="descname">setlogmask</code><span class="sig-paren">(</span><em>maskpri</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-37">将优先级掩码设置为<em>maskpri</em>,并返回上一个掩码值。</span><span class="yiyi-st" id="yiyi-38">Calls to <a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-func docutils literal"><span class="pre">syslog()</span></code></a> with a priority level not set in <em>maskpri</em> are ignored. </span><span class="yiyi-st" id="yiyi-39">默认值为记录所有优先级。</span><span class="yiyi-st" id="yiyi-40">函数<code class="docutils literal"><span class="pre">LOG_MASK(pri)</span></code>计算单个优先级<em>pri</em>的掩码。</span><span class="yiyi-st" id="yiyi-41">函数<code class="docutils literal"><span class="pre">LOG_UPTO(pri)</span></code>计算所有优先级的掩码,包括<em>pri</em></span></p></dd></dl><p><span class="yiyi-st" id="yiyi-42">模块定义以下常量:</span></p><dl class="docutils"><dt><span class="yiyi-st" id="yiyi-43">优先级(从高到低):</span></dt><dd><span class="yiyi-st" id="yiyi-44"><code class="xref py py-const docutils literal"><span class="pre">LOG_EMERG</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_ALERT</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_CRIT</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_ERR</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_WARNING</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_NOTICE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_INFO</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_DEBUG</span></code>.</span></dd><dt><span class="yiyi-st" id="yiyi-45">设备:</span></dt><dd><span class="yiyi-st" id="yiyi-46"><code class="xref py py-const docutils literal"><span class="pre">LOG_KERN</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_USER</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_MAIL</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_DAEMON</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_AUTH</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_LPR</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_NEWS</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_UUCP</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_CRON</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_SYSLOG</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_LOCAL0</span></code> to <code class="xref py py-const docutils literal"><span class="pre">LOG_LOCAL7</span></code>, and, if defined in <code class="docutils literal"><span class="pre">&lt;syslog.h&gt;</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_AUTHPRIV</span></code>.</span></dd><dt><span class="yiyi-st" id="yiyi-47">日志选项:</span></dt><dd><span class="yiyi-st" id="yiyi-48"><code class="xref py py-const docutils literal"><span class="pre">LOG_PID</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_CONS</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_NDELAY</span></code>, and, if defined in <code class="docutils literal"><span class="pre">&lt;syslog.h&gt;</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_ODELAY</span></code>, <code class="xref py py-const docutils literal"><span class="pre">LOG_NOWAIT</span></code>, and <code class="xref py py-const docutils literal"><span class="pre">LOG_PERROR</span></code>.</span></dd></dl><div class="section" id="examples"><h2><span class="yiyi-st" id="yiyi-49">35.13.1. </span><span class="yiyi-st" id="yiyi-50">Examples</span></h2><div class="section" id="simple-example"><h3><span class="yiyi-st" id="yiyi-51">35.13.1.1. </span><span class="yiyi-st" id="yiyi-52">Simple example</span></h3><p><span class="yiyi-st" id="yiyi-53">一组简单的例子:</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">syslog</span>
<span class="n">syslog</span><span class="o">.</span><span class="n">syslog</span><span class="p">(</span><span class="s1">'Processing started'</span><span class="p">)</span>
<span class="k">if</span> <span class="n">error</span><span class="p">:</span>
<span class="n">syslog</span><span class="o">.</span><span class="n">syslog</span><span class="p">(</span><span class="n">syslog</span><span class="o">.</span><span class="n">LOG_ERR</span><span class="p">,</span> <span class="s1">'Processing started'</span><span class="p">)</span>
</code></pre><p><span class="yiyi-st" id="yiyi-54">设置一些日志选项的示例包括记录消息中的进程ID并将消息写入用于邮件记录的目标设施</span></p><pre><code class="language-python"><span></span><span class="n">syslog</span><span class="o">.</span><span class="n">openlog</span><span class="p">(</span><span class="n">logoption</span><span class="o">=</span><span class="n">syslog</span><span class="o">.</span><span class="n">LOG_PID</span><span class="p">,</span> <span class="n">facility</span><span class="o">=</span><span class="n">syslog</span><span class="o">.</span><span class="n">LOG_MAIL</span><span class="p">)</span>
<span class="n">syslog</span><span class="o">.</span><span class="n">syslog</span><span class="p">(</span><span class="s1">'E-mail processing initiated...'</span><span class="p">)</span>
</code></pre></div></div></div></div>