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

StreamLogger #283

Open
wants to merge 2 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
14 changes: 8 additions & 6 deletions src/Tracy/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ final public function __construct()

/**
* Enables displaying or logging errors and exceptions.
* @param mixed production, development mode, autodetection or IP address(es) whitelist.
* @param string error log directory
* @param string administrator email; enables email sending in production mode
* @param mixed production, development mode, autodetection or IP address(es) whitelist.
* @param string|ILogger error log directory or logger instance
* @param string administrator email; enables email sending in production mode
* @return void
*/
public static function enable($mode = null, $logDirectory = null, $email = null)
public static function enable($mode = null, $logDirectoryOrLogger = null, $email = null)
{
if ($mode !== null || self::$productionMode === null) {
self::$productionMode = is_bool($mode) ? $mode : !self::detectDebugMode($mode);
Expand All @@ -159,8 +159,10 @@ public static function enable($mode = null, $logDirectory = null, $email = null)
if ($email !== null) {
self::$email = $email;
}
if ($logDirectory !== null) {
self::$logDirectory = $logDirectory;
if (is_string($logDirectoryOrLogger)) {
self::$logDirectory = $logDirectoryOrLogger;
} elseif ($logDirectoryOrLogger instanceof ILogger) {
self::$logger = $logDirectoryOrLogger;
}
if (self::$logDirectory) {
if (!preg_match('#([a-z]+:)?[/\\\\]#Ai', self::$logDirectory)) {
Expand Down
74 changes: 74 additions & 0 deletions src/Tracy/StreamLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

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

namespace Tracy;


/**
* Logger which writes to a file.
*/
class StreamLogger implements ILogger
{
/** @var string path to a file where errors should be logged */
public $path;


/**
* @param string $path
*/
public function __construct($path)
{
$this->path = $path;
}


public function log($message, $priority = self::INFO)
{
$line = $this->formatLogLine($message);
if (!@file_put_contents($this->path, $line . PHP_EOL, FILE_APPEND | LOCK_EX)) { // @ is escalated to exception
throw new \RuntimeException("Unable to write to log file '{$this->path}'. Is directory writable?");
}
}


/**
* @param string|\Exception|\Throwable
* @return string
*/
protected function formatLogLine($message)
{
return implode(' ', [
@date('[Y-m-d H-i-s]'), // @ timezone may not be set
preg_replace('#\s*\r?\n\s*#', ' ', $this->formatMessage($message)),
' @ ' . Helpers::getSource(),
]);
}


/**
* @param string|\Exception|\Throwable
* @return string
*/
protected function formatMessage($message)
{
if ($message instanceof \Exception || $message instanceof \Throwable) {
while ($message) {
$tmp[] = ($message instanceof \ErrorException
? Helpers::errorTypeToString($message->getSeverity()) . ': ' . $message->getMessage()
: Helpers::getClass($message) . ': ' . $message->getMessage() . ($message->getCode() ? ' #' . $message->getCode() : '')
) . ' in ' . $message->getFile() . ':' . $message->getLine();
$message = $message->getPrevious();
}
$message = implode("\ncaused by ", $tmp);

} elseif (!is_string($message)) {
$message = Dumper::toText($message);
}

return trim($message);
}
}