Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
php_utils
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
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
bp
php_utils
Commits
98d1aea6
Commit
98d1aea6
authored
Aug 03, 2021
by
wanjilong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add: 日志增加traceId
parent
26abffd9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
171 additions
and
3 deletions
+171
-3
FileLog.php
src/Log/FileLog.php
+7
-3
Tracer.php
src/Log/Tracer.php
+164
-0
No files found.
src/Log/FileLog.php
View file @
98d1aea6
...
...
@@ -3,6 +3,7 @@
namespace
Api\PhpUtils\Log
;
use
Api\PhpUtils\Cache\ApcuUtil
;
use
Api\PhpUtils\Log\Tracer
;
use
Api\PhpUtils\Message\Email
;
class
FileLog
...
...
@@ -17,7 +18,8 @@ class FileLog
*/
public
static
function
info
(
$signature
,
$detail_info
=
''
,
$with_access_log
=
false
)
{
$log
=
'PHP User_info: ['
.
$signature
.
']'
.
' [detail info:] '
.
$detail_info
;
$traceId
=
Tracer
::
getTraceId
();
$log
=
'PHP User_info: ['
.
$signature
.
'] [traceId:'
.
$traceId
.
'] [detail info:] '
.
$detail_info
;
if
(
$with_access_log
)
{
$log
.=
' [request info:] '
.
self
::
accessLog
();
}
...
...
@@ -33,7 +35,8 @@ class FileLog
*/
public
static
function
waring
(
$signature
,
$detail_info
=
''
,
$exception
=
null
)
{
$log
=
'PHP User_warning: ['
.
$signature
.
']'
.
' [detail info:] '
.
$detail_info
.
' [request info:] '
.
self
::
accessLog
();
$traceId
=
Tracer
::
getTraceId
();
$log
=
'PHP User_warning: ['
.
$signature
.
'] [traceId:'
.
$traceId
.
'] [detail info:] '
.
$detail_info
.
' [request info:] '
.
self
::
accessLog
();
if
(
$exception
!=
''
)
{
$exception_info
=
'In '
.
$exception
->
getFile
()
.
' on line '
.
$exception
->
getLine
()
.
' '
.
$exception
->
getCode
()
.
': '
.
$exception
->
getMessage
();
$log
.=
' [exception info: ]'
.
$exception_info
;
...
...
@@ -51,7 +54,8 @@ class FileLog
*/
public
static
function
error
(
$signature
,
$detail_info
=
''
,
$exception
=
null
,
$mail_to
=
''
)
{
$log
=
'PHP User_error: ['
.
$signature
.
']'
.
' [detail info:] '
.
$detail_info
.
' [request info:] '
.
self
::
accessLog
();
$traceId
=
Tracer
::
getTraceId
();
$log
=
'PHP User_error: ['
.
$signature
.
'] [traceId:'
.
$traceId
.
'] [detail info:] '
.
$detail_info
.
' [request info:] '
.
self
::
accessLog
();
if
(
$exception
!=
''
)
{
$exception_info
=
' [exception info: ] In '
.
$exception
->
getFile
()
.
' on line '
.
$exception
->
getLine
()
.
' '
.
$exception
->
getCode
()
.
': '
.
$exception
->
getMessage
();
}
else
{
...
...
src/Log/Tracer.php
0 → 100644
View file @
98d1aea6
<?php
namespace
Api\PhpUtils\Log
;
class
Tracer
{
const
DEBUG
=
1
;
const
INFO
=
2
;
const
WARNING
=
3
;
const
ERROR
=
4
;
protected
static
$traceId
=
''
;
protected
static
$spanId
=
''
;
/**
* http 请求日志
*/
public
static
function
request
()
{
$tag
=
'request'
;
$message
=
'request log'
;
$content
=
[
'uri'
=>
''
,
'get'
=>
[],
'post'
=>
[],
];
//__METHOD__
self
::
info
(
$message
,
$content
,
$tag
);
}
/**
* @param $ret
*/
public
static
function
response
(
$ret
)
{
$tag
=
'response'
;
$message
=
'response log'
;
$content
=
[
'ret'
=>
$ret
,
'requestId'
=>
self
::
getTraceId
(),
];
self
::
info
(
$message
,
$content
,
$tag
);
}
public
static
function
daemon
()
{
$tag
=
'daemon'
;
$message
=
'daemon log'
;
$content
=
[
'pid'
=>
posix_getpid
(),
];
self
::
info
(
$message
,
$content
,
$tag
);
}
/**
* @param $message
* @param array $content
* @param string $tag
*/
public
static
function
debug
(
$message
,
$content
=
[],
$tag
=
''
){
self
::
log
(
self
::
DEBUG
,
$message
,
$content
,
$tag
);
}
public
static
function
info
(
$message
,
$content
=
[],
$tag
=
''
){
self
::
log
(
self
::
INFO
,
$message
,
$content
,
$tag
);
}
public
static
function
warning
(
$message
,
$content
=
[],
$tag
=
''
)
{
self
::
log
(
self
::
WARNING
,
$message
,
$content
,
$tag
);
}
public
static
function
error
(
$message
,
$content
=
[],
$tag
=
''
)
{
self
::
log
(
self
::
ERROR
,
$message
,
$content
,
$tag
);
}
public
static
function
log
(
$level
,
$message
,
$content
,
$tag
)
{
if
(
!
defined
(
'LOG_PATH'
))
{
define
(
'LOG_PATH'
,
ROOT_PATH
.
'/logs/'
);
}
switch
(
$level
)
{
case
self
::
DEBUG
:
case
self
::
INFO
:
case
self
::
WARNING
:
case
self
::
ERROR
:
$file
=
LOG_PATH
.
'log_'
.
date
(
'Y-m-d'
)
.
'.log'
;
break
;
}
$dateTime
=
new
\DateTime
();
$log
=
[
'timestamp'
=>
$dateTime
->
format
(
\DateTime
::
RFC3339_EXTENDED
),
'traceId'
=>
self
::
getTraceId
(),
'spanId'
=>
self
::
getSpanId
(),
'tag'
=>
$tag
,
'content'
=>
$content
,
'message'
=>
$message
];
//$_SERVER['HTTP_X_FORWARDED_FOR']
$string
=
json_encode
(
$log
,
JSON_UNESCAPED_UNICODE
)
.
"
\n
"
;
if
(
$fd
=
@
fopen
(
$file
,
'a'
))
{
fputs
(
$fd
,
$string
);
fclose
(
$fd
);
}
}
/**
* @return false|mixed|string
* 获取traceId
*/
public
static
function
getTraceId
()
{
if
(
empty
(
self
::
$traceId
))
{
if
(
!
empty
(
$_SERVER
[
'HTTP_X_TRACE_ID'
]))
{
self
::
$traceId
=
$_SERVER
[
'HTTP_X_TRACE_ID'
];
}
elseif
(
!
empty
(
$_SERVER
[
'HTTP_X_REQUEST_ID'
]))
{
self
::
$traceId
=
$_SERVER
[
'HTTP_X_REQUEST_ID'
];
}
else
{
self
::
$traceId
=
self
::
genUniqId
();
}
}
return
self
::
$traceId
;
}
/**
* @return false|string
* 获取spanId
*/
public
static
function
getSpanId
()
{
if
(
empty
(
self
::
$spanId
))
{
self
::
$spanId
=
self
::
genUniqId
();
}
return
self
::
$spanId
;
}
/**
* @return false|string
* @throws \Exception
* 获取随机串用于记录traceId
*/
public
static
function
genUniqId
()
{
if
(
function_exists
(
"random_bytes"
))
{
$bytes
=
random_bytes
(
ceil
(
8
));
return
substr
(
bin2hex
(
$bytes
),
0
,
16
);
}
elseif
(
function_exists
(
"openssl_random_pseudo_bytes"
))
{
$bytes
=
openssl_random_pseudo_bytes
(
ceil
(
8
));
return
substr
(
bin2hex
(
$bytes
),
0
,
16
);
}
else
{
$pre
=
rand
(
1
,
4095
);
return
uniqid
(
sprintf
(
"%03x"
,
$pre
));
}
}
}
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