2 Star 0 Fork 1

广东时代传媒集团有限公司 / times-ext-sls

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Laravel SLS

安装

# 安装
$ composer require newtimes/times-ext-sls
Laravel
# 1、生成配置文件
$ php artisan vendor:publish --tag="sls"

# 2、修改配置文件 /config/sls.php 或在 /.env 文件中添加配置
SLS_ACCESS_KEY_ID=
SLS_ACCESS_KEY_SECRET=
SLS_ENDPOINT=
SLS_PROJECT=
SLS_LOG_STORE=
#SLS_TOPIC=  #可选
#SLS_SOURCE=  #可选
#SLS_ERROR_LOG_CHANNEL= #可选[默认-daily]

# 3、修改 /config/logging.php 配置,channels 中增加 sls,以下方式二选一;
    
## 3.1 修改 /.env 中 LOG_CHANNEL 为 stack,stack.channels 增加 sls
    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily', 'sls'],  //增加 'sls'
            'ignore_exceptions' => false,
        ],
        ......

        'sls' => [
            'driver' => 'monolog',
            'handler' => Seffeng\LaravelSLS\Handler\SLSHandler::class,
            'level'  => 'debug',
        ],

## 3.2 修改 /.env 中 LOG_CHANNEL 为 ssl
    'channels' => [
        ......

        'sls' => [
            'driver' => 'daily',
            'level'  => 'debug',
            'path' => storage_path('logs/laravel.log'),
            'tap'  => [Seffeng\LaravelSLS\Logging\SLSFormatter::class],
            'days' => 14,
        ],
lumen
# 1、将以下代码段添加到 /bootstrap/app.php 文件中的 Providers 部分
$app->register(Seffeng\LaravelSLS\SLSServiceProvider::class);

# 2、参考扩展包内 config/sls.php 在 /.env 文件中添加配置
SLS_ACCESS_KEY_ID=
SLS_ACCESS_KEY_SECRET=
SLS_ENDPOINT=
SLS_PROJECT=
SLS_LOG_STORE=
#SLS_TOPIC=  #可选
#SLS_SOURCE=  #可选
#SLS_ERROR_LOG_CHANNEL= #可选[默认-daily]

# 3、使用 Log::info() 方式时需增加配置文件/config/logging.php,channels 中增加 sls,参考文件/vendor/laravel/lumen-framework/config/logging.php,以下方式二选一;

## 3.1 修改 /.env 中 LOG_CHANNEL 为 stack,stack.channels 增加 sls
    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily', 'sls'],  //增加 'sls'
        ],
        ......

        'sls' => [
            'driver' => 'monolog',
            'handler' => Seffeng\LaravelSLS\Handler\SLSHandler::class,
            'level'  => 'debug',
        ],

## 3.2 修改 /.env 中 LOG_CHANNEL 为 ssl,
    'channels' => [
        ......

        'sls' => [
            'driver' => 'daily',
            'level'  => 'debug',
            'path' => storage_path('logs/lumen.log'),
            'tap'  => [Seffeng\LaravelSLS\Logging\SLSFormatter::class],
            'days' => 14,
        ],

目录说明

├─config
│   sls.php
├─src
│  │  SLSLog.php
│  │  SLSServiceProvider.php
│  │  Writer.php
│  ├─Facades
│  │    SLSLog.php
│  │    Writer.php
│  ├─Handler
│  │    SLSHandler.php
│  ├─Helpers
│  │    ArrayHelper.php
│  └─Logging
│       SLSContentFormatter.php
│       SLSFormatter.php
└─tests
    LogTest.php

示例

# 1、如果控制器直接抛出符合 App\Exceptions\Handler->report() 的异常,则仅需配置logging.php,不需额外代码,该方法默认会写 error 日志;$logger->error($e->getMessage(),...)

# 2、App\Exceptions\Handler 在 /bootstrap/app.php 查看。

# 3、是否同时记录本地日志,可自行通过 /config/logging.php 配置。
/**
 * 参考 tests/LogTest.php
 */

use Illuminate\Support\Facades\Log;
use Seffeng\LaravelSLS\Facades\SLSLog;
use Seffeng\LaravelSLS\Facades\Writer;

class SiteController extends Controller
{
    public function index()
    {
        // 使用方式,建议最后一种
        $mode = 2;
        if ($mode === 1) {
            // 仅写到阿里云日志,内容支持多条:[['username' => 'admin', 'action' => 'create user.111'], ['username' => 'admin', 'action' => 'delete user.111']]
            app('sls')->putLogs(['username' => 'admin', 'action' => 'create user.111']);
        } elseif ($mode === 2) {
            // 仅写到阿里云日志,内容支持多条:[['username' => 'admin', 'action' => 'create user.111'], ['username' => 'admin', 'action' => 'delete user.111']]
            SLSLog::putLogs([['username' => 'admin', 'action' => 'create user.111'], ['username' => 'admin', 'action' => 'delete user.111']]);
        } elseif ($mode === 3) {
            // 仅写到阿里云日志
            Writer::info('bbbb', ['user' => 'bbb', 'action' => 'cccccccccccc']);
        } else {
            // 如果不同日志内容需要不同 topic 和 source,请在写日志( Log::info()|SLSLog::putLogs()|app('sls')->putLogs()...)前执行 setTopic、setSource
            // app('sls')->setTopic('topic-new')->setSource('source-new');

            // 写到本地同时写到阿里云日志,需配置 logging,同时 LOG_CHANNEL 为 ssl
            Log::debug('admin create user.333', ['user' => 'bbb', 'action' => 'cccccccccccc']);
        }
    }
}
    

项目依赖

依赖 仓库地址 备注
lokielse/aliyun-open-api-sls https://github.com/AliyunOpenAPI/php-aliyun-open-api-sls

备注

1、测试脚本 tests/LogTest.php 仅作为示例供参考;

2、from lokielse/laravel-sls

空文件

简介

阿里云日志服务扩展 展开 收起
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/timeweekly/times-ext-sls.git
git@gitee.com:timeweekly/times-ext-sls.git
timeweekly
times-ext-sls
times-ext-sls
master

搜索帮助