* Logger.php
<?php
/**
* ログを取るクラス
* Usage:
* // クラス定義読み出し
* require './Logger.php';
* // 保存先指定
* Logger::setpath("./log");
* // メッセージログ出力
* Logger::write("保存したいメッセージを書く", 'admin');
*/
class Logger {
protected static $_on = true; // 出力切替
protected static $_prefix = '';
protected static $_ext = '.log';
protected static $_path = '';
private static $MAXSIZE = 4; // rotate size[MB]
/**
* ログ出力をオフに
*/
public static function off() {
self::$_on = false;
}
/**
* ログ出力をオンに(デフォルト)
*/
public static function on() {
self::$_on = true;
}
public static function setSize($size) {
self::$MAXSIZE = $size;
}
/**
* メッセージログ出力
*/
public static function write($s, $user = '') {
if (self::$_on) {
$file = self::_getfile($user);
self::_rotate($user);
// 日付付加
$s = rtrim($s);
if (preg_match('/\n|\r/', $s)) {
$s = self::_dateline() . PHP_EOL
. preg_replace('/(?:\r\n|\r|\n){1,}/', PHP_EOL, $s);
} else {
$s = "[" . self::_getdate() . "] " . self::_caller() . "\t" . $s;
}
return file_put_contents($file, rtrim($s) . PHP_EOL, FILE_APPEND | LOCK_EX);
}
return '';
}
public static function setpath($path) {
if (!is_dir($path)) {
mkdir($path, 0777);
}
self::$_path = realpath($path);
}
public static function setprefix($prefix) {
self::$_prefix = $prefix;
}
protected static function _getdate($short = false) {
date_default_timezone_set('Asia/Shanghai');
return date($short ? 'Y-m-d' : 'Y/m/d H:i:s');
}
protected static function _dateline() {
return str_pad(self::_getdate(), 56, "-", STR_PAD_BOTH);
}
protected static function _getfile($user, $rotate = false) {
$file = rtrim(self::$_path, "/\\")
. DIRECTORY_SEPARATOR
. self::$_prefix
. $user
. ($rotate ? self::_getdate(true) : "")
. self::$_ext;
return $file;
}
protected static function _rotate($type) {
$file = self::_getfile($type);
if (is_writable($file) && filesize($file) > 1024 * 1024 * self::$MAXSIZE) {
rename($file, self::_getfile($type, true));
}
}
protected static function _caller() {
global $argv;
if (!empty($argv[0])) {
$caller = $argv[0];
} else if (isset($_SERVER['SCRIPT_NAME'])) {
$caller = basename($_SERVER['SCRIPT_NAME']);
} else {
$caller = 'Unknown';
}
return $caller;
}
}
类似thinkphp
要define('RUNTIME_PATH', 'xxx');
<?php
/**
* Created by PhpStorm.
* User: mingzhanghui
* Date: 10/24/2019
* Time: 16:24
*/
namespace app\oa\util;
class Logger
{
const MAX_LOG_SIZE = 1048576;
/** @var resource */
protected static $handler = null;
public static function write($msg) {
self::init();
// datetime
$msg = sprintf("[%s] %s\n", date("Y-m-d H:i:s", time()), $msg);
fwrite(self::$handler, $msg, strlen($msg));
}
private static function init() {
$logDir = RUNTIME_PATH.'log';
if (!is_dir($logDir)) {
mkdir($logDir, 0777, true);
}
$logPath = $logDir.'/jobs.log';
if (!is_resource(self::$handler)) {
self::$handler = fopen($logPath, 'a');
}
$stat = fstat(self::$handler);
if ($stat['size'] > self::MAX_LOG_SIZE) {
copy($logPath, $logDir.'/jobs.archive.log');
ftruncate(self::$handler, 0);
rewind(self::$handler);
}
}
public static function close() {
if (is_resource(self::$handler)) {
fclose(self::$handler);
self::$handler = null;
}
}
}