Home php教程 php手册 PHP实现日志处理类库

PHP实现日志处理类库

Jun 13, 2016 am 09:43 AM
aspnet software programming

继上篇文章【微信开发之微电商网站】技术笔记之一,昨日做了日志处理的功能。

对于现在的应用程序来说,日志的重要性是不言而喻的。很难想象没有任何日志记录功能的应用程序运行在生产环境中。日志所能提供的功能是多种多样的,包括记录程序运行时产生的错误信息、状态信息、调试信息和执行时间信息等。在生产环境中,日志是查找问题来源的重要依据。应用程序运行时的产生的各种信息,都应该通过日志类库来进行记录

废话不多说了,附上日志类库的源代码: 

<span   1</span> <span /*</span><span *
</span><span   2</span> <span  * 日志处理类
</span><span   3</span> <span  * 
</span><span   4</span> <span  * @since alpha 0.0.1
</span><span   5</span> <span  * @date 2014.03.04
</span><span   6</span> <span  * @author genialx
</span><span   7</span> <span  * 
</span><span   8</span>  <span */</span>
<span   9</span>  
<span  10</span> <span class</span> <span Log</span><span {
</span><span  11</span>      
<span  12</span>     <span //</span><span 单例模式</span>
<span  13</span>     <span private</span> <span static</span> <span $instance</span>    = <span NULL</span><span ;
</span><span  14</span>     <span //</span><span 文件句柄</span>
<span  15</span>     <span private</span> <span static</span> <span $handle</span>      = <span NULL</span><span ;
</span><span  16</span>     <span //</span><span 日志开关</span>
<span  17</span>     <span private</span> <span $log_switch</span>     = <span NULL</span><span ;
</span><span  18</span>     <span //</span><span 日志相对目录</span>
<span  19</span>     <span private</span> <span $log_file_path</span>      = <span NULL</span><span ;
</span><span  20</span>     <span //</span><span 日志文件最大长度,超出长度重新建立文件</span>
<span  21</span>     <span private</span> <span $log_max_len</span>        = <span NULL</span><span ;
</span><span  22</span>     <span //</span><span 日志文件前缀,入 log_0</span>
<span  23</span>     <span private</span> <span $log_file_pre</span>       = 'log_'<span ;
</span><span  24</span>  
<span  25</span>          
<span  26</span>     <span /*</span><span *
</span><span  27</span> <span      * 构造函数
</span><span  28</span> <span      * 
</span><span  29</span> <span      * @since alpha 0.0.1
</span><span  30</span> <span      * @date 2014.02.04
</span><span  31</span> <span      * @author genialx
</span><span  32</span>      <span */</span>
<span  33</span>     <span protected</span> <span function</span> __construct(){<span //</span><span 注意:以下是配置文件中的常量,请读者自行更改</span>
<span  34</span>          
<span  35</span>         <span $this</span>->log_file_path     =<span  LOG_FILE_PATH;
</span><span  36</span>          
<span  37</span>         <span $this</span>->log_switch     =<span  LOG_SWITCH;  
</span><span  38</span>      
<span  39</span>         <span $this</span>->log_max_len    =<span  LOG_MAX_LEN;
</span><span  40</span>      
<span  41</span> <span     }
</span><span  42</span>      
<span  43</span>     <span /*</span><span *
</span><span  44</span> <span      * 单利模式
</span><span  45</span> <span      * 
</span><span  46</span> <span      * @since alpha 0.0.1
</span><span  47</span> <span      * @date 2014.02.04
</span><span  48</span> <span      * @author genialx
</span><span  49</span>      <span */</span>
<span  50</span>     <span public</span> <span static</span> <span function</span><span  get_instance(){
</span><span  51</span>         <span if</span>(!self::<span $instance</span><span  instanceof self){
</span><span  52</span>             self::<span $instance</span> = <span new</span><span  self;
</span><span  53</span> <span         }
</span><span  54</span>         <span return</span> self::<span $instance</span><span ;
</span><span  55</span> <span     }
</span><span  56</span>      
<span  57</span>     <span /*</span><span *
</span><span  58</span> <span      * 
</span><span  59</span> <span      * 日志记录
</span><span  60</span> <span      * 
</span><span  61</span> <span      * @param int $type  0 -> 记录(THING LOG) / 1 -> 错误(ERROR LOG)
</span><span  62</span> <span      * @param string $desc
</span><span  63</span> <span      * @param string $time
</span><span  64</span> <span      * 
</span><span  65</span> <span      * @since alpha 0.0.1
</span><span  66</span> <span      * @date 2014.02.04
</span><span  67</span> <span      * @author genialx
</span><span  68</span> <span      * 
</span><span  69</span>      <span */</span>
<span  70</span>     <span public</span> <span function</span> <span log</span>(<span $type</span>,<span $desc</span>,<span $time</span><span ){
</span><span  71</span>         <span if</span>(<span $this</span>-><span log_switch){
</span><span  72</span>              
<span  73</span>             <span if</span>(self::<span $handle</span> == <span NULL</span><span ){
</span><span  74</span>                 <span $filename</span> = <span $this</span>->log_file_pre . <span $this</span>-><span get_max_log_file_suf();
</span><span  75</span>                 self::<span $handle</span> = <span fopen</span>(<span $this</span>->log_file_path . <span $filename</span>, 'a'<span );
</span><span  76</span> <span             }
</span><span  77</span>             <span switch</span>(<span $type</span><span ){
</span><span  78</span>                 <span case</span> 0:
<span  79</span>                     <span fwrite</span>(self::<span $handle</span>, 'THING LOG:' . ' ' . <span $desc</span> . ' ' . <span $time</span> . <span chr</span>(13<span ));
</span><span  80</span>                     <span break</span><span ;
</span><span  81</span>                 <span case</span> 1:
<span  82</span>                     <span fwrite</span>(self::<span $handle</span>, 'ERROR LOG:' . ' ' . <span $desc</span> . ' ' . <span $time</span> . <span chr</span>(13<span ));
</span><span  83</span>                     <span break</span><span ;
</span><span  84</span>                 <span default</span>:
<span  85</span>                     <span fwrite</span>(self::<span $handle</span>, 'THING LOG:' . ' ' . <span $desc</span> . ' ' . <span $time</span> . <span chr</span>(13<span ));
</span><span  86</span>                     <span break</span><span ;
</span><span  87</span> <span             }
</span><span  88</span>              
<span  89</span> <span         }
</span><span  90</span> <span     }
</span><span  91</span>      
<span  92</span>     <span /*</span><span *
</span><span  93</span> <span      * 获取当前日志的最新文档的后缀
</span><span  94</span> <span      * 
</span><span  95</span> <span      * @since alpha 0.0.1
</span><span  96</span> <span      * @date 2014.02.04
</span><span  97</span> <span      * @author genialx
</span><span  98</span>      <span */</span>
<span  99</span>     <span private</span> <span function</span><span  get_max_log_file_suf(){
</span><span 100</span>         <span $log_file_suf</span> = <span null</span><span ;
</span><span 101</span>         <span if</span>(<span is_dir</span>(<span $this</span>-><span log_file_path)){
</span><span 102</span>             <span if</span>(<span $dh</span> = <span opendir</span>(<span $this</span>-><span log_file_path)){
</span><span 103</span>                 <span while</span>((<span $file</span> = <span readdir</span>(<span $dh</span>)) != <span FALSE</span><span ){
</span><span 104</span>                     <span if</span>(<span $file</span> != '.' && <span $file</span> != '..'<span ){
</span><span 105</span>                         <span if</span>(<span filetype</span>( <span $this</span>->log_file_path . <span $file</span>) == 'file'<span ){
</span><span 106</span>                             <span $rs</span> = <span split</span>('_', <span $file</span><span );
</span><span 107</span>                             <span if</span>(<span $log_file_suf</span> < <span $rs</span>[1<span ]){
</span><span 108</span>                                 <span $log_file_suf</span> = <span $rs</span>[1<span ];
</span><span 109</span> <span                             }
</span><span 110</span> <span                         }
</span><span 111</span> <span                     }
</span><span 112</span> <span                 }
</span><span 113</span>                  
<span 114</span>                 <span if</span>(<span $log_file_suf</span> == <span NULL</span><span ){
</span><span 115</span>                     <span $log_file_suf</span> = 0<span ;
</span><span 116</span> <span                 }
</span><span 117</span>                 <span //</span><span 截断文件</span>
<span 118</span>                 <span if</span>( <span file_exists</span>(<span $this</span>->log_file_path . <span $this</span>->log_file_pre . <span $log_file_suf</span>) && <span filesize</span>(<span $this</span>->log_file_path . <span $this</span>->log_file_pre . <span $log_file_suf</span>) >= <span $this</span>-><span log_max_len){
</span><span 119</span>                     <span $log_file_suf</span> = <span intval</span>(<span $log_file_suf</span>) + 1<span ;
</span><span 120</span> <span                 }
</span><span 121</span>                  
<span 122</span>                 <span return</span> <span $log_file_suf</span><span ;
</span><span 123</span> <span             }   
</span><span 124</span> <span         }
</span><span 125</span>          
<span 126</span>         <span return</span> 0<span ;
</span><span 127</span>          
<span 128</span> <span     }
</span><span 129</span>      
<span 130</span>     <span /*</span><span *
</span><span 131</span> <span      * 关闭文件句柄
</span><span 132</span> <span      * 
</span><span 133</span> <span      * @since alpha 0.0.1
</span><span 134</span> <span      * @date 2014.02.04
</span><span 135</span> <span      * @author genialx
</span><span 136</span>      <span */</span>
<span 137</span>     <span public</span> <span function</span><span  close(){
</span><span 138</span>         <span fclose</span>(self::<span $handle</span><span );
</span><span 139</span> <span     }
</span><span 140</span> }
Copy after login

功能说明:
该日志类利用单例模式,节省资源。自行判断文件大小,超出指定大小则按序自行创建文件。如:文件log_0大于指定大小,则重新创建log_1文件(注意:创建文件是安装文件名后缀的数字的,请勿随意更改日志文件名)。

有待优化:没有指定文件的最大个数,所以定期要手动删除过多的日志文件。


调用示例:

<span 1</span> <span //</span><span LOG</span>
<span 2</span> <span $L</span> = <span Log</span>::<span get_instance();
</span><span 3</span> <span //</span><span 第一个参数 int 0代表事件记录(THING LOG:),1代表错误记录(ERROR LOG:)
</span><span 4</span> <span //第二个参数 string 描述文字
</span><span 5</span> <span //第三个参数 string 时间</span>
<span 6</span> <span $L</span>-><span log</span>(1,'日志描述', <span date</span>('Y-n-j H:m:s'<span ));
</span><span 7</span> <span $L</span>->close();
Copy after login

 

感谢您的查阅!

文章来源:http://www.ihuxu.com/p/223.html

微信公众号(每日分享有价值的互联网资讯):胡旭个人博客

新浪微博:@身边的互联网

编程讨论群:235173087

QQ:2252065614

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The combination of Vue.js and ASP.NET provides tips and suggestions for performance optimization and expansion of web applications. The combination of Vue.js and ASP.NET provides tips and suggestions for performance optimization and expansion of web applications. Jul 29, 2023 pm 05:19 PM

The combination of Vue.js and ASP.NET provides tips and suggestions for performance optimization and expansion of web applications. With the rapid development of web applications, performance optimization has become an indispensable and important task for developers. As a popular front-end framework, Vue.js combined with ASP.NET can help us achieve better performance optimization and expansion. This article will introduce some tips and suggestions, and provide some code examples. 1. Reduce HTTP requests The number of HTTP requests directly affects the loading speed of web applications. pass

Ten ways generative AI will change software development Ten ways generative AI will change software development Mar 11, 2024 pm 12:10 PM

Translator | Reviewed by Chen Jun | Chonglou In the 1990s, when people mentioned software programming, it usually meant choosing an editor, checking the code into the CVS or SVN code base, and then compiling the code into an executable file. Corresponding integrated development environments (IDEs) such as Eclipse and Visual Studio can integrate programming, development, documentation, construction, testing, deployment and other steps into a complete software development life cycle (SDLC), thus improving the work of developers. efficiency. In recent years, popular cloud computing and DevSecOps automation tools have improved developers' comprehensive capabilities, making it easier for more enterprises to develop, deploy and maintain software applications. Today, generative AI is the next generation development

MySQL connection pool usage and optimization techniques in ASP.NET programs MySQL connection pool usage and optimization techniques in ASP.NET programs Jun 30, 2023 pm 11:54 PM

How to correctly use and optimize the MySQL connection pool in ASP.NET programs? Introduction: MySQL is a widely used database management system that features high performance, reliability, and ease of use. In ASP.NET development, using MySQL database for data storage is a common requirement. In order to improve the efficiency and performance of database connections, we need to correctly use and optimize the MySQL connection pool. This article will introduce how to correctly use and optimize the MySQL connection pool in ASP.NET programs.

How to reconnect to MySQL in ASP.NET program? How to reconnect to MySQL in ASP.NET program? Jun 29, 2023 pm 02:21 PM

How to reconnect to MySQL in ASP.NET program? In ASP.NET development, it is very common to use the MySQL database. However, due to network or database server reasons, the database connection may sometimes be interrupted or time out. In this case, in order to ensure the stability and reliability of the program, we need to re-establish the connection after the connection is disconnected. This article will introduce how to reconnect MySQL connections in ASP.NET programs. To reference the necessary namespaces first, reference them at the head of the code file

The combination of Vue.js and ASP.NET enables the development and deployment of enterprise-level applications The combination of Vue.js and ASP.NET enables the development and deployment of enterprise-level applications Jul 29, 2023 pm 02:37 PM

The combination of Vue.js and ASP.NET enables the development and deployment of enterprise-level applications. In today's rapidly developing Internet technology field, the development and deployment of enterprise-level applications has become more and more important. Vue.js and ASP.NET are two technologies widely used in front-end and back-end development. Combining them can bring many advantages to the development and deployment of enterprise-level applications. This article will introduce how to use Vue.js and ASP.NET to develop and deploy enterprise-level applications through code examples. First, we need to install

How to correctly configure and use MySQL connection pool in ASP.NET program? How to correctly configure and use MySQL connection pool in ASP.NET program? Jun 29, 2023 pm 12:56 PM

How to correctly configure and use MySQL connection pool in ASP.NET program? With the development of the Internet and the increase in data volume, the demand for database access and connections is also increasing. In order to improve the performance and stability of the database, connection pooling has become an essential technology. This article mainly introduces how to correctly configure and use the MySQL connection pool in ASP.NET programs to improve the efficiency and response speed of the database. 1. The concept and function of connection pooling. Connection pooling is a technology that reuses database connections. At the beginning of the program,

Using and optimizing transaction performance of MySQL connection pool in ASP.NET Using and optimizing transaction performance of MySQL connection pool in ASP.NET Jun 30, 2023 pm 12:12 PM

How to correctly use and optimize the transaction performance of MySQL connection pool in ASP.NET programs? In ASP.NET programs, database transactions are a very important part. Transactions ensure the consistency and integrity of the database while also providing better performance. When using a MySQL database, it is essential to use connection pools to manage connection resources and optimize performance. First, let us briefly understand the concept of MySQL connection pool. The connection pool is a buffer pool of a group of connections. By pre-initializing a certain number of

What are the built-in objects in aspnet? What are the built-in objects in aspnet? Nov 21, 2023 pm 02:59 PM

The built-in objects in ASP.NET include "Request", "Response", "Session", "Server", "Application", "HttpContext", "Cache", "Trace", "Cookie" and "Server.MapPath": 1. Request, indicating the HTTP request issued by the client; 2. Response: indicating the HTTP response returned by the web server to the client, etc.

See all articles