Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
b711b707
Commit
b711b707
authored
Nov 18, 2011
by
Igor Wiedler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[MonologServiceProvider] improve logging, log reponse status, improve test granularity
parent
20eeadbc
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
13 deletions
+48
-13
src/Silex/Provider/MonologServiceProvider.php
src/Silex/Provider/MonologServiceProvider.php
+9
-7
tests/Silex/Tests/Provider/MonologServiceProviderTest.php
tests/Silex/Tests/Provider/MonologServiceProviderTest.php
+39
-6
No files found.
src/Silex/Provider/MonologServiceProvider.php
View file @
b711b707
...
@@ -17,6 +17,8 @@ use Monolog\Handler\StreamHandler;
...
@@ -17,6 +17,8 @@ use Monolog\Handler\StreamHandler;
use
Silex\Application
;
use
Silex\Application
;
use
Silex\ServiceProviderInterface
;
use
Silex\ServiceProviderInterface
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpFoundation\Response
;
use
Symfony\Component\HttpKernel\Exception\HttpException
;
use
Symfony\Component\HttpKernel\Exception\HttpException
;
/**
/**
...
@@ -54,16 +56,16 @@ class MonologServiceProvider implements ServiceProviderInterface
...
@@ -54,16 +56,16 @@ class MonologServiceProvider implements ServiceProviderInterface
$app
[
'autoloader'
]
->
registerNamespace
(
'Monolog'
,
$app
[
'monolog.class_path'
]);
$app
[
'autoloader'
]
->
registerNamespace
(
'Monolog'
,
$app
[
'monolog.class_path'
]);
}
}
$app
->
before
(
function
()
use
(
$app
)
{
$app
->
before
(
function
(
Request
$request
)
use
(
$app
)
{
$app
[
'monolog'
]
->
addInfo
(
$app
[
'request'
]
->
getMethod
()
.
' '
.
$app
[
'request'
]
->
getRequestUri
());
$app
[
'monolog'
]
->
addInfo
(
'> '
.
$request
->
getMethod
()
.
' '
.
$request
->
getRequestUri
());
});
});
$app
->
error
(
function
(
\Exception
$e
)
use
(
$app
)
{
$app
->
error
(
function
(
\Exception
$e
)
use
(
$app
)
{
if
(
$e
instanceof
HttpException
)
{
$app
[
'monolog'
]
->
addWarning
(
$e
->
getStatusCode
()
.
' '
.
$app
[
'request'
]
->
getMethod
()
.
' '
.
$app
[
'request'
]
->
getRequestUri
());
}
else
{
$app
[
'monolog'
]
->
addError
(
$e
->
getMessage
());
$app
[
'monolog'
]
->
addError
(
$e
->
getMessage
());
}
});
$app
->
after
(
function
(
Request
$request
,
Response
$response
)
use
(
$app
)
{
$app
[
'monolog'
]
->
addInfo
(
'< '
.
$response
->
getStatusCode
());
});
});
}
}
}
}
tests/Silex/Tests/Provider/MonologServiceProviderTest.php
View file @
b711b707
...
@@ -32,7 +32,7 @@ class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -32,7 +32,7 @@ class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase
}
}
}
}
public
function
testRegister
AndRender
()
public
function
testRegister
()
{
{
$app
=
new
Application
();
$app
=
new
Application
();
...
@@ -44,10 +44,48 @@ class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -44,10 +44,48 @@ class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase
return
new
TestHandler
(
$app
[
'monolog.level'
]);
return
new
TestHandler
(
$app
[
'monolog.level'
]);
});
});
return
$app
;
}
/**
* @depends testRegister
*/
public
function
testRequestLogging
(
$app
)
{
$app
->
get
(
'/foo'
,
function
()
use
(
$app
)
{
return
'foo'
;
});
$this
->
assertFalse
(
$app
[
'monolog.handler'
]
->
hasInfoRecords
());
$request
=
Request
::
create
(
'/foo'
);
$app
->
handle
(
$request
);
$this
->
assertTrue
(
$app
[
'monolog.handler'
]
->
hasInfoRecords
());
}
/**
* @depends testRegister
*/
public
function
testManualLogging
(
$app
)
{
$app
->
get
(
'/log'
,
function
()
use
(
$app
)
{
$app
->
get
(
'/log'
,
function
()
use
(
$app
)
{
$app
[
'monolog'
]
->
addDebug
(
'logging a message'
);
$app
[
'monolog'
]
->
addDebug
(
'logging a message'
);
});
});
$this
->
assertFalse
(
$app
[
'monolog.handler'
]
->
hasDebugRecords
());
$request
=
Request
::
create
(
'/log'
);
$app
->
handle
(
$request
);
$this
->
assertTrue
(
$app
[
'monolog.handler'
]
->
hasDebugRecords
());
}
/**
* @depends testRegister
*/
public
function
testErrorLogging
(
$app
)
{
$app
->
get
(
'/error'
,
function
()
{
$app
->
get
(
'/error'
,
function
()
{
throw
new
\RuntimeException
(
'very bad error'
);
throw
new
\RuntimeException
(
'very bad error'
);
});
});
...
@@ -56,16 +94,11 @@ class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -56,16 +94,11 @@ class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase
return
'error handled'
;
return
'error handled'
;
});
});
$this
->
assertFalse
(
$app
[
'monolog.handler'
]
->
hasDebugRecords
());
$this
->
assertFalse
(
$app
[
'monolog.handler'
]
->
hasErrorRecords
());
$this
->
assertFalse
(
$app
[
'monolog.handler'
]
->
hasErrorRecords
());
$request
=
Request
::
create
(
'/log'
);
$app
->
handle
(
$request
);
$request
=
Request
::
create
(
'/error'
);
$request
=
Request
::
create
(
'/error'
);
$app
->
handle
(
$request
);
$app
->
handle
(
$request
);
$this
->
assertTrue
(
$app
[
'monolog.handler'
]
->
hasDebugRecords
());
$this
->
assertTrue
(
$app
[
'monolog.handler'
]
->
hasErrorRecords
());
$this
->
assertTrue
(
$app
[
'monolog.handler'
]
->
hasErrorRecords
());
}
}
}
}
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