记录日记
一定要引入vendor/autoload.php 否则无法使用monolog
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
require_once __DIR__ . '../../../../../api/v1/vendor/autoload.php';
// log setting. $log = new Logger('email'); $month = date('Y-m'); // 这是当前文件夹下建立一个专门存日记的文件夹 $log->pushHandler(new StreamHandler(__DIR__ . '/log/email_' . $month . '.log', Logger::DEBUG)); $log->info('Start processing...', ['wo_id' => $wo_id]); // 这是一个实例 try { $result = send_email($send_email, $mail_subject, $content, $content); } catch (Exception $e) { $log->error('email sending error'); }
日记2:
echo __DIR__; $log=123123123; file_put_contents(__DIR__.'/splogs/12312.txt', $log, FILE_APPEND);
日记3:
function wh_log($log_msg) { $log_filename = $_SERVER['DOCUMENT_ROOT']."/log"; if (!file_exists($log_filename)) { // create directory/folder uploads. mkdir($log_filename, 0777, true); } $log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log'; file_put_contents($log_file_data, $log_msg . "\n", FILE_APPEND); }
monolog 日记封装:
<?php namespace Shared; use PDOStatement; use Monolog\Logger; use Monolog\Handler\StreamHandler; use Monolog\Formatter\LineFormatter; class Logging { private $logger; private $dateFormat = 'Y-m-d H:i:s'; private $output = "%datetime% %message%\n"; public function __construct($name = '', $db_name = 'mic00001') { if ($name === '') { $name = 'logs_' . Date('Y-m'); } $this->logger = new Logger('eamic'); $path = __DIR__ . '/../../../files/' . $db_name; if (file_exists('/mnt/disk01/files/' . $db_name)) { $path = '/mnt/disk01/files/' . $db_name; } $path .= '/logs/' . $name . '.txt'; $stream = new StreamHandler($path, Logger::DEBUG); $formatter = new LineFormatter($this->output, $this->dateFormat); $stream->setFormatter($formatter); $this->logger->pushHandler($stream); } public function write($log) { // Ensure that the variable $log is a string if ($log instanceof PDOStatement) { $log = implode(' ', array_slice($log->errorInfo(), 2)); } elseif (!is_string($log)) { $log = json_encode($log); } $this->logger->debug($log); } }