PHP日志笔记

PHP日志

PHP自带

error_log

bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )

把错误信息发送到 web 服务器的错误日志,或者到一个文件里。

message_type:
0.PHP日志
1.发送到destination设置的邮件地址
3.被发送到位置为destination的文件里
4.直接发送到SAPI的日志处理程序中

// 如果无法连接到数据库,发送通知到服务器日志
if (!Ora_Logon($username, $password)) {
    error_log("Oracle database not available!", 0);
}
// 如果用尽了 FOO,通过邮件通知管理员
if (!($foo = allocate_new_foo())) {
    error_log("Big trouble, we're all out of FOOs!", 1,
               "operator@example.com");
}
// 调用 error_log() 的另一种方式:
error_log("You messed up!", 3, "/var/tmp/my-errors.log");  

Monolog

github

Monolog works with PHP 7.0 or above, use Monolog ^1.0 for PHP 5.3+ support.

  1. 8个级别:debug, info, notice, warning, error, critical, alert, emergency
  2. 支持firePHP,chromePHP调试,浏览器输出
  3. 支持redis,socket,数据库,文件流等写入
use MonologLogger;
use MonologHandlerStreamHandler;
use MonologHandlerChromePHPHandler;
use MonologFormatterLineFormatter;

// Create some handlers
$stream = new StreamHandler(__DIR__.'/my_app.log');

// 设置格式

// the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%
"
$output = "[%datetime%] > %level_name% > %message% %context% %extra%
";
// finally, create a formatter
$formatter = new LineFormatter($output);
$stream->setFormatter($formatter);

// Create the main logger of the app
$logger = new Logger('my_logger');
$logger->pushHandler($stream);
$logger->pushHandler(new ChromePHPHandler());

$logger->error('12121');
$logger->warn('这是警告信息!');

// Or clone the first one to only change the channel
$securityLogger = $logger->withName('security');

$securityLogger->error('12121');
$securityLogger->warn('这是警告信息!');

thinkphp5.0.9的Log

File驱动也是用的error_log函数

protected function write($message, $destination, $apart = false)
{
    //检测日志文件大小,超过配置大小则备份日志文件重新生成
    if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
        rename($destination, dirname($destination) . DS . time() . '-' . basename($destination));
        $this->writed[$destination] = false;
    }
    ......
    return error_log($message, 3, $destination);
}

YII2.0.11的Log

File驱动:用的是fwrite和file_put_contents

/**
 * Writes log messages to a file.
 * @throws InvalidConfigException if unable to open the log file for writing
 */
public function export()
{
    $text = implode("
", array_map([$this, 'formatMessage'], $this->messages)) . "
";
    if (($fp = @fopen($this->logFile, 'a')) === false) {
        throw new InvalidConfigException("Unable to append to log file: {$this->logFile}");
    }
    @flock($fp, LOCK_EX);
    if ($this->enableRotation) {
        // clear stat cache to ensure getting the real current file size and not a cached one
        // this may result in rotating twice when cached file size is used on subsequent calls
        clearstatcache();
    }
    if ($this->enableRotation && @filesize($this->logFile) > $this->maxFileSize * 1024) {
        $this->rotateFiles();
        @flock($fp, LOCK_UN);
        @fclose($fp);
        @file_put_contents($this->logFile, $text, FILE_APPEND | LOCK_EX);
    } else {
        @fwrite($fp, $text);
        @flock($fp, LOCK_UN);
        @fclose($fp);
    }
    if ($this->fileMode !== null) {
        @chmod($this->logFile, $this->fileMode);
    }
}
原文地址:https://www.cnblogs.com/pthlp/p/6928948.html