forked from php-tmdb/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.php
68 lines (59 loc) · 2.18 KB
/
logging.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 4.0.0
*/
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LogLevel;
use Tmdb\Event\BeforeHydrationEvent;
use Tmdb\Event\BeforeRequestEvent;
use Tmdb\Event\HttpClientExceptionEvent;
use Tmdb\Event\Listener\Logger\LogApiErrorListener;
use Tmdb\Event\Listener\Logger\LogHttpMessageListener;
use Tmdb\Event\Listener\Logger\LogHydrationListener;
use Tmdb\Event\ResponseEvent;
use Tmdb\Event\TmdbExceptionEvent;
use Tmdb\Formatter\HttpMessage\SimpleHttpMessageFormatter;
use Tmdb\Formatter\Hydration\SimpleHydrationFormatter;
use Tmdb\Formatter\TmdbApiException\SimpleTmdbApiExceptionFormatter;
use Tmdb\Repository\MovieRepository;
require_once '../vendor/autoload.php';
require_once 'apikey.php';
/** @var Tmdb\Client $client * */
$client = require_once('setup-client.php');
$ed = $client->getEventDispatcher();
$logger = new Logger(
'php-tmdb',
[
new StreamHandler(__DIR__ . '/var/log/php-tmdb.log', LogLevel::DEBUG)
]
);
// Optional for logging, you can also omit events you do not wish to be logged.
$requestLoggerListener = new LogHttpMessageListener(
$logger,
new SimpleHttpMessageFormatter()
);
$hydrationLoggerListener = new LogHydrationListener(
$logger,
new SimpleHydrationFormatter(),
true // set to true if you wish to add the json data passed for each hydration, do not use this on production
);
$apiErrorListener = new LogApiErrorListener(
$logger,
new SimpleTmdbApiExceptionFormatter()
);
$ed->addListener(BeforeRequestEvent::class, $requestLoggerListener);
$ed->addListener(ResponseEvent::class, $requestLoggerListener);
$ed->addListener(HttpClientExceptionEvent::class, $requestLoggerListener);
$ed->addListener(TmdbExceptionEvent::class, $apiErrorListener);
$ed->addListener(BeforeHydrationEvent::class, $hydrationLoggerListener);
$repository = new MovieRepository($client);
$movie = $repository->load(19995);