Use of php error_log function_PHP tutorial
我们来大致了解一下error_log()函数。我们看下手册的解释:
error_log
(PHP 3, PHP 4, PHP 5)
bool error_log ( string message [, int message_type [, string destination [, string extra_headers]]] )
发送一个错误信息到Web服务器的错误日志,一个TCP的端口或者是一个文件。
[separator]
我们看它的参数,第一个参数message是消息内容,第二个参数type是消息类型,第三个参数是目标文件,第四个参数是其他的头信息。其实一般第四个参数都用不到,我们主要看前面三个参数,第二个参数是消息类型,包括0, 1, 2, 3等四种类型,0是默认的类型。四个类型的作用:
0
信息内容发送到PHP的系统日志记录,使用操作系统的自己的日志记录机制或一个文件,整个取决于php.ini中的配置选项error_log的内容。这是缺省选项。
1
把信息内容发送到一个邮件地址,第三个参数就是一个邮件地址,第四个参数是一个头信息发送邮件的,第二个类型使用mail()函数来发送邮件
2
消息通过PHP的调试服务器,远程的写入到某个PHP调试服务器上,当然,PHP在编译的时候--enable-debugger必须打开,另外,整个类型只是针对PHP 3有效
3
消息作为一个新行附加在一个目标文件中
其实对于我们来说,日志为了简便起见,一般直接使用类型3就比较合适了,把日志文件写到自己需要的文件中。
为了示例error_log()函数的简单使用,我们个例子说明。假设我们数据库抽象类使用PEAR::DB类,现在我想在程序中记录我们的程序是否有执行错误。那么我们使用error_log()来记录我们那些SQL语句执行错误或者失败了,至少我们的PEAR::DB类提供了 DB::isError() 方法来获取某个执行结果对象是否有错误,那么我们就能够旁断是否执行某条SQL出现了错误,再来考虑是否记录日志,同时对象有个userinfo的属性,该属性记录了错误的SQL语句,那么我们可以构造这样一个函数:
function logError($object)
{
if(DB::isError($object))
{
error_log(date("[Y-m-d H:i:s]")." -[".$_SERVER['REQUEST_URI']."] :".$object -> userinfo."\n", 3, "/tmp/php_sql_err.log");
return true;
}
return false;
}
这个函数就是能够记录发现了错误SQL的地方,那么自动会把时间,当前页面,和错误的SQL语句信息记录到 /tmp/php_sql_err.log 文件当中,那么,当我们在调试程序的时候,发现数据提取不正确或者没有数据提取的时候,那么我们完全可以查看 /tmp/php_sql_err.log 文件来查看我们的错误页面和错误的SQL语句。
当然,我们必须在我们执行SQL查询的程序中去使用这个函数,比如我们执行一个编写一个提取新闻信息的函数:
function getNewsContent($news_id, $field="")
{
global $db;
$result = $db->getRow("SELECT $field FROM news WHERE news_id = '$news_id'");
if (logError($result))
{
return false;
}
return $result;
}
我们在里面判断SQL是否错误,如果错误了则返回false,然后我们就可以查看日志一下,看我们的函数运行是否如我们预期想象的一样。
我们执行: tail /tmp/php_sql_err.log
能看到类似于这样的信息:
[2006-01-12 11:44:34] -[/news_list.php?news_id=1] :SELECT FROM news WHERE news_id = '1' [nativecode=1064 ** You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version fo
r the right syntax to use near 'FROM news WHERE news_id = '1']
大致是因为我们select的时候,没有把需要提取的字段名写进去,那么我们就可以检查news_list.php文件,减产 getNewsContent ()函数法向没有传递$field参数进去,导致SQL执行错误。所以error_log()函数就帮助检查我们的SQL写的是否正确,或者是参数没有传递正确,这样大大的减轻了开发负担,想但于了对我们的程序进行了单元测试。
当然,你也可以利用error_log()函数做更多记录错误日志,方便PHP开发,这个全是由自己决定。
Use it to record simple logs in this work
$address = post('address'); Explode (',', $ address);
$ url = "/usr/local/apache/eyoung/tmp/mailog _". Date ("y-m-d"). "Log";
foreach($group_add as $value)
{
/**{{{ modify by muzhaoyang -2006.12.13- Record log
*/
$logstr=date("H:i:s"). " ".$uid." " .$star_uid." ".$value."n";
error_log($logstr,3,$url); /code> ;
http://www.bkjia.com/PHPjc/320012.html
true

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
