Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
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
PSDI Army
yii2
Commits
c6a946ec
Commit
c6a946ec
authored
Mar 16, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #497: Removed `\yii\log\Target::logUser` and added…
Fixes #497: Removed `\yii\log\Target::logUser` and added `\yii\log\Target::prefix` to support customizing message prefix
parent
f61734d4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
12 deletions
+30
-12
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
Target.php
framework/log/Target.php
+29
-12
No files found.
framework/CHANGELOG.md
View file @
c6a946ec
...
...
@@ -73,6 +73,7 @@ Yii Framework 2 Change Log
-
Bug:
`Query::queryScalar`
wasn't making
`SELECT DISTINCT`
queries subqueries (jom)
-
Enh #46: Added Image extension based on
[
Imagine library
](
http://imagine.readthedocs.org
)
(
tonydspaniard
)
-
Enh #364: Improve Inflector::slug with
`intl`
transliteration. Improved transliteration char map. (tonydspaniard)
-
Enh #497: Removed
`\yii\log\Target::logUser`
and added
`\yii\log\Target::prefix`
to support customizing message prefix (qiangxue)
-
Enh #797: Added support for validating multiple columns by
`UniqueValidator`
and
`ExistValidator`
(qiangxue)
-
Enh #802: Added support for retrieving sub-array element or child object property through
`ArrayHelper::getValue()`
(qiangxue, cebe)
-
Enh #938: Added
`yii\web\View::renderAjax()`
and
`yii\web\Controller::renderAjax()`
(qiangxue)
...
...
framework/log/Target.php
View file @
c6a946ec
...
...
@@ -10,6 +10,7 @@ namespace yii\log;
use
Yii
;
use
yii\base\Component
;
use
yii\base\InvalidConfigException
;
use
yii\web\Request
;
/**
* Target is the base class for all log target classes.
...
...
@@ -52,17 +53,18 @@ abstract class Target extends Component
*/
public
$except
=
[];
/**
* @var boolean whether to log a message containing the current user name and ID. Defaults to false.
* @see \yii\web\User
*/
public
$logUser
=
false
;
/**
* @var array list of the PHP predefined variables that should be logged in a message.
* Note that a variable must be accessible via `$GLOBALS`. Otherwise it won't be logged.
* Defaults to `['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']`.
*/
public
$logVars
=
[
'_GET'
,
'_POST'
,
'_FILES'
,
'_COOKIE'
,
'_SESSION'
,
'_SERVER'
];
/**
* @var callable a PHP callable that returns a string to be prefix to every exported message.
* If not set, [[getMessagePrefix()]] will be used, which prefixes user IP, user ID and session ID
* to every message. The signature of the callable should be `function ($message)`.
*/
public
$prefix
;
/**
* @var integer how many messages should be accumulated before they are exported.
* Defaults to 1000. Note that messages will always be exported when the application terminates.
* Set this property to be 0 if you don't want to export messages until the application terminates.
...
...
@@ -111,11 +113,6 @@ abstract class Target extends Component
protected
function
getContextMessage
()
{
$context
=
[];
if
(
$this
->
logUser
&&
(
$user
=
Yii
::
$app
->
getComponent
(
'user'
,
false
))
!==
null
)
{
/** @var \yii\web\User $user */
$context
[]
=
'User: '
.
$user
->
getId
();
}
foreach
(
$this
->
logVars
as
$name
)
{
if
(
!
empty
(
$GLOBALS
[
$name
]))
{
$context
[]
=
"
\$
{
$name
}
= "
.
var_export
(
$GLOBALS
[
$name
],
true
);
...
...
@@ -233,8 +230,28 @@ abstract class Target extends Component
if
(
!
is_string
(
$text
))
{
$text
=
var_export
(
$text
,
true
);
}
$ip
=
isset
(
$_SERVER
[
'REMOTE_ADDR'
])
?
$_SERVER
[
'REMOTE_ADDR'
]
:
'127.0.0.1'
;
return
date
(
'Y/m/d H:i:s'
,
$timestamp
)
.
" [
$ip
] [
$level
] [
$category
]
$text
"
;
$prefix
=
$this
->
prefix
?
call_user_func
(
$this
->
prefix
,
$message
)
:
$this
->
getMessagePrefix
(
$message
);
return
date
(
'Y/m/d H:i:s'
,
$timestamp
)
.
"
$prefix
[
$level
] [
$category
]
$text
"
;
}
/**
* Returns a string to be prefixed to the given message.
* The default implementation will return user IP, user ID and session ID as a prefix.
* @param array $message the message being exported
* @return string the prefix string
*/
public
function
getMessagePrefix
(
$message
)
{
$request
=
Yii
::
$app
->
getRequest
();
$ip
=
$request
instanceof
Request
?
$request
->
getUserIP
()
:
'-'
;
/** @var \yii\web\User $user */
$user
=
Yii
::
$app
->
getComponent
(
'user'
,
false
);
$userID
=
$user
?
$user
->
getId
(
false
)
:
'-'
;
/** @var \yii\web\Session $session */
$session
=
Yii
::
$app
->
getComponent
(
'session'
,
false
);
$sessionID
=
$session
&&
$session
->
getIsActive
()
?
$session
->
getId
()
:
'-'
;
return
"[
$ip
] [
$userID
] [
$sessionID
]"
;
}
}
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