-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Let monolog support handle aliyun sls log
- Loading branch information
0 parents
commit fbcc213
Showing
13 changed files
with
525 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
ALI_LOG_NAME= | ||
ALI_LOG_ENDPOINT= | ||
ALI_LOG_ACCESS_KEY_ID= | ||
ALI_LOG_ACCESS_KEY= | ||
ALI_LOG_PROJECT= | ||
ALI_LOG_LOGSTORE= | ||
ALI_SLS_LOG_TOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.buildpath | ||
.settings/ | ||
.project | ||
*.patch | ||
.idea/ | ||
.git/ | ||
runtime/ | ||
vendor/ | ||
.env | ||
.DS_Store | ||
*.lock | ||
*.cache | ||
.bashrc | ||
*.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
return (new PhpCsFixer\Config()) | ||
->setRiskyAllowed(true) | ||
->setRules([ | ||
'@PSR2' => true, | ||
'@Symfony' => true, | ||
'@DoctrineAnnotation' => true, | ||
'@PhpCsFixer' => true, | ||
'header_comment' => [ | ||
'comment_type' => 'PHPDoc', | ||
'separate' => 'none', | ||
'location' => 'after_declare_strict', | ||
], | ||
'array_syntax' => [ | ||
'syntax' => 'short' | ||
], | ||
'list_syntax' => [ | ||
'syntax' => 'short' | ||
], | ||
'concat_space' => [ | ||
'spacing' => 'one' | ||
], | ||
'blank_line_before_statement' => [ | ||
'statements' => [ | ||
'declare', | ||
], | ||
], | ||
'general_phpdoc_annotation_remove' => [ | ||
'annotations' => [ | ||
'author' | ||
], | ||
], | ||
'ordered_imports' => [ | ||
'imports_order' => [ | ||
'class', 'function', 'const', | ||
], | ||
'sort_algorithm' => 'alpha', | ||
], | ||
'single_line_comment_style' => [ | ||
'comment_types' => [ | ||
], | ||
], | ||
'yoda_style' => [ | ||
'always_move_variable' => false, | ||
'equal' => false, | ||
'identical' => false, | ||
], | ||
'phpdoc_align' => [ | ||
'align' => 'left', | ||
], | ||
'multiline_whitespace_before_semicolons' => [ | ||
'strategy' => 'no_multi_line', | ||
], | ||
'constant_case' => [ | ||
'case' => 'lower', | ||
], | ||
'global_namespace_import' => [ | ||
'import_classes' => true, | ||
'import_constants' => true, | ||
'import_functions' => null, | ||
], | ||
'class_attributes_separation' => true, | ||
'combine_consecutive_unsets' => true, | ||
'declare_strict_types' => true, | ||
'linebreak_after_opening_tag' => true, | ||
'lowercase_static_reference' => true, | ||
'no_useless_else' => true, | ||
'no_unused_imports' => true, | ||
'not_operator_with_successor_space' => true, | ||
'not_operator_with_space' => false, | ||
'ordered_class_elements' => true, | ||
'php_unit_strict' => false, | ||
'phpdoc_separation' => false, | ||
'single_quote' => true, | ||
'standardize_not_equals' => true, | ||
'multiline_comment_opening_closing' => true, | ||
]) | ||
->setFinder( | ||
PhpCsFixer\Finder::create() | ||
->exclude('bin') | ||
->exclude('public') | ||
->exclude('runtime') | ||
->exclude('vendor') | ||
->in(__DIR__) | ||
) | ||
->setUsingCache(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Monolog Aliyu Log | ||
## Installation | ||
|
||
Install the latest version with | ||
|
||
```bash | ||
$ composer require ydg/monolog-aliyun-log | ||
``` | ||
|
||
## Basic Usage | ||
|
||
```php | ||
<?php | ||
|
||
use Monolog\Logger; | ||
use Hdk\MonologAliyunLog\AliyunLogHelper; | ||
|
||
$config = [ | ||
'endpoint' => 'your endpoint', | ||
'accessKeyId' => 'your accessKeyId', | ||
'accessKey' => 'your accessKey', | ||
'project' => 'your project', | ||
'logstore' => 'your logstore', | ||
]; | ||
|
||
$log = new Logger('name'); | ||
$log->pushHandler(AliyunLogHelper::getLogHandler(AliyunLogHelper::getLogClient($config), $config)); | ||
|
||
// add records to the log | ||
$log->warning('Foo'); | ||
$log->error('Bar'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Dotenv\Dotenv; | ||
use Dotenv\Repository\Adapter; | ||
use Dotenv\Repository\RepositoryBuilder; | ||
|
||
!defined('BASE_PATH') && define('BASE_PATH', __DIR__); | ||
|
||
require_once BASE_PATH . '/vendor/autoload.php'; | ||
|
||
if (file_exists(BASE_PATH . '/.env')) { | ||
$repository = RepositoryBuilder::createWithNoAdapters() | ||
->addAdapter(Adapter\PutenvAdapter::class) | ||
->immutable() | ||
->make(); | ||
|
||
Dotenv::create($repository, [BASE_PATH])->load(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "ydg/monolog-aliyun-log", | ||
"description": "Let monolog support aliyun log.", | ||
"license": "MIT", | ||
"keywords": [ | ||
"php", | ||
"log", | ||
"aliyun" | ||
], | ||
"require": { | ||
"php": ">=7.2", | ||
"monolog/monolog": "^1.24 || ^2.0", | ||
"alibabacloud/aliyun-log-php-sdk": "^0.6.3" | ||
}, | ||
"require-dev": { | ||
"friendsofphp/php-cs-fixer": "^3.12", | ||
"phpunit/phpunit": "^9.5", | ||
"vlucas/phpdotenv": "^5.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Ydg\\MonologAliyunLog\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"YdgTest\\MonologAliyunLog\\": "tests/" | ||
}, | ||
"files": [ | ||
"tests/functions.php" | ||
] | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"scripts": { | ||
"test": "phpunit", | ||
"cs-fix": "php-cs-fixer fix $1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="./bootstrap.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Tests"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Ydg\MonologAliyunLog; | ||
|
||
use Aliyun_Log_Client as Client; | ||
use Monolog\Logger; | ||
use Ydg\MonologAliyunLog\Handler\AliyunLogHandler; | ||
|
||
class AliyunLogHelper | ||
{ | ||
private static $instance; | ||
private $logger; | ||
|
||
static function instance(...$args) | ||
{ | ||
if (!isset(self::$instance)) { | ||
self::$instance = new static(...$args); | ||
} | ||
return self::$instance; | ||
} | ||
|
||
public function getLogger(array $config): Logger | ||
{ | ||
if (!$this->logger instanceof Logger) { | ||
$this->logger = new Logger($config['name']); | ||
} | ||
$this->logger->pushHandler(AliyunLogHelper::getLogHandler(AliyunLogHelper::getLogClient($config), $config)); | ||
|
||
return $this->logger; | ||
} | ||
|
||
public static function getLogHandler($client, $config): AliyunLogHandler | ||
{ | ||
return new AliyunLogHandler($client, $config['project'], $config['logstore']); | ||
} | ||
|
||
public static function getLogClient($config): Client | ||
{ | ||
return new Client($config['endpoint'], $config['accessKeyId'], $config['accessKey'], $config['token'] ?? ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ydg\MonologAliyunLog\Formatter; | ||
|
||
use Monolog\Formatter\NormalizerFormatter; | ||
|
||
class AliyunLogFormatter extends NormalizerFormatter | ||
{ | ||
const SIMPLE_DATE = "Y-m-d\TH:i:sP"; | ||
|
||
public function format(array $record) | ||
{ | ||
$record = parent::format($record); | ||
|
||
foreach ($record as &$value) { | ||
$value = $this->formatString($value); | ||
} | ||
|
||
return $record; | ||
} | ||
|
||
public function formatString($value) | ||
{ | ||
if (is_array($value)) { | ||
return $this->toJson($value); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
Oops, something went wrong.