Skip to content

Commit 69039f7

Browse files
committed
feat: 新增对接 cls 日志
1 parent 0b71fb7 commit 69039f7

32 files changed

Lines changed: 993 additions & 0 deletions

ClsLogger/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
node_modules/
2+
npm-debug.log
3+
*-error.log
4+
*lock*
5+
.temp
6+
.cache
7+
.settings/
8+
.idea
9+
.vscode
10+
.phpunit.result.cache
11+
.DS_Store
12+
dist/
13+
.idea/

ClsLogger/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ClsLogger
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Plugins\ClsLogger\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Routing\Controller;
7+
8+
class SettingController extends Controller
9+
{
10+
public function index(Request $request)
11+
{
12+
// code
13+
$configs = [];
14+
15+
return view('ClsLogger::index', [
16+
'configs' => $configs,
17+
]);
18+
}
19+
20+
public function showSettingView(Request $request)
21+
{
22+
config(['session.same_site' => 'none']);
23+
config(['session.secure' => uniqid()]);
24+
25+
// code
26+
$itemKeys = [
27+
// 'item_key1',
28+
// 'item_key2',
29+
];
30+
31+
// $configs = Config::whereIn('item_key', $itemKeys)->where('item_tag', 'cls_logger')->get();
32+
$configs = [];
33+
34+
return view('ClsLogger::setting', [
35+
'configs' => $configs,
36+
]);
37+
}
38+
39+
public function saveSetting(Request $request)
40+
{
41+
$request->validate([
42+
// 'item_key1' => 'required|url',
43+
// 'item_key2' => 'nullable|url',
44+
]);
45+
46+
$itemKeys = [
47+
// 'item_key1',
48+
// 'item_key2',
49+
];
50+
51+
// code
52+
// Config updateConfigs with $itemKeys and 'cls_logger'
53+
54+
return redirect(route('cls-logger.setting'));
55+
}
56+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Plugins\ClsLogger\Logging;
4+
5+
use Illuminate\Log\Logger;
6+
use Illuminate\Support\Arr;
7+
use Psr\Log\LoggerInterface;
8+
use Psr\Log\InvalidArgumentException;
9+
10+
class ClsLogger extends Logger implements LoggerInterface
11+
{
12+
protected $channelConfig = [];
13+
14+
public function __invoke(array $config)
15+
{
16+
$this->channelConfig = $config;
17+
18+
return $this;
19+
}
20+
21+
public function info($message, array $context = []): void
22+
{
23+
$this->log("INFO", $message, $context);
24+
}
25+
26+
public function log($level, $message, array $context = []): void
27+
{
28+
\Plugins\ClsLogger\Utilties\ClsLogUtiltity::logToCls($level, $message, $context);
29+
}
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Plugins\ClsLogger\Logging;
4+
5+
use Monolog\LogRecord;
6+
use Monolog\Handler\AbstractProcessingHandler;
7+
8+
class ClsLoggerHandler extends AbstractProcessingHandler
9+
{
10+
public function write(LogRecord $record): void
11+
{
12+
\Plugins\ClsLogger\Utilties\ClsLogUtiltity::logToCls($record['level_name'], $record['formatted']);
13+
}
14+
}

ClsLogger/app/Models/.gitkeep

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Plugins\ClsLogger\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class ClsLoggerServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*/
12+
public function register(): void
13+
{
14+
//
15+
}
16+
17+
/**
18+
* Bootstrap services.
19+
*/
20+
public function boot(): void
21+
{
22+
$channels = config('logging.channels');
23+
$channels['cls'] = [
24+
'driver' => 'monolog',
25+
'handler' => \Plugins\ClsLogger\Logging\ClsLoggerHandler::class,
26+
27+
// or
28+
29+
// 'driver' => 'custom',
30+
// 'via' => \Plugins\ClsLogger\Logging\ClsLogger::class,
31+
];
32+
33+
config(['logging.channels' => $channels]);
34+
}
35+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* Fresns (https://fresns.org)
5+
* Copyright (C) 2021-Present Jevan Tang
6+
* Released under the Apache-2.0 License.
7+
*/
8+
9+
namespace Plugins\ClsLogger\Providers;
10+
11+
use Illuminate\Support\Arr;
12+
use Illuminate\Support\Facades\File;
13+
use Illuminate\Support\Str;
14+
use Illuminate\Support\ServiceProvider;
15+
use Symfony\Component\Finder\Finder;
16+
17+
class CommandServiceProvider extends ServiceProvider
18+
{
19+
/**
20+
* Bootstrap services.
21+
*/
22+
public function boot(): void
23+
{
24+
$commandsDirectory = dirname(__DIR__) . '/Console/Commands';
25+
if (File::exists($commandsDirectory)) {
26+
$this->load($commandsDirectory);
27+
}
28+
}
29+
30+
/**
31+
* Register all of the commands in the given directory.
32+
*
33+
* @param array|string $paths
34+
*/
35+
protected function load($paths): void
36+
{
37+
$paths = array_unique(Arr::wrap($paths));
38+
39+
$paths = array_filter($paths, function ($path) {
40+
return is_dir($path);
41+
});
42+
43+
if (empty($paths)) {
44+
return;
45+
}
46+
47+
$commands = [];
48+
foreach ((new Finder)->in($paths)->files() as $command) {
49+
$commandClass = Str::before(self::class, 'Providers\\') . 'Console\\Commands\\' . str_replace('.php', '', $command->getBasename());
50+
if (class_exists($commandClass)) {
51+
$commands[] = $commandClass;
52+
}
53+
}
54+
55+
$this->commands($commands);
56+
}
57+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* Fresns (https://fresns.org)
5+
* Copyright (C) 2021-Present Jevan Tang
6+
* Released under the Apache-2.0 License.
7+
*/
8+
9+
namespace Plugins\ClsLogger\Providers;
10+
11+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
12+
13+
class PluginServiceProvider extends BaseServiceProvider
14+
{
15+
/**
16+
* Boot the application events.
17+
*/
18+
public function boot(): void
19+
{
20+
$this->registerTranslations();
21+
$this->registerConfig();
22+
$this->registerViews();
23+
24+
$this->loadMigrationsFrom(dirname(__DIR__, 2) . '/database/migrations');
25+
26+
// Event::listen(UserCreated::class, UserCreatedListener::class);
27+
}
28+
29+
/**
30+
* Register the service provider.
31+
*/
32+
public function register(): void
33+
{
34+
// if ($this->app->runningInConsole()) {
35+
$this->app->register(CommandServiceProvider::class);
36+
// }
37+
}
38+
39+
/**
40+
* Register config.
41+
*/
42+
protected function registerConfig(): void
43+
{
44+
$this->mergeConfigFrom(
45+
dirname(__DIR__, 2) . '/config/cls-logger.php', 'cls-logger'
46+
);
47+
}
48+
49+
/**
50+
* Register views.
51+
*/
52+
public function registerViews(): void
53+
{
54+
$this->loadViewsFrom(dirname(__DIR__, 2) . '/resources/views', 'ClsLogger');
55+
}
56+
57+
/**
58+
* Register translations.
59+
*/
60+
public function registerTranslations(): void
61+
{
62+
$this->loadTranslationsFrom(dirname(__DIR__, 2) . '/resources/lang', 'ClsLogger');
63+
}
64+
65+
/**
66+
* Get the services provided by the provider.
67+
*/
68+
public function provides(): array
69+
{
70+
return [];
71+
}
72+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* Fresns (https://fresns.org)
5+
* Copyright (C) 2021-Present Jevan Tang
6+
* Released under the Apache-2.0 License.
7+
*/
8+
9+
namespace Plugins\ClsLogger\Providers;
10+
11+
use Fresns\MarketManager\Models\Plugin;
12+
use Illuminate\Support\Facades\Cache;
13+
use Illuminate\Support\Facades\Route;
14+
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as BaseServiceProvider;
15+
16+
class RouteServiceProvider extends BaseServiceProvider
17+
{
18+
/**
19+
* Called before routes are registered.
20+
*
21+
* Register any model bindings or pattern based filters.
22+
*/
23+
public function boot(): void
24+
{
25+
parent::boot();
26+
}
27+
28+
/**
29+
* Define the routes for the application.
30+
*/
31+
public function map(): void
32+
{
33+
$host = null;
34+
35+
// try {
36+
// if (class_exists(Plugin::class)) {
37+
// $fskey = 'ClsLogger';
38+
// $cacheKey = "{$fskey}_model";
39+
40+
// $pluginModel = Cache::get($cacheKey);
41+
// if (empty($pluginModel)) {
42+
// $pluginModel = Plugin::withTrashed()->where('fskey', $fskey)->first();
43+
44+
// Cache::put($cacheKey, $pluginModel, now()->addMinutes(30));
45+
// }
46+
47+
// $pluginHost = $pluginModel?->plugin_host ?? '';
48+
49+
// $host = str_replace(['http://', 'https://'], '', rtrim($pluginHost, '/'));
50+
// }
51+
// } catch (\Throwable $e) {
52+
// info("get plugin host failed: " . $e->getMessage());
53+
// }
54+
55+
Route::group([
56+
'domain' => $host,
57+
], function () {
58+
$this->mapApiRoutes();
59+
60+
$this->mapWebRoutes();
61+
});
62+
}
63+
64+
/**
65+
* Define the "web" routes for the application.
66+
*
67+
* These routes all receive session state, CSRF protection, etc.
68+
*/
69+
protected function mapWebRoutes(): void
70+
{
71+
Route::middleware('web')->group(dirname(__DIR__, 2) . '/routes/web.php');
72+
}
73+
74+
/**
75+
* Define the "api" routes for the application.
76+
*
77+
* These routes are typically stateless.
78+
*/
79+
protected function mapApiRoutes(): void
80+
{
81+
Route::prefix('api')->name('api.')->middleware('api')->group(dirname(__DIR__, 2) . '/routes/api.php');
82+
}
83+
}

0 commit comments

Comments
 (0)