<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>S.Q</title>
    <description>孙强的博客地址见:</description>
    <link>https://shellbacksq.github.io/</link>
    <atom:link href="https://shellbacksq.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 21 Dec 2018 14:44:17 +0000</pubDate>
    <lastBuildDate>Fri, 21 Dec 2018 14:44:17 +0000</lastBuildDate>
    <generator>Jekyll v3.7.4</generator>
    
      <item>
        <title>机器学习泛化误差</title>
        <description>&lt;h1 id=&quot;机器学习泛化误差&quot;&gt;机器学习泛化误差&lt;/h1&gt;
&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#机器学习泛化误差&quot; id=&quot;markdown-toc-机器学习泛化误差&quot;&gt;机器学习泛化误差&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-概念解释&quot; id=&quot;markdown-toc-1-概念解释&quot;&gt;1. 概念解释&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#11-期望损失风险&quot; id=&quot;markdown-toc-11-期望损失风险&quot;&gt;1.1 期望损失风险&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#12-经验损失风险&quot; id=&quot;markdown-toc-12-经验损失风险&quot;&gt;1.2 经验损失风险&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#13-结构损失风险&quot; id=&quot;markdown-toc-13-结构损失风险&quot;&gt;1.3 结构损失风险&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;1-概念解释&quot;&gt;1. 概念解释&lt;/h2&gt;

&lt;h3 id=&quot;11-期望损失风险&quot;&gt;1.1 期望损失风险&lt;/h3&gt;
&lt;blockquote&gt;
  模型在任意未知测试样本的表现
&lt;/blockquote&gt;

&lt;script type=&quot;math/tex; mode=display&quot;&gt;min_{g\in \mathcal{G}} L(g)=\mathbb{E_{x,y\sim P_{x,y}}}l(g;x,y)&lt;/script&gt;

其中 $\mathcal{G}$ 是预先给定的一个函数族, $g$ 是函数组中的一个函数, $P_{x,y}$ 是数据的真是分布,$l$ 是损失函数.

&lt;h3 id=&quot;12-经验损失风险&quot;&gt;1.2 经验损失风险&lt;/h3&gt;
&lt;blockquote&gt;
  模型在训练集数据上的表现
&lt;/blockquote&gt;

假设有训练数据集 $S_n={(x_i,y_i),…,(x_n,y_n)}$

&lt;script type=&quot;math/tex; mode=display&quot;&gt;min_{g\in\mathcal{G}} L(\hat{g}_n)=\frac{1}{n}\sum_{i=1}^n l(g;x_i,y_i)&lt;/script&gt;

&lt;h3 id=&quot;13-结构损失风险&quot;&gt;1.3 结构损失风险&lt;/h3&gt;
&lt;blockquote&gt;
  函数空间$\mathcal{G}$受限时,即有正则项时的表现,此时函数子空间范数小于$c$.
&lt;/blockquote&gt;

</description>
        <pubDate>Thu, 20 Dec 2018 22:21:47 +0000</pubDate>
        <link>https://shellbacksq.github.io/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/2018/12/20/erroranalysis.html</link>
        <guid isPermaLink="true">https://shellbacksq.github.io/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/2018/12/20/erroranalysis.html</guid>
        
        <category>理论</category>
        
        
        <category>机器学习</category>
        
      </item>
    
      <item>
        <title>Python logging模块</title>
        <description>&lt;h1 id=&quot;python-logging模块&quot;&gt;Python logging模块&lt;/h1&gt;
&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#python-logging模块&quot; id=&quot;markdown-toc-python-logging模块&quot;&gt;Python logging模块&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-80-usage-part&quot; id=&quot;markdown-toc-1-80-usage-part&quot;&gt;1. 80% USAGE PART&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#11-基本语句&quot; id=&quot;markdown-toc-11-基本语句&quot;&gt;1.1 基本语句&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#12-如何在程序中使用&quot; id=&quot;markdown-toc-12-如何在程序中使用&quot;&gt;1.2 如何在程序中使用&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-深入理解logging模块&quot; id=&quot;markdown-toc-2-深入理解logging模块&quot;&gt;2. 深入理解logging模块&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#21-python中logging模块的结构&quot; id=&quot;markdown-toc-21-python中logging模块的结构&quot;&gt;2.1 Python中logging模块的结构&lt;/a&gt;            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#211-logger&quot; id=&quot;markdown-toc-211-logger&quot;&gt;2.1.1 Logger&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#212-handler&quot; id=&quot;markdown-toc-212-handler&quot;&gt;2.1.2 Handler&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#213-formatter&quot; id=&quot;markdown-toc-213-formatter&quot;&gt;2.1.3 Formatter&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#22-配置共享&quot; id=&quot;markdown-toc-22-配置共享&quot;&gt;2.2 配置共享&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#23-文件配置&quot; id=&quot;markdown-toc-23-文件配置&quot;&gt;2.3 文件配置&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#3-loguru&quot; id=&quot;markdown-toc-3-loguru&quot;&gt;3. loguru&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
  起因: 看到一篇文章&lt;a href=&quot;https://juejin.im/post/5b13fdd0f265da6e0b6ff3dd&quot;&gt;PyCon 2018: 利用logging模块轻松地进行Python日志记录&lt;/a&gt;(本文也主要参考了这篇文章), 里面有几句话引起了我的注意:
  &lt;ol&gt;
    &lt;li&gt;开发过程中程序出现了问题,我们可以去debug,但是程序开发完毕,在生产环境中,我们如何追踪可能出现的问题, 这时候就需要日志记录.&lt;/li&gt;
    &lt;li&gt;很多人会使用 print 语句输出一些运行信息，然后再在控制台观察，这样其实是非常不规范的. (其实我就是print流的…)&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

不难看出logging模块的出现是为了在程序运行时输出运行时信息,&lt;strong&gt;有&lt;code class=&quot;highlighter-rouge&quot;&gt;bug&lt;/code&gt;时可以定位&lt;code class=&quot;highlighter-rouge&quot;&gt;bug&lt;/code&gt;,没有&lt;code class=&quot;highlighter-rouge&quot;&gt;bug&lt;/code&gt;也可以用来跟踪输出&lt;/strong&gt;,这在追踪模型输出,评估模型好坏时也特别有用,&lt;code class=&quot;highlighter-rouge&quot;&gt;automl&lt;/code&gt;就是这个思路.

&lt;h2 id=&quot;1-80-usage-part&quot;&gt;1. 80% USAGE PART&lt;/h2&gt;

&lt;h3 id=&quot;11-基本语句&quot;&gt;1.1 基本语句&lt;/h3&gt;
可以看出简单使用的话可以这样写:
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# log级别,格式,保存位置&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEBUG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;format&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(asctime)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s : &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(levelname)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s : &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(message)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;# 运行时间,日志级别,日志内容.&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;test.log&quot;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#要想打印到控制台就不用这句,basicConfig下不能兼得.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 下面这些写在代码里面,可以出现在任何想出现的位置.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'debug message'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;info message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'warn message'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;error message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;critical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'critical message'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 更加典型的使用场景,记录错误和异常.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Faild to get result'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exc_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Finished'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

一些解释:
&lt;ul&gt;
  &lt;li&gt;basicConfig 配置了 level 信息和 format 信息,&lt;code class=&quot;highlighter-rouge&quot;&gt;asctime&lt;/code&gt;,&lt;code class=&quot;highlighter-rouge&quot;&gt;levelname&lt;/code&gt;,&lt;code class=&quot;highlighter-rouge&quot;&gt;message&lt;/code&gt;分别代表运行实践,日志级别和日志信息.&lt;/li&gt;
  &lt;li&gt;basicConfig更多的参数如:
    &lt;ul&gt;
      &lt;li&gt;filename：即日志输出的文件名，使用了它,日志信息就不会被控制台打印出来.&lt;/li&gt;
      &lt;li&gt;filemode：这个是指定日志文件的写入方式，有两种形式，一种是 w，一种是 a，分别代表清除后写入和追加写入。&lt;/li&gt;
      &lt;li&gt;format：指定日志信息的输出格式，即上文示例所示的参数，详细参数可以参考： https://docs.python.org/3/library/logging.html?highlight=logging%20threadname#logrecord-attributes&lt;/li&gt;
      &lt;li&gt;datefmt：指定时间的输出格式。
        &lt;ul&gt;
          &lt;li&gt;重要的有:&lt;/li&gt;
          &lt;li&gt;%(levelno)s：打印日志级别的数值。&lt;/li&gt;
          &lt;li&gt;%(levelname)s：打印日志级别的名称。&lt;/li&gt;
          &lt;li&gt;%(pathname)s：打印当前执行程序的路径，其实就是sys.argv[0]。&lt;/li&gt;
          &lt;li&gt;%(filename)s：打印当前执行程序名。&lt;/li&gt;
          &lt;li&gt;%(funcName)s：打印日志的当前函数。&lt;/li&gt;
          &lt;li&gt;%(lineno)d：打印日志的当前行号。&lt;/li&gt;
          &lt;li&gt;%(asctime)s：打印日志的时间。&lt;/li&gt;
          &lt;li&gt;%(message)s：打印日志信息。&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;style：如果 format 参数指定了，这个参数就可以指定格式化时的占位符风格，如 %、{、$ 等。&lt;/li&gt;
      &lt;li&gt;level：指定日志输出的类别，程序会输出大于等于此级别的信息。&lt;/li&gt;
      &lt;li&gt;stream：在没有指定 filename 的时候会默认使用 StreamHandler，这时 stream 可以指定初始化的文件流。&lt;/li&gt;
      &lt;li&gt;handlers：可以指定日志处理时所使用的 Handlers，必须是可迭代的。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;接下来声明了一个 Logger 对象，它就是日志输出的主类,在初始化的时候我们传入了模块的名称，这里直接使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;__name__&lt;/code&gt; 来代替了，就是模块的名称.&lt;/li&gt;
  &lt;li&gt;运行结果
    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;2018&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;06&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;03&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;526&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__main__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBUG&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2018&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;06&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;03&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;526&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__main__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INFO&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2018&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;06&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;03&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;526&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__main__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WARNING&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Warning&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exists&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2018&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;06&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;03&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;526&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__main__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ERROR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Warning&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exists&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2018&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;06&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;03&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;526&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__main__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CRITICAL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Warning&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exists&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;hr /&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;12-如何在程序中使用&quot;&gt;1.2 如何在程序中使用&lt;/h3&gt;

&lt;strong&gt;不能做到的&lt;/strong&gt;
&lt;ul&gt;
  &lt;li&gt;一边输出到控制台,一边写入到日志文件.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-深入理解logging模块&quot;&gt;2. 深入理解logging模块&lt;/h2&gt;
所以如何更加专业地使用logging模块呢?
&lt;strong&gt;我们先定下几个目标,然后逐渐实现它:&lt;/strong&gt;
&lt;ol&gt;
  &lt;li&gt;一边输出到控制台,一边写入到日志文件,将不同级别的日志发送到不同的地方.&lt;/li&gt;
  &lt;li&gt;实现可配置的日志,可以有一个模板,我们不必从头开始&lt;/li&gt;
  &lt;li&gt;自动化的日志搜集和分析&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;21-python中logging模块的结构&quot;&gt;2.1 Python中logging模块的结构&lt;/h3&gt;

现在回到问题的一开始,如何设计这样一个模块它要能完成如下任务:
&lt;ul&gt;
  &lt;li&gt;它要包含一定运行时信息,包括时间,代码位置等等.&lt;/li&gt;
  &lt;li&gt;它要能保存到本地或者某台服务器上.&lt;/li&gt;
  &lt;li&gt;它最好速度要快,不影响运行&lt;/li&gt;
  &lt;li&gt;它最好分为一定等级,这样便于定位不同信息.&lt;/li&gt;
&lt;/ul&gt;

粗略能想到这些,那么&lt;code class=&quot;highlighter-rouge&quot;&gt;python&lt;/code&gt;中的&lt;code class=&quot;highlighter-rouge&quot;&gt;logging&lt;/code&gt;模块是怎样实现的呢?
在 https://docs.python.org/3/library/logging.html 中有以下几个主要的对象:
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Logger&lt;/strong&gt;     主类 https://docs.python.org/3/library/logging.html#logger-objects&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Handler&lt;/strong&gt;    指定打印或存放位置  https://docs.python.org/3/library/logging.html#handler-objects&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Formatter&lt;/strong&gt;  指定日志的格式 https://docs.python.org/3/library/logging.html#formatter-objects&lt;/li&gt;
&lt;/ul&gt;

具体来说&lt;code class=&quot;highlighter-rouge&quot;&gt;Logger&lt;/code&gt;用来创建一个日志记录的对象,它是所有操作的入口,&lt;code class=&quot;highlighter-rouge&quot;&gt;Handler&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;Formatter&lt;/code&gt;对象最后都会传给它. 而在&lt;code class=&quot;highlighter-rouge&quot;&gt;Logger&lt;/code&gt;创建一个日志对象后, 使用&lt;code class=&quot;highlighter-rouge&quot;&gt;Handler&lt;/code&gt;来指定日志保存的位置,可以是控制台,本地文件,远程服务器,甚至是某个邮箱. 设置好保存的位置,接下来就可以定制日志的样式了,这里很重要,因为我们最终看到的日志,就是在这里设定的, 我们指定&lt;code class=&quot;highlighter-rouge&quot;&gt;Formatter&lt;/code&gt;来定制样式.

&lt;strong&gt;整个使用流程见下面这个图&lt;/strong&gt;:
&lt;img src=&quot;https://shellbacksq.github.io/assets/logging flow.png&quot; alt=&quot;logging&quot; /&gt;

一个分开&lt;code class=&quot;highlighter-rouge&quot;&gt;Logger&lt;/code&gt;,&lt;code class=&quot;highlighter-rouge&quot;&gt;Handler&lt;/code&gt;,&lt;code class=&quot;highlighter-rouge&quot;&gt;Formatter&lt;/code&gt;使用的简单例子.

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicConfig&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;和&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;后面再设置有什么区别&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Logger&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setLevel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;INFO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'output.log'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Handler&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;formatter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Formatter&lt;/span&gt;\
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(asctime)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(name)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(levelname)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(message)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Formatter&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setFormatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;formatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'This is a log info'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Debugging'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warning&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Warning exists'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Finish'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

结合上面的流程图和上面代码中的这两行:
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setFormatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;formatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
可以看出&lt;code class=&quot;highlighter-rouge&quot;&gt;Formatter&lt;/code&gt;传给&lt;code class=&quot;highlighter-rouge&quot;&gt;Handler&lt;/code&gt;,&lt;code class=&quot;highlighter-rouge&quot;&gt;Handler&lt;/code&gt;带着自身和&lt;code class=&quot;highlighter-rouge&quot;&gt;Formatter&lt;/code&gt;的信息又传给了&lt;code class=&quot;highlighter-rouge&quot;&gt;Logger&lt;/code&gt;, 最后完成了一个完整的日志记录过程.
那么我们来一个一个分析&lt;code class=&quot;highlighter-rouge&quot;&gt;Formatter&lt;/code&gt;,&lt;code class=&quot;highlighter-rouge&quot;&gt;Handler&lt;/code&gt;,&lt;code class=&quot;highlighter-rouge&quot;&gt;Logger&lt;/code&gt;.

&lt;h4 id=&quot;211-logger&quot;&gt;2.1.1 &lt;a href=&quot;https://docs.python.org/3/library/logging.html#logger-objects&quot;&gt;Logger&lt;/a&gt;&lt;/h4&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;Logger&lt;/code&gt;一般不会单独使用,而是使用模块级别的函数&lt;code class=&quot;highlighter-rouge&quot;&gt;logging.getLogger(name)&lt;/code&gt;,常用的记录器对象的方法分为两类：配置和发送消息。
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Logger.setLevel()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Logger.debug()&lt;/code&gt;….
日志级别如下:&lt;/li&gt;
&lt;/ul&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Level&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Numeric value&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;CRITICAL&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;50&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;ERROR&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;40&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;WARNING&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;30&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;INFO	2&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;DEBUG&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;10&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;NOTSET&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

还有一些方法如:
&lt;ul&gt;
  &lt;li&gt;Logger.addHandler()和Logger.removeHandler()从记录器对象中添加和删除处理程序对象。处理器详见Handlers。&lt;/li&gt;
  &lt;li&gt;Logger.addFilter()和Logger.removeFilter()从记录器对象添加和删除过滤器对象。&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;212-handler&quot;&gt;2.1.2 &lt;a href=&quot;https://docs.python.org/3/library/logging.html#handler-objects&quot;&gt;Handler&lt;/a&gt;&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;SteamHandler             日志输出到流   #logging自带&lt;/li&gt;
  &lt;li&gt;FileHandler              日志输出到文件 #logging自带&lt;/li&gt;
  &lt;li&gt;RotatingFileHandler       按照大小自动分割日志文件   #超出最大容量时&lt;/li&gt;
  &lt;li&gt;TimedRotatingFileHandler 按照时间自动分割日志文件&lt;/li&gt;
  &lt;li&gt;SocketHandler            远程输出日志到TCP/IP sockets&lt;/li&gt;
  &lt;li&gt;DatagramHandler          远程输出日志到UDP sockets&lt;/li&gt;
  &lt;li&gt;SMTPHandler              远程输出日志到邮件地址&lt;/li&gt;
  &lt;li&gt;MemoryHandler            日志输出到内存中的指定buffer&lt;/li&gt;
  &lt;li&gt;HTTPHandler              通过”GET”或者”POST”远程输出到HTTP服务器。&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging.handlers&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HTTPHandler&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setLevel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEBUG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# StreamHandler&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stream_handler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StreamHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stream_handler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setLevel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEBUG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# FileHandler&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;file_handler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'output.log'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;file_handler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setLevel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;INFO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;formatter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Formatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(asctime)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(name)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(levelname)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(message)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;file_handler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setFormatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;formatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# HTTPHandler&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;http_handler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HTTPHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'localhost:8001'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'log'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'POST'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;http_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# SMTPHandler&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;smtp_handler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SMTPHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mailhost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;smtp.163.com&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fromaddr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'sunqiang42@163.com'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                  &lt;span class=&quot;n&quot;&gt;toaddrs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'sq11235@163.com'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,],&lt;/span&gt;
                                  &lt;span class=&quot;n&quot;&gt;subject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;logging to email test&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                  &lt;span class=&quot;n&quot;&gt;credentials&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'sunqiang42@163.com'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'***'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                                  &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;smtp_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Log&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'This is a log info'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Debugging'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warning&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Warning exists'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Finish'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
上述代码在运行后会在:
&lt;ul&gt;
  &lt;li&gt;控制台打印日志信息&lt;/li&gt;
  &lt;li&gt;本地保存日志信息&lt;/li&gt;
  &lt;li&gt;HTTP Server接收到日志信息&lt;/li&gt;
  &lt;li&gt;Email 接受日志信息&lt;/li&gt;
&lt;/ul&gt;

这里我们就完成了几个任务:
&lt;ol&gt;
  &lt;li&gt;同时输出不同格式的信息给不同的对象&lt;/li&gt;
  &lt;li&gt;可以同时拥有多个logger对象,不同的信息内容给不同的对象,例如只有ERROR信息才发邮件.&lt;/li&gt;
  &lt;li&gt;另外每个 Handler 还可以设置 level 信息，最终输出结果的 level 信息会取 Logger 对象的 level 和 Handler 对象的 level 的交集。&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;213-formatter&quot;&gt;2.1.3 &lt;a href=&quot;https://docs.python.org/3/library/logging.html#formatter-objects&quot;&gt;Formatter&lt;/a&gt;&lt;/h4&gt;

Formatter和前面&lt;a href=&quot;#1.1&quot;&gt;基本语句&lt;/a&gt;差不多,不在赘述.

&lt;h3 id=&quot;22-配置共享&quot;&gt;2.2 配置共享&lt;/h3&gt;

我们希望在一个地方定义好了logging配置,然后在别的模块里复用就行了.比如说在&lt;code class=&quot;highlighter-rouge&quot;&gt;main.py&lt;/code&gt;函数里定义好了level,formatter,handler等信息,然后如果&lt;code class=&quot;highlighter-rouge&quot;&gt;main.py&lt;/code&gt;中调用别的模块,在该模块里面也能使用&lt;code class=&quot;highlighter-rouge&quot;&gt;main.py&lt;/code&gt;中的logger配置.

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# main.py&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;core&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'main'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setLevel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEBUG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Handler&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'result.log'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setLevel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;INFO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;formatter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Formatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(asctime)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(name)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(levelname)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(message)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setFormatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;formatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Main Info'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Main Debug'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Main Error'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;core&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;# main中可能要调用core的信息&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
这就设置好了模板.然后在core中:
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# core.py&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'main.core'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;# 在core.py的开头只需要写上这两句就可以了.&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Core Info'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Core Debug'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Core Error'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;23-文件配置&quot;&gt;2.3 文件配置&lt;/h3&gt;
把配置写到日志配置文件中去:(有.conf和.yaml多种方式,看起来还是.yaml更加直观.)
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# logging.yaml&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;formatters&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;brief&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%(asctime)s&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%(message)s&quot;&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;simple&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%(asctime)s&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%(name)s&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%(levelname)s&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%(message)s&quot;&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;handlers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;logging.StreamHandler&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;formatter&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;brief&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;level   &lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;INFO&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;stream  &lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ext://sys.stdout&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;logging.FileHandler&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;formatter&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;simple&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DEBUG&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;debug.log&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;logging.handlers.RotatingFileHandler&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ERROR&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;formatter&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;simple&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;error.log&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;maxBytes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;10485760&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;backupCount&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;20&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;utf8&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;loggers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;main.core&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DEBUG&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;handlers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DEBUG&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;handlers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
然后在程序中调用
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging.config&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fileConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'logging.conf'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'debug message'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;info message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'warn message'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;error message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;critical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'critical message'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

可以看出日志的配置更加灵活,几句话概括就是不同的handler可以使用不同的formmter,不同的
logger可以使用不同的handler.

&lt;hr /&gt;
然后我们可以这样使用:
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# main_conf.py&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;core&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;yaml&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging.config&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup_logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'config.yaml'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default_level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;INFO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default_path&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'r'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'utf-8'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yaml&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dictConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_level&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Start'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Exec'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Finished'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__main__'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;yaml_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'config.yaml'&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;setup_logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;yaml_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;core&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-loguru&quot;&gt;3. loguru&lt;/h2&gt;
https://github.com/Delgan/loguru

&lt;strong&gt;未完待续&lt;/strong&gt;!

参考资料:
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/soddu10v40aDYpm_P6U3IQ&quot;&gt;所有 Python 程序员必须要学会的「日志」记录。
&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/32969467&quot;&gt;使用logging管理爬虫
&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.algorithmdog.com/loggingsystem&quot;&gt;普通程序员怎么理解日志系统
&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://juejin.im/post/5b13fdd0f265da6e0b6ff3dd#heading-3&quot;&gt;PyCon 2018: 利用logging模块轻松地进行Python日志记录&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.python.org/3/library/logging.html#handler-objects&quot;&gt;logging — Logging facility for Python¶
&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/Delgan/loguru/blob/master/README.rst&quot;&gt;loguru&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://blog.jobbole.com/113413/?utm_source=blog.jobbole.com&amp;amp;utm_medium=relatedPosts&quot;&gt;日志的艺术（The art of logging）
&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Wed, 12 Dec 2018 12:06:47 +0000</pubDate>
        <link>https://shellbacksq.github.io/%E5%BC%80%E5%8F%91/2018/12/12/logger.html</link>
        <guid isPermaLink="true">https://shellbacksq.github.io/%E5%BC%80%E5%8F%91/2018/12/12/logger.html</guid>
        
        <category>log</category>
        
        <category>python</category>
        
        <category>工程</category>
        
        
        <category>开发</category>
        
      </item>
    
      <item>
        <title>论文写作---清华大学</title>
        <description>&lt;h1 id=&quot;论文写作清华大学&quot;&gt;论文写作—清华大学&lt;/h1&gt;

论文发表流程:

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;确定方向&lt;/th&gt;
      &lt;th&gt;统计机器翻译&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;确定问题&lt;/td&gt;
      &lt;td&gt;利用句法对长距离调序建模&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;确定思路&lt;/td&gt;
      &lt;td&gt;将树到串对泛化为树到串模板&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;确定方法&lt;/td&gt;
      &lt;td&gt;规则抽取，抽取算法&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;实验验证&lt;/td&gt;
      &lt;td&gt;数据集、基线系统、评价指标&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;撰写论文&lt;/td&gt;
      &lt;td&gt;投稿ACL&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;选择方向&quot;&gt;选择方向：&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;重要问题、重大挑战&lt;/li&gt;
  &lt;li&gt;自己感兴趣&lt;/li&gt;
  &lt;li&gt;即将成为热门&lt;/li&gt;
  &lt;li&gt;高风险性&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Fri, 07 Dec 2018 19:12:23 +0000</pubDate>
        <link>https://shellbacksq.github.io/%E5%85%B6%E5%AE%83/2018/12/07/paperwriting.html</link>
        <guid isPermaLink="true">https://shellbacksq.github.io/%E5%85%B6%E5%AE%83/2018/12/07/paperwriting.html</guid>
        
        <category>教程</category>
        
        
        <category>其它</category>
        
      </item>
    
      <item>
        <title>邢波老师致广大学员一封信</title>
        <description>&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &amp;lt;龙星计划&amp;gt;邢波老师致广大学员一封信(2010-10-26)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

http://bcmi.sjtu.edu.cn/ds/

亲爱的同学们，很荣幸这五天能和大家在龙星计划课堂和大家相识。我真诚地希望你们能够带着耐心和热情学完所有的章节，我也非常高兴你们能享受到这些课程的乐趣。作为一个老师你们的赞同和支持是对我最高的褒奖。我收到许多你们关于如何做一个好的研究者的问题，下面我分享一下我几年前也给一个同学类似的建议，这个同学名字我已经忘了。希望这些建议作为李菲菲关于如何做一个好的研究者的补充。

&lt;ol&gt;
  &lt;li&gt;明白你的目标是什么，你的长处和短处，从技术和性格两方面，因为他们对你的成功同样重要。设定你的计划，坚定履行。
2.&lt;font color=&quot;red&quot;&gt; 如果你觉得需要改变自己了，不要等待不要犹豫，立刻改变。&lt;/font&gt;任何时候改变你的职业方向都是不晚的只要这个改变是重要的，而你坚定努力实现它。浪费在犹豫上的时间往往比你攀登成功的时间要多。对我来说，我曾经陷入谷底，因为那是当我改变方向时我对机器学习一无所知。但我知道我喜欢这门学科，因为你是有魅力且强大有用。我花了五年时间从底物到成为CMU的机器学习教授。&lt;/li&gt;
  &lt;li&gt;你需要非常努力工作。在我的研究生和研究生涯中，我平均每天工作12个小时，每周7天，每时每刻保持高度集中和高效的工作方式。(当然我个人看来，并没有把这些当做是工作，在我眼中这些工作比所谓的娱乐有意思多了)&lt;/li&gt;
  &lt;li&gt;你努力工作并不是因为你的老板push你，而是因为你有热情和热爱你的工作。实际上，我经常告诉我的学生和朋友我’活’在我的研究中和不是工作。我热爱我的工作胜过看电视和打牌。&lt;/li&gt;
  &lt;li&gt;你仍然可以过得精彩纷呈、活力四射。我热爱体育和音乐，也喜欢做其他的事情，我喜欢和我的朋友和家人在一起。所以我从来不消耗时间。当我不做工作的时候，我非常努力地运动和朋友家人在一起。&lt;/li&gt;
  &lt;li&gt;你需要训练自己的创造力，和独立性。说实话我研究生生涯的所有idea都不是来自于导师而是我自己。我在伯克利导师那得到是更多是精神上的鼓励和支持，研究风格和品位、自豪感和学术诚信，还有就是收获友谊。&lt;/li&gt;
  &lt;li&gt;关注你所在领域的最新进展。&lt;font color=&quot;red&quot;&gt;每年读1000篇文章是很寻常的事。&lt;/font&gt;通常来说，要想要在你领域达到顶尖的位置，需要知道你所在领域的每件事，这还不够，还要知道很多相关领域的事。&lt;/li&gt;
  &lt;li&gt;当你取得一定成绩和突破事，另一方面，你要尊敬领域权威的智慧，不要想他们笨你很聪明，你要总是思考为什么他们当年没有想到你想到的事。另一方面当你确信你看到了权威们没看到的东西，你要更加自信地超过他们。&lt;/li&gt;
  &lt;li&gt;要灵活一点，许多问题和技术是有关联的，不要把自己吊死在一棵树上。&lt;/li&gt;
  &lt;li&gt;在做研究时，当一个完美主义者和理想主义者没什么不好的。显然乐观的性格和强壮的体魄都是非常重要的。不要认为别人不够聪明就可以少干一点。事实上是，我认识的顶级研究者，不但智力惊人，同样工作很长时间并且高效。即便是在体能上他们也都很厉害。我在学校时是个半职业运动员，即便是现在我也能长时间工作，比小我十岁的学生都厉害。&lt;/li&gt;
  &lt;li&gt;学会沟通的艺术。随时准备和你的同事和竞争者讨论和分享你的idea。从你的同事甚至竞争者身上学习你没有的东西。&lt;/li&gt;
  &lt;li&gt;最重要的事，要诚实、开放、耐心、快乐、远见，而不是狭促、鼓励、不安、短视。&lt;/li&gt;
&lt;/ol&gt;

最后我祝愿你们在即将到来的一年快乐、丰收。

eric
</description>
        <pubDate>Fri, 07 Dec 2018 18:23:47 +0000</pubDate>
        <link>https://shellbacksq.github.io/%E5%85%B6%E5%AE%83/2018/12/07/xingbo.html</link>
        <guid isPermaLink="true">https://shellbacksq.github.io/%E5%85%B6%E5%AE%83/2018/12/07/xingbo.html</guid>
        
        <category>翻译</category>
        
        
        <category>其它</category>
        
      </item>
    
      <item>
        <title>如何使用github搭建个人博客</title>
        <description>&lt;h1 id=&quot;如何使用github搭建个人博客&quot;&gt;如何使用github搭建个人博客&lt;/h1&gt;

&lt;h2 id=&quot;1-先在本地搭建博客&quot;&gt;1. 先在本地搭建博客&lt;/h2&gt;
准备：
&lt;ul&gt;
  &lt;li&gt;安装jekyll&lt;/li&gt;
  &lt;li&gt;新建或者克隆一个项目&lt;/li&gt;
  &lt;li&gt;使用jekyll部署&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;11-安装jekyll&quot;&gt;1.1 安装jekyll&lt;/h3&gt;
为什么要使用jekyll，&lt;a href=&quot;http://jekyllcn.com/&quot;&gt;jekyll的中文网站&lt;/a&gt;已经解释的很清楚了:
&lt;blockquote&gt;
  简单
不再需要数据库，不需要开发评论功能，不需要不断的更新版本——只用关心你的博客内容。
使用 Jekyll
&lt;/blockquote&gt;

&lt;blockquote&gt;
  静态
Markdown（或 Textile）、Liquid 和 HTML &amp;amp; CSS 构建可发布的静态网站。
Jekyll 模版
&lt;/blockquote&gt;

&lt;blockquote&gt;
  博客支持
支持自定义地址、博客分类、页面、文章以及自定义的布局设计。
迁移你的博客
&lt;/blockquote&gt;

&lt;em&gt;建议在linux系统下使用jekyll, 我在windows下折腾了半天都没能解决编码问题,官方也告诫最好不要在windows下使用,如果手里只有windows计算机，可以使用win10自带的ubuntu子系统，亲测有效.&lt;/em&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;先安装ruby&lt;/strong&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;apt-get install ruby-full&lt;/code&gt;
然后到&lt;a href=&quot;https://gems.ruby-china.com//&quot;&gt;ruby中国&lt;/a&gt;去替换ruby源,不然安装包会很慢.
    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
bundle config mirror.https://rubygems.org https://gems.ruby-china.com
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;使用&lt;code class=&quot;highlighter-rouge&quot;&gt;gem&lt;/code&gt;安装&lt;code class=&quot;highlighter-rouge&quot;&gt;jekyll&lt;/code&gt;&lt;/strong&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;gem install jekyll&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;12-新建或者克隆一个项目&quot;&gt;1.2 新建或者克隆一个项目&lt;/h3&gt;

&lt;h4 id=&quot;121-基本步骤&quot;&gt;1.2.1 基本步骤&lt;/h4&gt;
先从简单的开始,克隆一个项目:
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$git clone https://github.com/poole/poole.git
$cd pool
$jekyll serve
Configuration file: /mnt/d/code/learbhub/blog/poole/_config.yml
       Deprecation: The 'gems' configuration option has been renamed to 'plugins'. Please update your config file accordingly.
            Source: /mnt/d/code/learbhub/blog/poole
       Destination: /mnt/d/code/learbhub/blog/poole/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
                    done in 1.388 seconds.
                    Auto-regeneration may not work on some Windows versions.
                    Please see: https://github.com/Microsoft/BashOnWindows/issues/216
                    If it does not work, please upgrade Bash on Windows or run Jekyll with --no-watch.
 Auto-regeneration: enabled for '/mnt/d/code/learbhub/blog/poole'
    Server address: http://127.0.0.1:4000/
  Server running... press ctrl-c to stop.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
这时在本地(http://127.0.0.1:4000/)就可以打开访问了
&lt;img src=&quot;https://shellbacksq.github.io/assets/home.PNG&quot; alt=&quot;home&quot; /&gt;

远端访问可以使用命令:
&lt;code class=&quot;highlighter-rouge&quot;&gt;jekyll s --host='0.0.0.0'&lt;/code&gt;

&lt;h4 id=&quot;122-文件结构解释&quot;&gt;1.2.2 文件结构解释&lt;/h4&gt;
主要模块及其作用:
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_config.yml&lt;/code&gt; 保存配置数据。很多配置选项都可以直接在命令行中进行设置，但是如果你把那些配置写在这儿，你就不用非要去记住那些命令了。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_drafts&lt;/code&gt; drafts（草稿）是未发布的文章。这些文件的格式中都没有 title.MARKUP 数据。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_includes&lt;/code&gt; 你可以加载这些包含部分到你的布局或者文章中以方便重用。可以用这个标签   来把文件 _includes/file.ext 包含进来。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_layouts&lt;/code&gt; layouts（布局）是包裹在文章外部的模板。布局可以在 YAML 头信息中根据不同文章进行选择。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_posts&lt;/code&gt; 这里放的就是你的文章了。文件格式很重要，必须要符合: YEAR-MONTH-DAY-title.MARKUP。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_data&lt;/code&gt; 	格式化好的网站数据应放在这里。jekyll 的引擎会自动加载在该目录下所有的 yaml 文件（后缀是 .yml, .yaml, .json 或者 .csv ）。这些文件可以经由 ｀site.data｀ 访问。如果有一个 members.yml 文件在该目录下，你就可以通过 site.data.members 获取该文件的内容。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_site&lt;/code&gt; 一旦 Jekyll 完成转换，就会将生成的页面放在这里（默认）。最好将这个目录放进你的 .gitignore 文件中。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;index.html and other HTML, Markdown, Textile files&lt;/code&gt; 如果这些文件中包含 YAML 头信息 部分，Jekyll 就会自动将它们进行转换。当然，其他的如 .html, .markdown, .md, 或者 .textile 等在你的站点根目录下或者不是以上提到的目录中的文件也会被转换。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;其他&lt;/code&gt; 其他一些未被提及的目录和文件如  css 还有 images 文件夹， favicon.ico 等文件都将被完全拷贝到生成的 site 中&lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;pool里面的目录结构是这样的&lt;/strong&gt;:
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;poole/
├── 404.html
├── LICENSE.md
├── README.md
├── _config.yml
├── _includes # 方便文件重用
│   └── head.html 
├── _layouts # 外部的模板,控制页面样式
│   ├── default.html
│   ├── page.html
│   └── post.html
├── _posts # 存放文章的地方,一般都是markdown文件
│   ├── 2016-01-01-whats-jekyll.md
│   ├── 2016-01-02-example-content.md
│   └── 2016-01-03-introduction.md
├── _sass # css扩展,控制样式
│   ├── _base.scss
│   ├── _code.scss
│   ├── _layout.scss
│   ├── _masthead.scss
│   ├── _message.scss
│   ├── _pagination.scss
│   ├── _posts.scss
│   ├── _syntax.scss
│   ├── _type.scss
│   └── _variables.scss
├── _site # jekyll转化后的文件
│   ├── 2016
│   │   └── 01
│   │       ├── 01
│   │       │   └── whats-jekyll
│   │       │       └── index.html
│   │       ├── 02
│   │       │   └── example-content
│   │       │       └── index.html
│   │       └── 03
│   │           └── introduction
│   │               └── index.html
│   ├── 404.html
│   ├── LICENSE.md
│   ├── README.md
│   ├── about
│   │   └── index.html
│   ├── atom.xml
│   ├── index.html
│   ├── page2
│   │   └── index.html
│   ├── page3
│   │   └── index.html
│   ├── public
│   │   ├── apple-touch-icon-precomposed.png
│   │   └── favicon.ico
│   └── styles.css
├── about.md # 简介
├── atom.xml
├── index.html # 首页
├── public # 公共文件
│   ├── apple-touch-icon-precomposed.png
│   └── favicon.ico
└── styles.scss

18 directories, 41 files
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h4 id=&quot;123-频繁使用的地方&quot;&gt;1.2.3 频繁使用的地方&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_posts&lt;/code&gt; 存放&lt;code class=&quot;highlighter-rouge&quot;&gt;markdwon&lt;/code&gt;文件的地方&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_config.yml&lt;/code&gt; 配置&lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_posts&lt;/code&gt;&lt;/strong&gt;
所有的文章直接放在_posts文件夹下面，格式就是我们之前提到的markdown文件，默认的格式是.md和.markdown文件。每篇文章的开始处需要使用yml格式来写明这篇文章的简单介绍，格式如下：
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
    author: kresnikwang 
    comments: true # 是否能被评论
    date: 2015-04-28 17:42:32+00:00
    layout: post
    title: PHP, Angular JS Development|My Export Quote|农产品出口工具开发
    categories: # 分类
    - Works
    - Tech 
    tags: # 标签
    - bootstrap
    - javascript
    - php
    - AngularJS
    ---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_config.yml&lt;/code&gt;&lt;/strong&gt;
详见(http://jekyllcn.com/docs/configuration/)
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Permalinks
permalink:           pretty

# Setup
title:               Poole
tagline:             The Jekyll Butler
url:                 http://getpoole.com
paginate:            1
baseurl:             &quot;&quot;

# Assets
#
# We specify the directory for Jekyll so we can use @imports.
sass:
  sass_dir:          _sass
  style:            :compressed

# About/contact
author:
  name:              Mark Otto
  url:               https://twitter.com/mdo
  email:             markdotto@gmail.com

# Custom vars
version:             2.0.0
github:
  repo:              https://github.com/poole/poole

# Gems
gems:
  - jekyll-paginate
  - jekyll-gist

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Mon, 03 Dec 2018 14:41:25 +0000</pubDate>
        <link>https://shellbacksq.github.io/%E5%85%B6%E5%AE%83/2018/12/03/githubio.html</link>
        <guid isPermaLink="true">https://shellbacksq.github.io/%E5%85%B6%E5%AE%83/2018/12/03/githubio.html</guid>
        
        <category>教程</category>
        
        
        <category>其它</category>
        
      </item>
    
  </channel>
</rss>
