Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-micro
Commits
08f91cde
Commit
08f91cde
authored
Jun 10, 2021
by
王源
🎧
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' into 2.0
parents
ccbad769
fea571fc
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
96 additions
and
0 deletions
+96
-0
Log.php
src/Tools/Log.php
+96
-0
No files found.
src/Tools/Log.php
0 → 100644
View file @
08f91cde
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2021/06/09
* Time: 10:14:32
*/
namespace
Meibuyu\Micro\Tools
;
use
Hyperf\Logger\Logger
;
use
Monolog\Formatter\FormatterInterface
;
use
Monolog\Formatter\LineFormatter
;
use
Monolog\Handler\FormattableHandlerInterface
;
use
Monolog\Handler\HandlerInterface
;
use
Monolog\Handler\RotatingFileHandler
;
use
Psr\Log\LoggerInterface
;
/**
* Class Log
* @package App\Services
* @method static emergency($message, string $name = 'app')
* @method static alert($message, string $name = 'app')
* @method static critical($message, string $name = 'app')
* @method static error($message, string $name = 'app')
* @method static warning($message, string $name = 'app')
* @method static notice($message, string $name = 'app')
* @method static info($message, string $name = 'app')
* @method static debug($message, string $name = 'app')
*/
class
Log
{
/**
* @var LoggerInterface
*/
protected
static
$logs
;
/**
* @return LoggerInterface
*/
public
static
function
log
(
$channel
=
'app'
)
{
if
(
!
isset
(
self
::
$logs
[
$channel
]))
{
self
::
$logs
[
$channel
]
=
make
(
Logger
::
class
,
[
'name'
=>
$channel
,
'handlers'
=>
self
::
handlers
(
$channel
),
]);
}
return
self
::
$logs
[
$channel
];
}
protected
static
function
handlers
(
$channel
)
:
array
{
$handlerConfigs
=
[
[
'class'
=>
RotatingFileHandler
::
class
,
'constructor'
=>
[
'filename'
=>
BASE_PATH
.
"/runtime/logs/
$channel
.log"
,
'level'
=>
Logger
::
DEBUG
,
],
'formatter'
=>
[
'class'
=>
LineFormatter
::
class
,
'constructor'
=>
[
'format'
=>
"[%datetime%] %channel%.%level_name%: %message%
\n
"
,
'dateFormat'
=>
'Y-m-d H:i:s'
,
'allowInlineLineBreaks'
=>
true
,
],
],
],
];
$handlers
=
[];
foreach
(
$handlerConfigs
as
$value
)
{
$formatterConfig
=
$value
[
'formatter'
];
/** @var HandlerInterface $handler */
$handler
=
make
(
$value
[
'class'
],
$value
[
'constructor'
]);
if
(
$handler
instanceof
FormattableHandlerInterface
)
{
$formatterClass
=
$formatterConfig
[
'class'
];
$formatterConstructor
=
$formatterConfig
[
'constructor'
];
/** @var FormatterInterface $formatter */
$formatter
=
make
(
$formatterClass
,
$formatterConstructor
);
$handler
->
setFormatter
(
$formatter
);
}
$handlers
[]
=
$handler
;
}
return
$handlers
;
}
public
static
function
__callStatic
(
$name
,
$arguments
)
{
$msg
=
$arguments
[
0
];
$channel
=
$arguments
[
1
];
return
static
::
log
(
$channel
)
->
{
$name
}(
$msg
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment