一个简单的日志记录函数
php代码
/** * 记录一条日志,会以以下三种方式依次尝试写日志。 * - 向当前参数指定的文件写入日志。 * - 尝试向php.ini中指定的error_log写内容。 * - 向系统日志写内容,还是失败的话则返回false。 * * 不用每次调用时都指定logFile和dateFormat参数 * 系统会自动记住上次指定的内容。 * * PHP5.0之后请确保已经设置好时区,否则可能会抛出一个错误。 * example: * @code php * // 第一次调用,初始化日志,并写入第一条信息。 * logg('init...', LOG_INFO, '/usr/log.txt', 'y-m-d'); * // 写日志 * logg('log msg', LOG_INFO); * @endcode * * @param string $message 日志内容 * @param int $type 日志类型,参照syslog函数的参数 * @param string $logFile 日志文件 * @param string $dateFormat 日志的时间格式 * @return bool 是否成功写入 * @staticvar array $types 参数$type对应的描述信息。 * @staticvar string $file 保存$logFile参数最后次传递的内容。 * @staticvar string $format 保存$dateFormat参数最后传递的内容。 * @link http://blog.830725.com/post/13.html */ function logg($message, $type, $logFile = null, $dateFormat = null) { static $types = array( LOG_EMERG => 'EMERG', LOG_ALERT => 'ALERT', LOG_CRIT => 'CRITICAL', LOG_ERR => 'ERROR', LOG_WARNING => 'WARNING', // windows下,以下这三个值是一样的 LOG_NOTICE => 'NOTICE', LOG_DEBUG => 'DEBUG', LOG_INFO => 'INFO'); static $file = null; static $format = 'Y-m-d H:i:s'; if(!is_null($logFile)){ $file = $logFile; } if(!is_null($dateFormat)){ $format = $dateFormat; } /* 格式化消息 */ $type = isset($types[$type]) ? $type : LOG_INFO; $msg = date($format) . ' [' . $types[$type] . '] ' . $message . PHP_EOL; if(error_log($msg, 3, $file)) { return true; } if(error_log($msg, 0)) { return true; } return syslog($type, $message); }
Copy after login
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
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
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation
1 months ago
By DDD
Roblox: Dead Rails - How To Complete Every Challenge
3 weeks ago
By DDD

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
CakePHP Tutorial
1389
52

