Skip to content

Simplify logging configuration for Docker #280

@JanTvrdik

Description

@JanTvrdik
  • bug report? no
  • feature request? yes

Description

To make Tracy work well with Docker, it needs to log to stdout or stderr. This is currently possible, but not easy to do. In this issue I'd like to discuss how we can make this common use-case easier.

The logger needs to be configured before or during Tracy::enable() call, otherwise errors which occur after Tracy::enable() and before Tracy::setLogger() will not be logged to stdout/stderr. Logging of error which occur before Tracy::enable() is handled by PHP itself (by configuring error_log and log_errors php.ini options).

Current solution may look like this

Tracy\Debugger::setLogger(new class extends Tracy\Logger {
	public function __construct() {
		// intentionally do not call parent constructor,
		// because we don't actually need the parameters
	}


	public function log($value, $priority = self::INFO) {
		@file_put_contents(
			'php://stderr',
			str_replace("\n", " ", $this->formatMessage($value)) . "\n",
			FILE_APPEND | LOCK_EX
		);
	}
});

Tracy\Debugger::enable();

This is solution is ugly (we don't use most of Tracy\Logger, only the formatMessage method) and way to long to write.

Proposed Solution A

// specify path to file instead of log directory
Tracy\Debugger::enable(Tracy\Debugger::DETECT, 'php://stderr');

Proposed Solution B (my preferred choice)

// allow Tracy\ILogger instance as alternative to "log directory"
// and define simple Tracy\StreamLogger class
$logger = new Tracy\StreamLogger('php://stderr');
Tracy\Debugger::enable(Tracy\Debugger::DETECT, $logger);

Note that in both cases, generating bluescreens will be disabled. This is not a problem for me, because after DI container is successfully compiled and initialized, a smarter (and more complex) logger will be used instead which handles this. But maybe other people have different requirements and use-cases so that should be probably considered as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions