今天 针对公司自己框架 简单写个日志类 主要使用 __callStatic
静态魔术方法 Swoole 脚本中调用该日志.
<?php
namespace ZScript\Library;
use ZScript\APP;
/**
* Created by PhpStorm.
* User: Len
* Date: 2018/12/13
* Time: 13:05
* Desc:
* @method static error($array)
* @method static unknown($array)
* @method static match($array)
* @method static matchErr($array)
* @method static cancel($array)
* @method static marketMatch($array)
* @method static 1sql($_lastQuery)
* @method static debug($sql)
* @method static rpc($array, $var = null)
* @method static init($array)
* @method static fee($array)
* @method static redis($array)
* @method static dbErr($array)
* @method static price($array)
* @method static landing($array)
*/
class Loger
{
const SPLIT_CHAR = ' ';
private static $unique_log_id = '|';
private static $log_name = null;
/**
* @desc 魔术方法静态方法不存在下调用
* @param $name
* @param $arguments
* ------------------------------------------------------------
*/
public static function __callStatic($name, $arguments)
{
// TODO: Implement __call() method.
self::general(current($arguments), $name);
}
/**
* @desc 交易对日志
* @param $msg
* @param string $log_tag
* ------------------------------------------------------------
*/
public static function general($msg, $log_tag)
{
$str = '';
if (is_array($msg)) {
foreach ($msg as $key => $val) {
if (is_array($val) || is_object($val))
$val = json_encode($val);
$str .= " {$key} {$val}";
}
} else {
$str .= $msg;
}
$worker_id = APP::getWorkerId();
if (is_null($worker_id)) {
$worker_id = 'task';
}
$date = date('Y/m/d H:i:s');
$no = date('Ymd');
$name = self::getLogName();
if (!$name) {
$file_name = LOG_PATH . DS . 'default' . '_' . $no . '.log';
} else {
$file_name = LOG_PATH . DS . $name . '_' . $worker_id . '_' . $no . '.log';
}
$data = $date . self::SPLIT_CHAR;
// 当前日志唯一标识
$data .= Loger::getUniqueLogId() . self::SPLIT_CHAR;
// 当前worker 进程PID
$data .= APP::getWorkerPid() . self::SPLIT_CHAR;
// worker id
$data .= $worker_id . self::SPLIT_CHAR;
// 标记
$data .= ucfirst($log_tag) . self::SPLIT_CHAR . $str . " \n";
file_put_contents($file_name, $data, FILE_APPEND);
}
/**
* @return string
*/
public static function getLogName()
{
return self::$log_name;
}
/**
* @desc
* @author len
* @param string $log_name
* ---------------------------------------------------
*/
public static function setLogName(string $log_name)
{
self::$log_name = $log_name;
}
/**
* @return string
*/
public static function getUniqueLogId()
{
return self::$unique_log_id;
}
/**
* @param string $unique_log_id
*/
public static function setUniqueLogId(string $unique_log_id)
{
self::$unique_log_id = $unique_log_id;
}
/**
* @param $msg
* @param string $name
* ------------------------------------------------------------
*/
public static function log($msg, $name = 'INFO')
{
$str = '';
if (is_array($msg)) {
foreach ($msg as $key => $val) {
if (is_array($val))
$val = json_encode($val);
$str .= " {$key} {$val}";
}
} else {
$str .= $msg;
}
$date = date('Y/m/d H:i:s');
$no = date('Ymd');
$file_name = LOG_PATH . DS . $name . '_' . $no . '.log';
$data = $date . self::SPLIT_CHAR;
$worker_id = APP::getWorkerId();
if (is_null($worker_id)) {
$worker_id = 'task';
}
// 获取唯一标识
$data .= Loger::getUniqueLogId() . self::SPLIT_CHAR;
// 当前worker 进程PID
$data .= APP::getWorkerPid() . self::SPLIT_CHAR;
$data .= $worker_id . self::SPLIT_CHAR . $str . " \n";
file_put_contents($file_name, $data, FILE_APPEND);
}
}
分隔符是为了 logtail
查看日志
写入 worker ID 及 worker PID 及 唯一标识
通过静态方法实现动态创建日志名称的效果.
性能如何暂不考虑, 能输出日志即可, 不喜勿喷.