Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger refactoring #281

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2

Expand Down Expand Up @@ -31,7 +27,7 @@ jobs:
php: 7.1
install:
# Install Nette Code Checker
- travis_retry composer create-project nette/code-checker temp/code-checker ~2 --no-progress
- travis_retry composer create-project nette/code-checker temp/code-checker ^3.0 --no-progress
# Install Nette Coding Standard
- travis_retry composer create-project nette/coding-standard temp/coding-standard --no-progress
# Install new Node.js and Eslint
Expand All @@ -40,7 +36,7 @@ jobs:
- nvm use stable
- npm install eslint
script:
- php temp/code-checker/src/code-checker.php --short-arrays -i tests/Tracy/fixtures -i examples/assets
- php temp/code-checker/src/code-checker.php --strict-types -i tests/Tracy/fixtures -i examples/assets
- php temp/coding-standard/ecs check src tests examples tools --config tests/coding-standard.neon
- ./node_modules/.bin/eslint "src/**/*.js"

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ install:
# Install PHP
- IF EXIST c:\php (SET PHP=0) ELSE (mkdir c:\php)
- IF %PHP%==1 cd c:\php
- IF %PHP%==1 curl https://windows.php.net/downloads/releases/archives/php-5.6.14-Win32-VC11-x86.zip --output php.zip
- IF %PHP%==1 curl https://windows.php.net/downloads/releases/archives/php-7.1.17-Win32-VC14-x64.zip --output php.zip
- IF %PHP%==1 7z x php.zip >nul
- IF %PHP%==1 echo extension_dir=ext >> php.ini
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
}
],
"require": {
"php": ">=5.4.4",
"php": ">=7.1",
"ext-session": "*",
"ext-json": "*"
},
"require-dev": {
"nette/di": "~2.3",
"nette/tester": "~1.7"
"nette/di": "^2.4 || ^3.0",
"nette/tester": "^2.0",
"psr/log": "^1.0"
},
"suggest": {
"https://nette.org/donate": "Please support Tracy via a donation"
Expand All @@ -33,7 +34,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "2.5-dev"
"dev-master": "3.0-dev"
}
}
}
2 changes: 2 additions & 0 deletions examples/ajax.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

use Tracy\Debugger;
Expand Down
2 changes: 2 additions & 0 deletions examples/barDump.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

use Tracy\Debugger;
Expand Down
2 changes: 2 additions & 0 deletions examples/dump.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

use Tracy\Debugger;
Expand Down
2 changes: 2 additions & 0 deletions examples/exception.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

use Tracy\Debugger;
Expand Down
2 changes: 2 additions & 0 deletions examples/fatal-error.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

use Tracy\Debugger;
Expand Down
2 changes: 2 additions & 0 deletions examples/firelogger.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

use Tracy\Debugger;
Expand Down
2 changes: 2 additions & 0 deletions examples/notice.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

use Tracy\Debugger;
Expand Down
2 changes: 2 additions & 0 deletions examples/output-debugger.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

Tracy\OutputDebugger::enable();
Expand Down
2 changes: 2 additions & 0 deletions examples/preloading.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

use Tracy\Debugger;
Expand Down
2 changes: 2 additions & 0 deletions examples/redirect.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

use Tracy\Debugger;
Expand Down
2 changes: 2 additions & 0 deletions examples/warning.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/tracy.php';

use Tracy\Debugger;
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ composer require tracy/tracy

Alternatively, you can download the whole package or [tracy.phar](https://github.com/nette/tester/releases) file.

Tracy 3.0-dev requires PHP version 7.1 or newer (supports PHP up to 7.2) and is compatible with Chrome 49+, Firefox 45+, MS Edge 12+, Safari 10+ and iOS Safari 10.2+.

Tracy 2.5 requires PHP version 5.4.4 or newer (supports PHP up to 7.2) and is compatible with Chrome 49+, Firefox 45+, MS Edge 12+, Safari 10+ and iOS Safari 10.2+.

Tracy 2.4 requires PHP version 5.4.4 or newer (supports PHP up to 7.2) and is compatible with Chrome 29+, Firefox 28+, IE 11+, MS Edge 12+, Safari 9+ and iOS Safari 9.2+.
Expand Down
16 changes: 11 additions & 5 deletions src/Bridges/Nette/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Tracy\Bridges\Nette;

use Latte;
Expand All @@ -19,11 +21,11 @@
*/
class Bridge
{
public static function initialize()
public static function initialize(): void
{
$blueScreen = Tracy\Debugger::getBlueScreen();

$blueScreen->addPanel(function ($e) {
$blueScreen->addPanel(function ($e): ?array {
if ($e instanceof Latte\CompileException) {
return [
'tab' => 'Template',
Expand All @@ -39,9 +41,10 @@ public static function initialize()
. '</div></pre>',
];
}
return null;
});

$blueScreen->addAction(function ($e) {
$blueScreen->addAction(function ($e): ?array {
if (
$e instanceof Latte\CompileException
&& @is_file($e->sourceName) // @ - may trigger error
Expand All @@ -53,9 +56,10 @@ public static function initialize()
'label' => 'fix it',
];
}
return null;
});

$blueScreen->addAction(function ($e) {
$blueScreen->addAction(function ($e): ?array {
if ($e instanceof Nette\MemberAccessException || $e instanceof \LogicException) {
$loc = $e instanceof Nette\MemberAccessException ? $e->getTrace()[1] : $e->getTrace()[0];
if (preg_match('#Cannot (?:read|write to) an undeclared property .+::\$(\w+), did you mean \$(\w+)\?#A', $e->getMessage(), $m)) { // @ - may trigger error
Expand All @@ -71,9 +75,10 @@ public static function initialize()
];
}
}
return null;
});

$blueScreen->addPanel(function ($e) {
$blueScreen->addPanel(function ($e): ?array {
if (
$e instanceof Nette\Neon\Exception
&& preg_match('#line (\d+)#', $e->getMessage(), $m)
Expand All @@ -87,6 +92,7 @@ public static function initialize()
: BlueScreen::highlightPhp($trace['args'][0], $m[1]),
];
}
return null;
});
}
}
10 changes: 5 additions & 5 deletions src/Bridges/Nette/MailSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Tracy\Bridges\Nette;

use Nette;
Expand All @@ -25,7 +27,7 @@ class MailSender
private $fromEmail;


public function __construct(Nette\Mail\IMailer $mailer, $fromEmail = null)
public function __construct(Nette\Mail\IMailer $mailer, string $fromEmail = null)
{
$this->mailer = $mailer;
$this->fromEmail = $fromEmail;
Expand All @@ -34,12 +36,10 @@ public function __construct(Nette\Mail\IMailer $mailer, $fromEmail = null)

/**
* @param mixed $message
* @param string $email
* @return void
*/
public function send($message, $email)
public function send($message, string $email): void
{
$host = preg_replace('#[^\w.-]+#', '', isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : php_uname('n'));
$host = preg_replace('#[^\w.-]+#', '', $_SERVER['HTTP_HOST'] ?? php_uname('n'));

$mail = new Nette\Mail\Message;
$mail->setHeader('X-Mailer', 'Tracy');
Expand Down
10 changes: 6 additions & 4 deletions src/Bridges/Nette/TracyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Tracy\Bridges\Nette;

use Nette;
Expand Down Expand Up @@ -43,7 +45,7 @@ class TracyExtension extends Nette\DI\CompilerExtension
private $cliMode;


public function __construct($debugMode = false, $cliMode = false)
public function __construct(bool $debugMode = false, bool $cliMode = false)
{
$this->debugMode = $debugMode;
$this->cliMode = $cliMode;
Expand Down Expand Up @@ -93,13 +95,13 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
}

$logger = $builder->getDefinition($this->prefix('logger'));
if ($logger->getFactory()->getEntity() !== ['Tracy\Debugger', 'getLogger']) {
if ($logger->getFactory()->getEntity() !== [Tracy\Debugger::class, 'getLogger']) {
$initialize->addBody($builder->formatPhp('Tracy\Debugger::setLogger(?);', [$logger]));
}
if ($this->config['netteMailer'] && $builder->getByType('Nette\Mail\IMailer')) {
if ($this->config['netteMailer'] && $builder->getByType(Nette\Mail\IMailer::class)) {
$initialize->addBody($builder->formatPhp('Tracy\Debugger::getLogger(?)->mailer = ?;', [
$logger,
[new Nette\DI\Statement('Tracy\Bridges\Nette\MailSender', ['fromEmail' => $this->config['fromEmail']]), 'send'],
[new Nette\DI\Statement(Tracy\Bridges\Nette\MailSender::class, ['fromEmail' => $this->config['fromEmail']]), 'send'],
]));
}

Expand Down
62 changes: 62 additions & 0 deletions src/Bridges/Psr/PsrToTracyLoggerAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of the Tracy (https://tracy.nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Tracy\Bridges\Psr;

use Psr;
use Tracy;


/**
* Psr\Log\LoggerInterface to Tracy\ILogger adapter.
*/
class PsrToTracyLoggerAdapter implements Tracy\ILogger
{
/** Tracy logger priority to PSR-3 log level mapping */
private const PRIORITY_MAP = [
Tracy\ILogger::DEBUG => Psr\Log\LogLevel::DEBUG,
Tracy\ILogger::INFO => Psr\Log\LogLevel::INFO,
Tracy\ILogger::WARNING => Psr\Log\LogLevel::WARNING,
Tracy\ILogger::ERROR => Psr\Log\LogLevel::ERROR,
Tracy\ILogger::EXCEPTION => Psr\Log\LogLevel::ERROR,
Tracy\ILogger::CRITICAL => Psr\Log\LogLevel::CRITICAL,
];

/** @var Psr\Log\LoggerInterface */
private $psrLogger;


public function __construct(Psr\Log\LoggerInterface $psrLogger)
{
$this->psrLogger = $psrLogger;
}


public function log($value, string $priority = self::INFO)
{
if ($value instanceof \Throwable) {
$message = $value->getMessage();
$context = ['exception' => $value];

} elseif (!is_string($value)) {
$message = trim(Tracy\Dumper::toText($value));
$context = [];

} else {
$message = $value;
$context = [];
}

$this->psrLogger->log(
self::PRIORITY_MAP[$priority] ?? Psr\Log\LogLevel::ERROR,
$message,
$context
);
}
}
Loading