Files
hustoj/web/python-docs/library/syslog.html
2025-03-09 17:10:56 +08:00

346 lines
25 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.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Unix syslog 库例程 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="被取代的模块" href="superseded.html" />
<link rel="prev" title="nis --- Sun 的 NIS (黄页) 接口" href="nis.html" />
<link rel="canonical" href="https://docs.python.org/3/library/syslog.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">Unix syslog 库例程</a><ul>
<li><a class="reference internal" href="#examples">例子</a><ul>
<li><a class="reference internal" href="#simple-example">简单示例</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="nis.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">nis</span></code> --- Sun 的 NIS (黄页) 接口</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="superseded.html"
title="下一章">被取代的模块</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/syslog.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/library/syslog.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="superseded.html" title="被取代的模块"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="nis.html" title="nis --- Sun 的 NIS (黄页) 接口"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="unix.html" accesskey="U">Unix 专有服务</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="module-syslog">
<span id="syslog-unix-syslog-library-routines"></span><h1>Unix syslog 库例程<a class="headerlink" href="#module-syslog" title="永久链接至标题"></a></h1>
<hr class="docutils" />
<p>此模块提供一个接口到Unix <code class="docutils literal notranslate"><span class="pre">syslog</span></code> 日常库. 参考 Unix 手册页关于 <code class="docutils literal notranslate"><span class="pre">syslog</span></code> 设施的详细描述.</p>
<p>此模块包装了系统的 <code class="docutils literal notranslate"><span class="pre">syslog</span></code> 例程族。 一个能与 syslog 服务器对话的纯 Python 库则以 <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 notranslate"><span class="pre">logging.handlers</span></code></a> 模块中 <code class="xref py py-class docutils literal notranslate"><span class="pre">SysLogHandler</span></code> 类的形式提供。</p>
<p>这个模块定义了以下函数:</p>
<dl class="function">
<dt id="syslog.syslog">
<code class="sig-prename descclassname">syslog.</code><code class="sig-name descname">syslog</code><span class="sig-paren">(</span><em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#syslog.syslog" title="永久链接至目标"></a></dt>
<dt>
<code class="sig-prename descclassname">syslog.</code><code class="sig-name descname">syslog</code><span class="sig-paren">(</span><em class="sig-param">priority</em>, <em class="sig-param">message</em><span class="sig-paren">)</span></dt>
<dd><p>将字符串 <em>message</em> 发送到系统日志记录器。 如有必要会添加末尾换行符。 每条消息都带有一个由 <em>facility</em><em>level</em> 组成的优先级标价签。 可选的 <em>priority</em> 参数默认值为 <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_INFO</span></code>,它确定消息的优先级。 如果未在 <em>priority</em> 中使用逻辑或 (<code class="docutils literal notranslate"><span class="pre">LOG_INFO</span> <span class="pre">|</span> <span class="pre">LOG_USER</span></code>) 对 facility 进行编码,则会使用在 <a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal notranslate"><span class="pre">openlog()</span></code></a> 调用中所给定的值。</p>
<p>如果 <a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><span class="pre">syslog()</span></code></a> 的调用之前被调用,则将不带参数地调用 <code class="docutils literal notranslate"><span class="pre">openlog()</span></code></p>
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">syslog.syslog</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">priority</span></code>, <code class="docutils literal notranslate"><span class="pre">message</span></code></p>
</dd></dl>
<dl class="function">
<dt id="syslog.openlog">
<code class="sig-prename descclassname">syslog.</code><code class="sig-name descname">openlog</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">ident</em><span class="optional">[</span>, <em class="sig-param">logoption</em><span class="optional">[</span>, <em class="sig-param">facility</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#syslog.openlog" title="永久链接至目标"></a></dt>
<dd><p>后续 <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 notranslate"><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 notranslate"><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 notranslate"><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 notranslate"><span class="pre">openlog()</span></code></a></p>
<p>可选的 <em>ident</em> 关键字参数是在每条消息前添加的字符串,默认为 <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> 去除打头的路径部分。 可选的 <em>logoption</em> 关键字参数(默认为 0是一个位字段 -- 请参见下文了解可能的组合值。 可选的 <em>facility</em> 关键字参数 (默认为 <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_USER</span></code>) 为没有显式编码 facility 的消息设置默认的 facility。</p>
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">syslog.openlog</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">ident</span></code>, <code class="docutils literal notranslate"><span class="pre">logoption</span></code>, <code class="docutils literal notranslate"><span class="pre">facility</span></code></p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>在之前的版本中,不允许关键字参数,并且要求必须有 <em>ident</em><em>ident</em> 的默认值依赖于系统库,它往往为 <code class="docutils literal notranslate"><span class="pre">python</span></code> 而不是 Python 程序文件的实际名称。</p>
</div>
</dd></dl>
<dl class="function">
<dt id="syslog.closelog">
<code class="sig-prename descclassname">syslog.</code><code class="sig-name descname">closelog</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#syslog.closelog" title="永久链接至目标"></a></dt>
<dd><p>重置日志模块值并且调用系统库 <code class="docutils literal notranslate"><span class="pre">closelog()</span></code>.</p>
<p>这使得此模块在初始导入时行为固定。 例如,<a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><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 notranslate"><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 notranslate"><span class="pre">openlog()</span></code></a> 形参会被重置为默认值。</p>
<p class="audit-hook">引发一个不带参数的 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">syslog.closelog</span></code></p>
</dd></dl>
<dl class="function">
<dt id="syslog.setlogmask">
<code class="sig-prename descclassname">syslog.</code><code class="sig-name descname">setlogmask</code><span class="sig-paren">(</span><em class="sig-param">maskpri</em><span class="sig-paren">)</span><a class="headerlink" href="#syslog.setlogmask" title="永久链接至目标"></a></dt>
<dd><p>将优先级掩码设为 <em>maskpri</em> 并返回之前的掩码值。 调用 <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 notranslate"><span class="pre">syslog()</span></code></a> 并附带未在 <em>maskpri</em> 中设置的优先级将会被忽略。 默认设置为记录所有优先级。 函数 <code class="docutils literal notranslate"><span class="pre">LOG_MASK(pri)</span></code> 可计算单个优先级 <em>pri</em> 的掩码。 函数 <code class="docutils literal notranslate"><span class="pre">LOG_UPTO(pri)</span></code> 可计算包括 <em>pri</em> 在内的所有优先级的掩码。</p>
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">syslog.setlogmask</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">maskpri</span></code></p>
</dd></dl>
<p>此模块定义了一下常量:</p>
<dl class="simple">
<dt>优先级级别 (高到低):</dt><dd><p><code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_EMERG</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_ALERT</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_CRIT</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_ERR</span></code>,
<code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_WARNING</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_NOTICE</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_INFO</span></code>,
<code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_DEBUG</span></code>.</p>
</dd>
<dt>设施:</dt><dd><p><code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_KERN</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_USER</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_MAIL</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_DAEMON</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_AUTH</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_LPR</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_NEWS</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_UUCP</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_CRON</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_SYSLOG</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_LOCAL0</span></code> to <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_LOCAL7</span></code>,如果 <code class="docutils literal notranslate"><span class="pre">&lt;syslog.h&gt;</span></code> 中有定义则还有 <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_AUTHPRIV</span></code></p>
</dd>
<dt>日志选项:</dt><dd><p><code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_PID</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_CONS</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_NDELAY</span></code>,如果 <code class="docutils literal notranslate"><span class="pre">&lt;syslog.h&gt;</span></code> 中有定义则还有 <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_ODELAY</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_NOWAIT</span></code> 以及 <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_PERROR</span></code></p>
</dd>
</dl>
<section id="examples">
<h2>例子<a class="headerlink" href="#examples" title="永久链接至标题"></a></h2>
<section id="simple-example">
<h3>简单示例<a class="headerlink" href="#simple-example" title="永久链接至标题"></a></h3>
<p>一个简单的示例集:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><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">&#39;Processing started&#39;</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">&#39;Processing started&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>一个设置多种日志选项的示例,其中有在日志消息中包含进程 ID以及将消息写入用于邮件日志记录的目标设施等:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><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">&#39;E-mail processing initiated...&#39;</span><span class="p">)</span>
</pre></div>
</div>
</section>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">Unix syslog 库例程</a><ul>
<li><a class="reference internal" href="#examples">例子</a><ul>
<li><a class="reference internal" href="#simple-example">简单示例</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="nis.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">nis</span></code> --- Sun 的 NIS (黄页) 接口</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="superseded.html"
title="下一章">被取代的模块</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/syslog.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="superseded.html" title="被取代的模块"
>下一页</a> |</li>
<li class="right" >
<a href="nis.html" title="nis --- Sun 的 NIS (黄页) 接口"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="unix.html" >Unix 专有服务</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>