search
Homephp教程PHP源码一个简单的日志记录函数

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);
}

Statement
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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment