Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

New PSR-3 compatible Logger #248

Merged
merged 8 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ cache/*
composer.phar
composer.lock
config/custom.php
Deciphers.log
logs/*
vendor
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- new PSR-3 compatible logger implementation `Logger\Logger` to log all kind of events
- `SimpleContainer` has a new `logger` service with a `Logger\Logger` instance
- new folder `/logs` for log files
- `Format` implements `Logger\LoggerAware` interface
- `VideoInfo` implements `Logger\LoggerAware` interface
- `SignatureDecipher::decipherSignatureWithRawPlayerScript()` expects an optional logger as 3rd parameter

### Changed

- Logs are now stored in `/logs`, the file `Deciphers.log` can be deleted

### Removed

- **Breaking** method `SignatureDecipher::downloadPlayerScript()` was removed, use `SignatureDecipher::downloadRawPlayerScript()` instead
Expand All @@ -16,7 +29,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added

- new PSR-16 compatible cache implemantation `Cache\FileCache` to store data in the filesystem
- new PSR-16 compatible cache implementation `Cache\FileCache` to store data in the filesystem
- `SignatureDecipher::getPlayerInfoByVideoId()` to get the player ID and player url of a cipher video
- `SignatureDecipher::downloadRawPlayerScript()` to download the raw player script of a cipher video
- `SignatureDecipher::decipherSignatureWithRawPlayerScript()` to decipher a signature with a raw dicipher script
Expand Down
45 changes: 45 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,51 @@ function($custom = 'custom')

$container->set('cache', $cache);

// Create Logger
$logger = new \YoutubeDownloader\Logger\HandlerAwareLogger(
new \YoutubeDownloader\Logger\Handler\NullHandler()
);

if ( $config->get('debug') === true )
{
# code...
$now = new \DateTime('now', new \DateTimeZone($config->get('default_timezone')));

$filepath = sprintf(
'%s' . \DIRECTORY_SEPARATOR . '%s',
__DIR__ . \DIRECTORY_SEPARATOR . 'logs',
$now->format('Y')
);

if ( ! file_exists($filepath) )
{
mkdir($filepath);
}

$stream = fopen(
$filepath . \DIRECTORY_SEPARATOR . $now->format('Y-m-d') . '.log',
'a+'
);

if ( is_resource($stream) )
{
$handler = new \YoutubeDownloader\Logger\Handler\StreamHandler($stream, [
\YoutubeDownloader\Logger\LogLevel::EMERGENCY,
\YoutubeDownloader\Logger\LogLevel::ALERT,
\YoutubeDownloader\Logger\LogLevel::CRITICAL,
\YoutubeDownloader\Logger\LogLevel::ERROR,
\YoutubeDownloader\Logger\LogLevel::WARNING,
\YoutubeDownloader\Logger\LogLevel::NOTICE,
\YoutubeDownloader\Logger\LogLevel::INFO,
\YoutubeDownloader\Logger\LogLevel::DEBUG,
]);

$logger->addHandler($handler);
}
}

$container->set('logger', $logger);

return $container;
},
[getenv('CONFIG_ENV') ?: 'custom']
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"mikey179/vfsStream": "^1.6",
"phpunit/phpunit": "^4.8.35 || ^6.0",
"psr/container": "^1.0",
"psr/log": "^1.0",
"psr/simple-cache": "^1.0"
},
"autoload": {
Expand Down
Empty file added logs/.gitkeep
Empty file.
4 changes: 4 additions & 0 deletions src/Application/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class App
public function __construct(Container $container)
{
$this->container = $container;

$this->getContainer()->get('logger')->debug('App started');
}

/**
Expand Down Expand Up @@ -65,5 +67,7 @@ public function runWithRoute($route)
$controller = $controller_factory->make($route, $this);

$controller->execute();

$this->getContainer()->get('logger')->debug('Controller executed. App closed.');
}
}
5 changes: 5 additions & 0 deletions src/Application/ControllerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ abstract class ControllerAbstract implements Controller
public function __construct(App $app)
{
$this->app = $app;

$this->get('logger')->debug(
'{controller_name} created',
['controller_name' => get_class($this)]
);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Application/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function execute()
$video_info = \YoutubeDownloader\VideoInfo::createFromStringWithConfig($video_info_string, $config);
$video_info->setCache($this->get('cache'));

if ( $video_info instanceOf \YoutubeDownloader\Logger\LoggerAware )
{
$video_info->setLogger($this->get('logger'));
}

try
{
$mp3_info = $toolkit->getDownloadMP3($video_info, $config);
Expand Down
5 changes: 5 additions & 0 deletions src/Application/ResultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public function execute()
$video_info = \YoutubeDownloader\VideoInfo::createFromStringWithConfig($video_info_string, $config);
$video_info->setCache($this->get('cache'));

if ( $video_info instanceOf \YoutubeDownloader\Logger\LoggerAware )
{
$video_info->setLogger($this->get('logger'));
}

if ($video_info->getStatus() == 'fail')
{
$message = 'Error in video ID: ' . $video_info->getErrorReason();
Expand Down
35 changes: 31 additions & 4 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

namespace YoutubeDownloader;

use YoutubeDownloader\Logger\LoggerAware;
use YoutubeDownloader\Logger\LoggerAwareTrait;

/**
* a video format
*/
class Format
class Format implements LoggerAware
{
use LoggerAwareTrait;

/**
* Creates a Stream from array
*
Expand Down Expand Up @@ -37,6 +42,8 @@ public static function createFromArray(

private $data = [];

private $data_parsed = false;

private $raw_data = [];

/**
Expand Down Expand Up @@ -74,8 +81,6 @@ private function __construct(VideoInfo $video_info, array $data, array $config)
}

$this->raw_data = $data;

$this->parseUrl();
}

/**
Expand All @@ -85,6 +90,11 @@ private function __construct(VideoInfo $video_info, array $data, array $config)
*/
private function parseUrl()
{
if ( $this->data_parsed === true )
{
return;
}

parse_str(urldecode($this->data['url']), $url_info);

if (isset($this->raw_data['bitrate']))
Expand Down Expand Up @@ -122,7 +132,8 @@ private function parseUrl()

$sig = SignatureDecipher::decipherSignatureWithRawPlayerScript(
$decipherScript,
$this->raw_data['s']
$this->raw_data['s'],
$this->getLogger()
);

if ( strpos($this->raw_data['url'], 'ratebypass=') === false )
Expand All @@ -141,6 +152,8 @@ private function parseUrl()
$this->data['expires'] = isset($url_info['expire']) ? date("G:i:s T", $url_info['expire']) : '';
$this->data['ipbits'] = isset($url_info['ipbits']) ? $url_info['ipbits'] : '';
$this->data['ip'] = isset($url_info['ip']) ? $url_info['ip'] : '';

$this->data_parsed = true;
}

/**
Expand All @@ -160,6 +173,8 @@ public function getVideoId()
*/
public function getUrl()
{
$this->parseUrl();

return $this->data['url'];
}

Expand All @@ -170,6 +185,8 @@ public function getUrl()
*/
public function getItag()
{
$this->parseUrl();

return $this->data['itag'];
}

Expand All @@ -180,6 +197,8 @@ public function getItag()
*/
public function getQuality()
{
$this->parseUrl();

return $this->data['quality'];
}

Expand All @@ -190,6 +209,8 @@ public function getQuality()
*/
public function getType()
{
$this->parseUrl();

return $this->data['type'];
}

Expand All @@ -200,6 +221,8 @@ public function getType()
*/
public function getExpires()
{
$this->parseUrl();

return $this->data['expires'];
}

Expand All @@ -210,6 +233,8 @@ public function getExpires()
*/
public function getIpbits()
{
$this->parseUrl();

return $this->data['ipbits'];
}

Expand All @@ -220,6 +245,8 @@ public function getIpbits()
*/
public function getIp()
{
$this->parseUrl();

return $this->data['ip'];
}
}
37 changes: 37 additions & 0 deletions src/Logger/Handler/Entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace YoutubeDownloader\Logger\Handler;

/**
* An entry interface for a handler instance
*/
interface Entry
{
/**
* Returns the message
*
* @return string
*/
public function getMessage();

/**
* Returns the context
*
* @return array
*/
public function getContext();

/**
* Returns the level
*
* @return string
*/
public function getLevel();

/**
* Returns the created DateTime
*
* @return DateTime
*/
public function getCreatedAt();
}
25 changes: 25 additions & 0 deletions src/Logger/Handler/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace YoutubeDownloader\Logger\Handler;

/**
* Describes a handler instance
*/
interface Handler
{
/**
* Check if this handler handels a log level
*
* @param string $level A valid log level from LogLevel class
* @return boolean
*/
public function handles($level);

/**
* Handle an entry
*
* @param Entry $entry
* @return boolean
*/
public function handle(Entry $entry);
}
31 changes: 31 additions & 0 deletions src/Logger/Handler/NullHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace YoutubeDownloader\Logger\Handler;

/**
* a handler instance that handles no entries
*/
class NullHandler implements Handler
{
/**
* Check if this handler handels a log level
*
* @param string $level A valid log level from LogLevel class
* @return boolean
*/
public function handles($level)
{
return false;
}

/**
* Handle an entry
*
* @param Entry $entry
* @return boolean
*/
public function handle(Entry $entry)
{
return false;
}
}
Loading