Docker推荐的日志收集方式是Stdout,而TP5的默认方式是File,通过Loki之类的采集工具不是那么方式,所以需要把日志发送到Stdout
文件位置
TP5的日志驱动位置如下:
在thinkphp/libaray/think/log/driver下创建一个Stdout文件,代码如下
<?php
namespace think\log\driver;
class Stdout
{
protected $config = [
'time_format' => ' c ',
'file_size' => 2097152,
'path' => LOG_PATH,
'apart_level' => [],
];
// 实例化并传入参数
public function __construct($config = [])
{
if (is_array($config)) {
$this->config = array_merge($this->config, $config);
}
}
/**
* 日志写入接口
* @access public
* @param array $log 日志信息
* @param bool $dep 是否写入分割线
* @return bool
*/
public function save(array $log = [], bool $dep = true): bool
{
$info = '';
$stdErr = fopen("php://stderr", "w");
foreach ($log as $val) {
$level = '';
foreach ($val as $msg) {
echo $msg;
if (!is_string($msg)) {
$msg = var_export($msg, true);
}
$level .= $msg;
if ($dep) {
$level .= "\r\n";
}
}
$info .= $level;
fwrite($stdErr, $info);
}
fclose($stdErr);
return true;
}
}
然后把配置文件的日志方式改成Stdout即可,这样就可以更加方便的采集到Docker里的日志,给Docker设置日志大小后,控制文件在100M以内,或者更大一点,也省去写定时脚本清理日志的麻烦。整体使用起来还是挺不错的。